hpekristiansen
hpekristiansen

Reputation: 1070

What is the recommended command line argument syntax?

I am writing a executable script(perl) that takes arguments from the command line. Here is some different syntaxs, I can think of:

  1. myPerlScript 10 42
  2. myPerlScript myarg1=10 myarg2=42
  3. myPerlScript -myarg1=10 -myarg2=42
  4. myPerlScript myarg1 10 myarg2 42
  5. myPerlScript -myarg1 10 -myarg2 42

I do not like option 1. as it is not possible to give the arguments in arbitrary order, or skip the arguments to rely on default values. I would also like to give 2(or 3,...) dimensional arguments. Here are some examples again:

  1. myPerlScript myarg 10x20
  2. myPerlScript myarg=10x20
  3. myPerlScript -myarg 10x20
  4. myPerlScript -myarg=10x20
  5. myPerlScript -myarg=10 20
  6. myPerlScript -myarg=10,20

What is the recommended syntax? or what would you recommend? and does it depend on situations and/or what the script is used for?

Upvotes: 0

Views: 249

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230561

I don't know much about perl, but in ruby world (and the rest of unix that I can observe) this seems to be the most accepted idiom:

myPerlScript --myarg1 10 --myarg2 42

There's a difference between single and double dash. Some commands have two forms: long (full command name) and short (a single letter, for brevity). Double dash is for long form, single - for short one. Real life example:

mysql --user root
mysql -u root

I'd say that using single dash with long form will surprise people. Don't do that :)

And I also can recommend this excellent book: Building awesome command-line applications in Ruby. Although it will be of less value to you, because it's in ruby, not perl.

Upvotes: 1

Related Questions