title: Parse command line with state in Python
body: <p>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:</p>
  <p><code>readmulticsv --cols 1,2,4 file1.csv --date_format &quot;%Y-%m-%d&quot; file2.csv --cols 4,3,9 file3.csv file4.csv</code></p>
  <p>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 &quot;%m/%d/%Y&quot; format, but switch to &quot;%Y-%m-%d&quot; 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:</p>
  <pre><code>[[&quot;file1.csv&quot;, &quot;1,2,4&quot;, &quot;%m/%d/%Y&quot;],
   [&quot;file2.csv&quot;, &quot;1,2,4&quot;, &quot;%Y-%m-%d&quot;],
  ...
  ]
  </code></pre>
  <p>Implementing this is straightforward if you walk sys.argv manually.</p>
  <p>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 &quot;walk&quot; 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.</p>
  <p>Is there an alternative arg parser that can do it all (help, defaults, a Namespace)?</p>
  <p>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.</p>
  <p>Thanks,</p>
  <p>--jh--</p>