Background
Many command-line utilities provide special handling for all arguments after a double dash (--). Examples:
git diff: All arguments after -- are paths (which can start with -):
git diff [options] -- [path...]
Some wrapper tools use this to pass arbitrary arguments on to the tool they are wrapping -- even arguments which may conflict with their own!
foowrap -a -- -a
│ └─── Passed to wrapped tool
└───────── Consumed by wrapper
Problem
How can we accomplish this using argparse?
It seems that
argparse(specificallyparse_known_args()already supports this natively, although it is not clearly documented:We can see that:
-aafter--appears inextra-bafter--appears inextraand does not setb=Truein the namespaceIf you want to ignore the
--separator, it can be filtered out:I actually discovered this while attempting to code up a "what doesn't work" example. Because it is not obvious to me from the documentation I decided to ask+answer anyway.