Note that there are some explanatory texts on larger screens.

plurals
  1. POPython and argparse: how to vary required additional arguments based on the first argument?
    primarykey
    data
    text
    <p>My goal is to write a Python command line utility using argparse that has multiple commands that each have different sets of required inputs.</p> <p>I tried reading through the docs, a few Google searches, and stack overflow and couldn't find anything. I can think of a few possible solutions but they are ugly and require dealing with the help docs and validation manually. My suspicion is that this is the kind of problem with a common, effective solution already well enough known, and I am just lacking the right terms to search for it, or it is obscure enough by a small margin that it isn't posted in many places.</p> <p>The best idea I have right now is to have one positional argument, and somehow have different requirements for a set of additional arguments based on the value of that input. Maybe I will parse twice?</p> <p>As an example, this is a similar case:</p> <p>There is one positional argument, animal</p> <p>Options for animal are cat, lizard, fish</p> <p>For cat, arguments claws, whiskers, paws are required</p> <p>For lizard, arguments scale_color, favorite_food are required</p> <p>For fish, argument water_type is required</p> <p>We want the required additional arguments for each different animal value to be documented in -h without resorting to unorthodox practice. </p> <p>I considered doing this with an optional argument for each of the main category choices. This is unattractive because the utility really only wants to take one of those arguments, and if I can avoid reinventing the wheel in terms of enforcing and documenting this, I would prefer to.</p> <p>I could do something like:</p> <pre><code>valid_commands = ['a','b','c','d','e','f','g'] parser.add_argument('command', choices = valid_commands) parser.add_argument('inputs', nargs = '*') </code></pre> <p>But then I don't have good input checks on the additional arguments for each command choice.</p> <p>Is there some normal way this is done? Surely it is fairly common to write a utility that has several possible commands, and different required arguments for each. I could definitely get most of want I need by adding manual checks and help documentation, but this is the kind of thing I will probably do enough times that it's worth it to get it right on the first try.</p> <p>Thanks for reading and let me know if I can help provide information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload