【发布时间】:2017-07-23 07:55:25
【问题描述】:
我知道从 argsparse 中获取值,例如:
parser.add_argument('--volume', help='Ejecuta una aplicacion cualquiera')
我也知道这个参数是真还是假:
parser.add_argument('--getVolume', action='store_true', help='Ejecuta una aplicacion cualquiera')
但我不知道有时会同时使用这两种方法,因为我想使用 getter-setter 之类的参数。例如,如果我不写数字,程序会返回当前音量,如果我写数字,应用程序会将此音量设置给机器人。
python app.py --volume # Return the current volume
python app.py --volume 80 # Set the volume to the 80%
非常感谢,
卡洛斯。
【问题讨论】:
-
除了链接之外,还可以使用这样的默认值,然后您可以决定是显示还是更新卷。
-
docs.python.org/3/library/argparse.html 看例子16.4.3.2