Suppose I have an argparse python script:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo", required=True)
Now I want to add another option --bar, which would default to appending "_BAR" to whatever was specified by --foo argument.
My goal:
>>> parser.parse_args(['--foo', 'FOO'])
>>> Namespace(foo='FOO', bar="FOO_BAR")
AND
>>> parser.parse_args(['--foo', 'FOO', '--bar', 'BAR'])
>>> Namespace(foo='FOO', bar="BAR")
I need something like this:
parser.add_argument("--bar", default=get_optional_foo + "_BAR")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…