I have an upstream system that invokes my program with varying arg names. Example:
foo --xyz1 10 --xyz2 25 --xyz3 31
I would like the result of argparsing to be a xyz = [10, 25, 31].
The names of my args have a common prefix, but unfortunately have to differ at least with a different numeric suffix, which also indicates order. I also don't have a fixed number of args.
Is there a way to model this with argparse? Either with what is available through some combination of built-in capabilities, or by overriding/pluging in some custom parser handling.
Here's what I did for reference (quick and dirty version), though I also like Stephen Rauch's answer (so I'll mark that as an answer -- esp. since I used internal implementation detail for my solution):
Usage:
Note -- This supports args of the form --xyz:1, --xyz:2, ... which is slightly different than the original question.
I would suggest a bit of pre-processing to achieve this:
Code:
To Test:
Output:
To use:
Nominally instead of a fixed
cmd_line
as in the above example this would be called with something like:UPDATE: If you need --xyz=31 (ie
=
separator):Then you will need to change:
To: