For example:
例如:
example.py
example.py
parser = argparse.ArgumentParser(description="Will take arguments... or none")
parser.add_argument("-a", action="store_true")
parser.add_argument("-b", action="store_true")
parser.add_argument("-c", action="store_true")
parser.add_argument("-d", action="store_true")
args = parser.parse_args()
print args
I want example.py to set a
to True
, but only if either:
我希望example.py将a设置为True,但前提是:
- The
-a
flag is used - 使用-a标志
- No flags are used
- 没有使用标志
I tried messing around with
我试着乱搞
parser.set_defaults(a=True, b=False)
parser.set_defaults(a = True,b = False)
and
和
parser.add_argument("-a", action="store_true", default=True)
parser.add_argument(“ - a”,action =“store_true”,默认= True)
but they will set a
to True
even if I decide to use the b
flag.
但即使我决定使用b标志,它们也会设置为True。
3 个解决方案
#1
5
yes using the default values will set a to True even other arguments are specified. This will violate your second requirement, following is a simple fix with a naive condition checking.
是的,使用默认值将a设置为True,甚至指定其他参数。这将违反您的第二个要求,以下是一个简单的修复与天真的条件检查。
parser = argparse.ArgumentParser(description="Will take arguments... or none")
parser.add_argument("-a", action="store_true")
parser.add_argument("-b", action="store_true")
parser.add_argument("-c", action="store_true")
parser.add_argument("-d", action="store_true")
args = parser.parse_args()
if not (args.b or args.c or args.d):
args.a=True
print args
#2
1
Sounds like you want a 'radio button' effect - choosing just one of several alternatives. An alternative to a set of flags would be an argument with choices.
听起来你想要一个“单选按钮”效果 - 只选择其中一种选择。一组标志的替代方案将是具有选择的参数。
parser.add_argument('flag', choices=['a','b','c','d'], default='a', nargs='?')
You can check the result in args.flag
, which will be one of 4 strings.
您可以在args.flag中检查结果,它将是4个字符串之一。
Obviously the positional argument couple replaced by a flag, e.g. -f
.
显然,位置参数被标志所取代,例如-F。
#3
1
I went for the following solution:
我去了以下解决方案:
parser = argparse.ArgumentParser(description="Will take arguments... or none")
lettergroup = parser.add_mutually_exclusive_group()
lettergroup.add_argument("-a", action="store_const", dest="letter", const="a", default="a")
lettergroup.add_argument("-b", action="store_const", dest="letter", const="b")
lettergroup.add_argument("-c", action="store_const", dest="letter", const="c")
lettergroup.add_argument("-d", action="store_const", dest="letter", const="d")
args = parser.parse_args()
Now, the value is stored in args.letter
. If no flag is called, args.letter
will have the value a
. If two flags are called at the same time, the parser will throw an error.
现在,该值存储在args.letter中。如果没有调用标志,args.letter将具有值a。如果同时调用两个标志,则解析器将抛出错误。
Just another way to solve this problem.
只是解决这个问题的另一种方法。
#1
5
yes using the default values will set a to True even other arguments are specified. This will violate your second requirement, following is a simple fix with a naive condition checking.
是的,使用默认值将a设置为True,甚至指定其他参数。这将违反您的第二个要求,以下是一个简单的修复与天真的条件检查。
parser = argparse.ArgumentParser(description="Will take arguments... or none")
parser.add_argument("-a", action="store_true")
parser.add_argument("-b", action="store_true")
parser.add_argument("-c", action="store_true")
parser.add_argument("-d", action="store_true")
args = parser.parse_args()
if not (args.b or args.c or args.d):
args.a=True
print args
#2
1
Sounds like you want a 'radio button' effect - choosing just one of several alternatives. An alternative to a set of flags would be an argument with choices.
听起来你想要一个“单选按钮”效果 - 只选择其中一种选择。一组标志的替代方案将是具有选择的参数。
parser.add_argument('flag', choices=['a','b','c','d'], default='a', nargs='?')
You can check the result in args.flag
, which will be one of 4 strings.
您可以在args.flag中检查结果,它将是4个字符串之一。
Obviously the positional argument couple replaced by a flag, e.g. -f
.
显然,位置参数被标志所取代,例如-F。
#3
1
I went for the following solution:
我去了以下解决方案:
parser = argparse.ArgumentParser(description="Will take arguments... or none")
lettergroup = parser.add_mutually_exclusive_group()
lettergroup.add_argument("-a", action="store_const", dest="letter", const="a", default="a")
lettergroup.add_argument("-b", action="store_const", dest="letter", const="b")
lettergroup.add_argument("-c", action="store_const", dest="letter", const="c")
lettergroup.add_argument("-d", action="store_const", dest="letter", const="d")
args = parser.parse_args()
Now, the value is stored in args.letter
. If no flag is called, args.letter
will have the value a
. If two flags are called at the same time, the parser will throw an error.
现在,该值存储在args.letter中。如果没有调用标志,args.letter将具有值a。如果同时调用两个标志,则解析器将抛出错误。
Just another way to solve this problem.
只是解决这个问题的另一种方法。