Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将多个命令行参数的值放入列表中_Python_Parsing_Python 3.x_Command Line_Argparse - Fatal编程技术网

Python 如何将多个命令行参数的值放入列表中

Python 如何将多个命令行参数的值放入列表中,python,parsing,python-3.x,command-line,argparse,Python,Parsing,Python 3.x,Command Line,Argparse,我有一个clibuild.py我称之为build.py-t-c-f 当我解析命令行参数时,是否有一种内置的方法来获取 ['t' = true,'c'=true,'f'=true,'s'=false] 下面是定义,不确定要在dest中更改什么(添加[…]不起作用。只是为了说明我试图实现的目标 from argparse import ArgumentParser from argparse import RawDescriptionHelpFormatter parser = Argument

我有一个cli
build.py
我称之为
build.py-t-c-f

当我解析命令行参数时,是否有一种内置的方法来获取

['t' = true,'c'=true,'f'=true,'s'=false]
下面是定义,不确定要在
dest
中更改什么(添加
[…]
不起作用。只是为了说明我试图实现的目标

from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter

parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter)

parser.add_argument("-s","--stored_proc", dest="build_what['s']", action="store_true", help="build all stored procedures, or the folder/*.sql specified")
parser.add_argument("-t","--triggers", dest="build_what['t']", action="store_true", help="build all triggers, or the folder/*.sql specified")
parser.add_argument("-f","--functions", dest="build_what['f']", action="store_true", help="build all functions, or the folder/*.sql specified")
parser.add_argument("-c","--scripts", dest="build_what['c']", action="store_true", help="run all scripts, or the folder/*.sql specified")

参数解析器的
parse_args
方法返回一个。您可以直接作为属性访问您的值:

args = parser.parse_args()
args.stored_proc # or `args.s` if you set `dest` to `'s'`
如果需要访问字典(无论出于何种原因),可以使用转换:

>>> parser.parse_args(['-s', '-f'])
Namespace(c=False, f=True, s=True, t=False)
>>> vars(_)
{'f': True, 'c': False, 't': False, 's': True}
请注意,该字典将包含所有注册的参数,而不仅仅是这四个。因此,如果您需要一个正好包含这四个值的字典,最好明确地创建它:

{'f': args.f, 'c': args.c, 't': args.t, 's': args.s}

您是否使用了
argparse
?修改我使用的是ArgumentParser,来自argparse导入ArgumentParser从argparse导入RawDescriptionHelpFormatter
['t'=true,'c'=true,'s'=false]
不是Python列表。您的意思是字典(
{'t':true,'c':true,'f':true,'s':false}
)?嗯……对于这个特定的任务,无论它是列表还是字典,我都不在乎。如果你想在
名称空间中使用短名称,请使用
dest='s'
。如果没有
dest
值,它将使用
'storaged\u proc'