从Argparse中的parse.add_argument提取值-python [英] Extract values from parse.add_argument in Argparse - python

查看:82
本文介绍了从Argparse中的parse.add_argument提取值-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Argparse作为命令行实用程序执行的手段.我定义了各种参数(下面显示了其中的几个).我需要存储参数名称,帮助,并在各自的列中输入数据库.

I am using Argparse as a means to command line utility execution. I have various arguments defined ( couple of them shown below ). I have a requirement to store argument name, help, type in the database in their respective columns.

我不确定如何从每个parse.add_argument中提取这三个并将其保存在某个数组/列表中.如果您可以共享任何输入,那将很有帮助.

I am not sure how to extract these three from each parse.add_argument and save it in some array/list. If you can share any inputs, that would be helpful.

     parser.add_argument("num",help="The fibnocacci number to calculate:", type=int)  # how to take the strings on the command line and turn them into objects
     parser.add_argument("-f","--file",help="Output to the text file",action="store_true")

推荐答案

其他人认为您需要解析命令行的结果,即args命名空间中的值.但是我怀疑您想要由add_argument方法定义的Action对象

Others think you want the results of parsing the commandlline, the values from the args namespace. But I suspect you want the Action object defined by the add_argument method

在交互式shell中,我可以将解析器定义为:

In an interactive shell I can define a parser as:

In [207]: parser=argparse.ArgumentParser()

In [208]: arg1= parser.add_argument("num",help="The fibnocacci number to calculate:", type=int) 

In [209]: arg2=parser.add_argument("-f","--file",help="Output to the text file",action="store_true")

In [210]: arg1
Out[210]: _StoreAction(option_strings=[], dest='num', nargs=None, const=None, default=None, type=<type 'int'>, choices=None, help='The fibnocacci number to calculate:', metavar=None)

In [211]: arg2
Out[211]: _StoreTrueAction(option_strings=['-f', '--file'], dest='file', nargs=0, const=True, default=False, type=None, choices=None, help='Output to the text file', metavar=None)

In [212]: parser._actions
Out[212]: 
[_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
 _StoreAction(option_strings=[], dest='num', nargs=None, const=None, default=None, type=<type 'int'>, choices=None, help='The fibnocacci number to calculate:', metavar=None),
 _StoreTrueAction(option_strings=['-f', '--file'], dest='file', nargs=0, const=True, default=False, type=None, choices=None, help='Output to the text file', metavar=None)]

add_argument创建一个Action子类(基于action参数).您可以将其保存在自己的变量中,也可以在解析器的_actions列表中找到它.

add_argument creates an Action subclass (based on action parameter). You can save this in your own variable, or find it in the _actions list of the parser.

打印此内容将显示其某些属性,但是您可以检查甚至更改更多的属性.

Printing this shows some of its attributes, but you can examine, and even change more of them.

In [213]: arg1.help
Out[213]: 'The fibnocacci number to calculate:'

In [214]: arg1.type
Out[214]: int

In [215]: arg1.dest
Out[215]: 'num'

In [217]: vars(arg1)
Out[217]: 
{'choices': None,
 'const': None,
 'container': <argparse._ArgumentGroup at 0x8f0cd4c>,
 'default': None,
 'dest': 'num',
 'help': 'The fibnocacci number to calculate:',
 'metavar': None,
 'nargs': None,
 'option_strings': [],
 'required': True,
 'type': int}

许多人需要检查argparse.py文件中的类定义以了解这些属性.

You many need to examine the class definitions in the argparse.py file to understand these attributes.

这篇关于从Argparse中的parse.add_argument提取值-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆