Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 07:40:05 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Command line argument processing  (Read 2127 times)

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Points: -5
  • Posts: 3,397
    • View Profile
    • Donate to Member
Command line argument processing
« on: April 10, 2010, 04:10:17 AM »
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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Command line argument processing
« Reply #1 on: April 10, 2010, 07:47:40 AM »
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • View Profile
    • Donate to Member
Re: Command line argument processing
« Reply #2 on: April 19, 2010, 10:32:39 AM »
What are your thoughts? How do you handle command-line args?
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.