Skip to content Skip to sidebar Skip to footer

Argparse `append` Not Working As Expected

I am trying to configure argparse to allow me to specify arguments that will be passed onto another module down the road. My desired functionality would allow me to insert argument

Solution 1:

The problem isn't that -A isn't allowed to be called more than once. It's that the -t is seen as a separate option, not an argument to the -A option.

As a crude workaround, you can prefix a space:

python my_program.py \
  -A " -k filepath" \
  -A " -t"

Given the following Minimal, Complete and Verifiable Example:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-A', '--module-args',
                    help="Arg to be passed through to the specified module",
                    action='append')
args = parser.parse_args()
print repr(args.module_args)

...that usage returns:

[' -k filepath', ' -t']

whereas leaving off the leading spaces reproduces your error.


Post a Comment for "Argparse `append` Not Working As Expected"