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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…