3

Question:

How a app should parse command line when there is a Command after a double dash? (Not duplicated of this and this)

I know what will double dash normally do:

A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as file‐ names and arguments. An argument of - is equivalent to --

So it set the following things as arguments, for example: myapp -f <args> ... Then $ myapp -f -- -a -b will treat -a and -b as arguments of -f, instead of Flags

But what will happen when an app required:

myapp cmd <arg> -f <args>...

And the command line is $ myapp -f -- test cmd sth, Should it parsed as:

  • myapp received a -f Flag with arguments test, cmd and sth
  • or myapp received a cmd Command followed by sth, a -f with argument test

I'm writing a command line parser for python so I need to know how it should behave.

Thx a lot :)

Community
  • 1
  • 1
user2395922
  • 841
  • 1
  • 9
  • 7

4 Answers4

4

You wrote

$ myapp -f -- -a -b will treat -a and -b as arguments of -f, instead of Flags

Not quite. The double dash makes -a and -b arguments to myapp. If -f is expecting an argument, using a double dash there will raise an error, since no such argument is given.

If your parser defines a sub parser, any options that precede it are assumed to be options defined by the main parser, and any options the follow the subcommand are part of the sub parser. For example, with

p = ArgumentParser()
p.add_argument("-v", action='store_true')
sp = p.add_subparsers()
p1 = sp.add_parser('cmd')
p1.add_argument('-v')

the command line myapp -v cmd -v test would treat the two -v differently; the first is the store_true option defined on p, the second the option defined on p1. Your command line

myapp -f -- test cmd sth

produces an error if -f expects an argument. If -f does not, then myapp simply one option -f and 3 positional arguments test, cmd, and sth.

chepner
  • 389,128
  • 51
  • 403
  • 529
1

Since none of the additional arguments in myapp -f -- test cmd sth start with a hyphen, there is no semantic difference between that command and myapp -f test cmd sth. So whatever that command does, the prior one does as well.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • 2
    Not quite; if `-f` expects an argument, then the first produces an error, and the second uses `test` as the argument to `-f`. – chepner Oct 09 '14 at 19:22
1

An argument of - is equivalent to --.

Are you quoting this from some authoritative reference or is this your own interpretation? Because many programs treat a single - as a special file name for standard input / output. So, for example in

$ diff - somefile

the - means “read the first file from standard input” whereas

$ diff -- -o somefile

means “read the first file from the awkwardly named file -o”. If we wanted to compare a file read from standard input with -o, the command would be

$ diff - -- -o

which is equivalent to

$ diff -- - -o

but only if you are not applying the rule you are quoting.

But what will happen when an app required:

myapp cmd <arg> -f <args>...

I would consider that a questionable design in the first place because usually, options should go before positional arguments. Although they may be mixed if this is unambiguous, which here it doesn't seem to be.

The rest of your example is not entirely clear to me (Did you possibly forget to list cmd in your examples?) but it seems that the source of the problem is accepting more than one argument list of variable length. Here, it doesn't really matter whether they are arguments to an option or the main program directly. Programs that accept such options usually have to introduce their own syntax to disambiguate the command line. For example, the -exec option of the find program terminates its argument list with a ;. Another trick is to ask a user to pass the arguments as one token and separate them with a special character. For example, the -o option of the mount program accepts a comma-separated list of arguments passed to the program as one token.

I'm writing a command line parser for python so I need to know how it should behave.

Just to be sure: Did you discover the argparse module from the standard library yet?

5gon12eder
  • 21,864
  • 5
  • 40
  • 85
0

The application can feel free on how to interpret the command line arguments. There are some conventions that most developers adhered to - but some don't. Read the documentation for the command

Ed Heal
  • 55,822
  • 16
  • 77
  • 115