I had the same need using argparse
too.
The thing is parse_args
function of an argparse.ArgumentParser
object instance implicitly takes its arguments by default from sys.args
. The work around, following Martijn line, consists of making that explicit, so you can change the arguments you pass to parse_args
as desire.
def main(args):
# some stuff
parser = argparse.ArgumentParser()
# some other stuff
parsed_args = parser.parse_args(args)
# more stuff with the args
if __name__ == '__main__':
import sys
main(sys.argv[1:])
The key point is passing args to parse_args
function.
Later, to use the main, you just do as Martijn tell.