Other Software > Developer's Corner
Command line argument processing
(1/1)
Josh:
To all the coders out there, I come to you with this inquiry.
I have recently delved into the wonderful world of powershell 2.0 for the Windows platform. I absolutely love its power, simplicity and extendability. My question is this. After programming assignment #2 from the programming school here at DC, I have started to wonder how others process command line arguments.
My primary question is how do you do this? Do you hard code each parameter into a big "switch" statement and cycle through each argument or do you use a dedicated function that processes arguments? I am wondering if I should delve into coding a powershell function to process command line arguments automatically or make it on a per script basis.
What are your thoughts? How do you handle command-line args?
housetier:
When I have to parse command line options, I always look if there is something like getopts, getopt or something similar. It makes paring so very much simpler.
Zend PHP Framework has getopts, bash has it built in; and I am sure there are many free libraries or builtins for many projects, frameworks, and shells.
Sadly, I do not know of such a tool for Powershell, but it would surprise me if you couldn't find a getopt-like thingie for it.
If there is no getopts I try to make someone else code the command line script, because I think it is just too much hassle to reinvent the wheel (of of command line parsing) all over again.
jpprater:
What are your thoughts? How do you handle command-line args?
-Josh (April 10, 2010, 04:10 AM)
--- End quote ---
With Powershell, in most cases, it makes the most sense to use a switch statement, but I'll use an if-then occasionally. Powershell makes command-line args very simple to deal with. No crazy parsing that you have to do, just a param() statement.
Navigation
[0] Message Index
Go to full version