I am writing a utility (call it runner
) which, as its main functionality, runs another script. That script to be run is passed as an argument, as well as any arguments that should be passed to the script. So, for example:
runner script.py arg1 arg2
will run script.py
as a subprocess, with arg1
and arg2
as arguments. This is easy enough, using the following:
parser = argparse.ArgumentParser()
parser.add_argument("script", help="The script to run")
parser.add_argument("script_args", nargs="*", help="Script arguments", default=[])
args = parser.parse_args()
The complication is that I have some additional arguments passed to runner
, which modify how the script will be called. One of these is --times
, which will be used to determine how many times to run the script. So:
runner --times 3 script.py arg1 arg2
will run script.py
three times, with arg1
and arg2
as arguments.
The problem I am trying to solve is this. I want any arguments after the script name to be treated as arguments to pass to the script, even if they match argument names of runner
. So:
runner script.py arg1 arg2 --times 3
should run script.py once, and pass the arguments arg1
, arg2
, --times
, and 3
to script.py.
I can't find any way to tell the ArgumentParser to treat all arguments after the script
arg as positional.
I know the caller of the script could force this behavior using the --
special argument, so the following would yield the desired behavior:
runner -- script.py arg1 arg2 --times 3
but I want the program to take care of this instead. Is this behavior possible?