For example, with GNU, lsyou can control the coloring with a parameter --color[=WHEN]. Now, in this case, the equal sign is crucial, since it lsmust distinguish between an optional argument --colorand positional arguments (which are lists of files). This is a ls --colorlist of files with colors that matches ls --color=always, but ls --color alwaysdisplays a file always(and colors).
Now from what I saw, argparseit seems to be accepting arguments for long parameters using syntax --longopt <argument>, which will make it impossible to make the argument optional. That is, if I try to implement mylswith the same behavior as GNU ls(this is just an example), I would run into problems, because now it myls --color alwaysmeans the same as myls --color=always(and not as required --colorwithout an argument and alwaysas a positional argument).
I know that I can get around this using myls --color -- always, but there is no way to make this work without this workaround? That is, to argparseindicate that the argument --colormust be provided with syntax --color[=WHEN].
Note that I do not want to rely on the fact that the option --colorhas a finite number of valid arguments. Here is an example of what I tried that did not work properly:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo",
action="store",
nargs="?")
parser.add_argument("frob",
action="store",
nargs=argparse.REMAINDER)
print(parser.parse_args(["alpha", "beta"]))
print(parser.parse_args(["--foo", "alpha", "beta"]))
print(parser.parse_args(["--foo=bar", "alpha", "beta"]))
With an exit:
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo='alpha', frob=['beta'])
Namespace(foo='bar', frob=['alpha', 'beta'])
pay attention to the second, where it is alphainterpreted as an argument --foo. I wanted:
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo='bar', frob=['alpha', 'beta'])