【发布时间】:2014-11-27 16:24:39
【问题描述】:
我有一个 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")
【问题讨论】:
-
你使用
argparse吗? -
已修改我正在使用 ArgumentParser,从 argparse import ArgumentParser from argparse import RawDescriptionHelpFormatter
-
['t' = true,'c'=true,'f'=true,'s'=false]不是 Python 列表。你是说字典 ({'t':true,'c':true,'f':true,'s':false})? -
嗯...对于我认为的这个特定任务来说它是一个列表还是字典对我来说并不重要
-
如果您想要在
Namespace中使用短名称,请使用dest='s'。如果没有dest值,它将使用'stored_proc'。
标签: python parsing python-3.x command-line argparse