This is likely beyond the scope of the argparse module, but I'll try to describe my issue using an example.
I have some fruits and some files attributed to each fruit. Specifically Apple, Banana, and Orange. Apple has 10 files associated with it, Banana has 7, and Oranges has 9. I can hardcode -a
, -b
, -o
each taking nargs='+'
to handle this toy example in a Python command-line script. But say I have a variable number of fruits, or a very large number (maybe 50). It would be crazy to hardcode a flag for each type, what is the best solution here?
I would create an argument for each of the fruits, but I would do it in the DRYest way I could:
Here is the resulting help message:
If there were a variable number of fruits (controlled by an environment variable, or the presence of configuration files, for example), then my loop wouldn't be hardcoded, but instead it might be:
If I wanted to name the parameters sequentially, as OP's comment suggests, I might code the loop:
And here is the resulting help message: