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

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Thu Oct 15 22:59:10 EDT 2020


On 2020-10-16 at 11:27:56 +1100,
Regarding "Re: CLI parsing—with `--help` text—`--foo bar`, how to give additional parameters to `bar`?,"
Samuel Marks <samuelmarks at gmail.com> wrote:

> The feature that existing CLI parsers are missing is a clean syntax
> for specifying options on the second parameter (the "value"), where
> there may be different options available depending on which you
> choose.
> 
> For example:
> https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Adam
> has `learning_rate`, `beta_1`, `beta_2`, `epsilon`, and `amsgrad`*
> Whereas
> https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/RMSprop
> has `learning_rate`, `rho`, `momentum`, `epsilon`, `centered`*
> 
> *with clipnorm, clipvalue, decay hidden behind kwargs
> 
> So the question is how to expose this as CLI options. `--optimizer
> Adam` is a good first step, but it should error if I try and give it
> `momentum`. The comma syntax is my favourite so far.

I guess to me, that level of complexity (an optimizer followed by a
series of key/value pairs) is at least partially an application problem
rather than a library problem.  IOW, if my command line parser tells me
that the user specified "--optimizer" with an argument of
"Adam,learning_rate=1.0,beta_1=3.0," then I'll parse the rest in my
application.  At some point, I might consider a lex/yacc or ini-file
solution instead of that kind of command line.

> I guess I'll just have to write a validator outside the CLI parser to
> handle this…

Exactly.  :-)

-- 
“Whoever undertakes to set himself up as a
judge of Truth and Knowledge is shipwrecked
by the laughter of the gods.” – Albert Einstein
Dan Sommers, http://www.tombstonezero.net/dan


More information about the Python-list mailing list