我有一个需要一些命令行输入的python脚本,我使用argparse来解析它们.我发现文档有点令人困惑,无法找到检查输入参数格式的方法.通过此示例脚本解释了检查格式的含义:
parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD ", required=True)
parser.add_argument('-e', "--enddate", help="The End Date format YYYY-MM-DD (Inclusive)", required=True)
parser.add_argument('-a', "--accountid", type=int, help='Account ID for the account for which data is required (Default: 570)')
parser.add_argument('-o', "--outputpath", help='Directory where output needs to be stored (Default: ' + os.path.dirname(os.path.abspath(__file__)))
Run Code Online (Sandbox Code Playgroud)
我需要检查选项,-s并且-e用户输入的格式是YYYY-MM-DD.在argparse中是否有一个选项,我不知道哪个完成了这个.
我想检查用户是否设置了可选的argparse参数.
我可以使用isset安全地检查吗?
像这样的东西:
if(isset(args.myArg)):
#do something
else:
#do something else
Run Code Online (Sandbox Code Playgroud)
对于float/int/string类型参数,这是否相同?
我可以设置一个默认参数并检查它(例如,为字符串设置myArg = -1或"",或"NOT_SET").但是,我最终想要使用的值仅在稍后的脚本中计算.因此我将其设置为-1作为默认值,然后将其更新为其他内容.与简单地检查该值是否由用户设置相比,这看起来有点笨拙.
我有如下要求:
./xyifier --prox --lport lport --rport rport
Run Code Online (Sandbox Code Playgroud)
对于参数prox,我使用action ='store_true'来检查它是否存在.我不要求任何论据.但是,如果设置了--prox,我也需要 rport和lport.有没有一种简单的方法可以使用argparse执行此操作而无需编写自定义条件编码.
更多代码:
non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy')
non_int.add_argument('--lport', type=int, help='Listen Port.')
non_int.add_argument('--rport', type=int, help='Proxy port.')
Run Code Online (Sandbox Code Playgroud) parser.add_argument('-auto', action='store_true')
Run Code Online (Sandbox Code Playgroud)
如果-auto未指定,我如何存储假?我可以隐约记得这样,如果没有指定,它会存储None
是否可以要求argparse参数是少数预设值之一?
我目前的方法是手动检查参数,如果它不是允许值调用print_help()和退出之一.
这是当前的实现:
...
parser.add_argument('--val',
help='Special testing value')
args = parser.parse_args(sys.argv[1:])
if args.val not in ['a','b','c']:
parser.print_help()
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
这不是特别困难,而是看起来很混乱.
我已经使用argparse了Python程序,可以-process,-upload或两者:
parser = argparse.ArgumentParser(description='Log archiver arguments.')
parser.add_argument('-process', action='store_true')
parser.add_argument('-upload', action='store_true')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
没有至少一个参数,该程序毫无意义.如何配置argparse强制选择至少一个参数?
更新:
评论之后:使用至少一个选项参数化程序的Pythonic方法是什么?
我需要的是:
pro [-a xxx | [-b yyy -c zzz]]
Run Code Online (Sandbox Code Playgroud)
我尝试了这个但是没有用.有人可以帮帮我吗?
group= parser.add_argument_group('Model 2')
group_ex = group.add_mutually_exclusive_group()
group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")
group_ex_2 = group_ex.add_argument_group("option 2")
group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")
group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")
Run Code Online (Sandbox Code Playgroud)
谢谢!
Python2.7 argparse只接受互斥组中的可选参数(前缀):
parser = argparse.ArgumentParser(prog='mydaemon')
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon')
action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon')
action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon')
Run Code Online (Sandbox Code Playgroud)
$ mydaemon -h
usage: mydaemon [-h] (--start | --stop | --restart)
optional arguments:
-h, --help show this help message and exit
--start Starts mydaemon daemon
--stop Stops mydaemon daemon
--restart Restarts mydaemon daemon
Run Code Online (Sandbox Code Playgroud)
有没有办法让argparse参数表现得像传统的unix守护进程控件:
(start | stop | restart) and not (--start | --stop | --restart) ?
Run Code Online (Sandbox Code Playgroud) 我正在实现一个命令行程序,其界面如下:
cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...]
Run Code Online (Sandbox Code Playgroud)
我已经浏览了argparse文档.我可以GLOBAL_OPTIONS使用add_argumentin 实现可选参数argparse.并{command [COMMAND_OPTS]}使用子命令.
从文档中看来,我只能有一个子命令.但是你可以看到我必须实现一个或多个子命令.解析使用这些命令行参数的最佳方法是什么argparse?
我正在尝试使用argh库将参数列表传递给python脚本.可以采取以下输入的东西:
./my_script.py my-func --argA blah --argB 1 2 3 4
./my_script.py my-func --argA blah --argB 1
./my_script.py my-func --argA blah --argB
Run Code Online (Sandbox Code Playgroud)
我的内部代码如下所示:
import argh
@argh.arg('--argA', default="bleh", help='My first arg')
@argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!')
def my_func(args):
"A function that does something"
print args.argA
print args.argB
for b in args.argB:
print int(b)*int(b) #Print the square of each number in the list
print sum([int(b) for b in args.argB]) #Print the sum of the list
p = argh.ArghParser()
p.add_commands([my_func])
p.dispatch()
Run Code Online (Sandbox Code Playgroud)
这是它的行为方式: …