我有一个简单的 argparse 脚本,它有两个参数; --编码
和--解码
。如果没有给出参数,我想将 --decode
设置为默认值。我该怎么做?
我想要这个:
myscript.py --decode "some encoded string here"
当我这样做时发生:
myscript.py "some encoded string here"
默认。
最佳答案
查看 python documentation 上的 'store_true' 操作
,甚至是 default keyword on the add argument method
您需要实现一些逻辑,但这里的想法是:
parser.add_argument('--decode', rest_of_options..., default=True)
parser.add_argument('--encode', rest_of_options..., default=False)
values = parser.parse_args()
if values.decode:
do_some_stuff
elif values.encode:
do_some_other_stuff
关于python - 如果没有传递参数,如何使参数默认?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992679/