I presume this might be beyond what argparse can do, but maybe someone knows a pretty trick.
I want my first argument to be positional, optional and only get consummed if it is in a list of choices. If not consummed it should get a default value.
parser = argparse.ArgumentParser()
parser.add_argument('myoption', nargs='?', choice=['a', 'b', 'c'], default='a')
Then I want to catch the rest of the arguments to use them in another command.
parsed, rest = parser.parse_known_args()
myoption = parsed.myoption
But I am still forced to pass an argument among the choices a,b,c as first positional argument. For example this works
python myprog.py a things to pipe to another command
but this doesn't
python myprog.py things to pipe to another command
python myprog.py -m things to pipe to another command
and raises
argument type: invalid choice: 'things' (choose from 'a', 'b', 'c')
I also tried to use parser.parse_known_intermixed_args() but it doesn't work.
How can I get argpasre to ignore the positional arguments that are not in the list of choices ? Is there a nice way to hack this while preserving argparse automatic outputs like error messsages and help display ?