Some program command lines have state that applies to the arguments following the state-setting option (the '--' argument to rm and touch is an example; ffmpeg is infamous for stateful arg parsing). For example:
readmulticsv --cols 1,2,4 file1.csv --date_format "%Y-%m-%d" file2.csv --cols 4,3,9 file3.csv file4.csv
Here, it will pull columns 1, 2, and 4 from file1.csv and file2.csv and then pull columns 4, 3, and 9, in that order, from file3.csv and file4.csv. Further, it will start interpreting dates (in the first column of the --cols argument) in file1.csv with a default "%m/%d/%Y" format, but switch to "%Y-%m-%d" for the remaining files. What I want is a list of lists, where each element list has the file name and the values of the relevant state variables:
[["file1.csv", "1,2,4", "%m/%d/%Y"],
["file2.csv", "1,2,4", "%Y-%m-%d"],
...
]
Implementing this is straightforward if you walk sys.argv manually.
Is there a way to do this with argparse? My program uses argparse for many other options and its nice help feature, and the whole code is written around its Namespace object. I could use parse_known_args() and leave the rest for a "walk" approach, but that excludes --cols, --date_format, and the files from the help and Namespace. I've tried figuring out an Action(), but I'm not sure how to proceed there. The docs for setting that up aren't super clear to me and I don't see how to access the existing state.
Is there an alternative arg parser that can do it all (help, defaults, a Namespace)?
My application is a program to calculate stock basis, gain, and growth by reading CSV transaction files, where the investments have transferred between brokers with different file formats and format changes over several decades. I could write a converter for each of the old formats, but I'd rather write a single program that works directly from the source data.
Thanks,
--jh--