CLI parsing—with `--help` text—`--foo bar`, how to give additional parameters to `bar`?

Cameron Simpson cs at cskk.id.au
Thu Oct 15 19:26:31 EDT 2020


One other thing:

On 15Oct2020 20:53, Samuel Marks <samuelmarks at gmail.com> wrote:
>Idea: preprocess `sys.argv` so that this syntax would work
>`--optimizer Adam[learning_rate=0.01]`*
>
>*square rather than round so as not to require escape characters or
>quoting in `sh`

Square brackets are also shell syntax, introducing glob character
ranges. You're only not noticing probably because an unmatched glob is
normally let through unchanged.

For example:

   somecmd x[b]

would go through as "x[b]" _unless_ you had a matching local filename
(in this case a file named "xb") in which case the shell would match the
glob and pass through the match (in this case "xb").

You'll find this issue a fair bit: if you want some punctuation, someone
else has probably already wanted that punctuation for something. You'll
still need to quote things for a lot of stuff.

Commas are not special. You could try:

    --optimizer Adam,learning_rate=0.01,something_else=3

which would work for a fair amount of stuff.

>Unfortunately this approach wouldn't give nice `--help` text.
>What's the right solution here?

To get nice help text you need your command line parser to be able to
compose that help text in some way. Usually the help text in argparse is
supplied when you define the option.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list