使用 argparse 输出来调用函数
目前我的代码如下所示。它允许我解析我的程序脚本获取的多个参数。有没有更接近“最佳实践”的不同方法?我还没有看到实际使用 argparse 输出的代码,只看到了如何设置它。
def useArguments():
x = 0
while x <= 5:
if x == 0:
if args.getweather != None:
getWeather(args.getweather)
if x == 1:
if args.post != None:
post(args.post)
if x == 2:
if args.custompost != None:
custompost(args.custompost)
if x == 3:
if args.list != None:
listAccounts(args.list)
if x == 4:
if args.add != None:
addAccount(args.add[0])
if x == 5:
if args.edit != None:
editAccount(args.edit[0])
x = x + 1
if __name__ == '__main__':
updateConfig()
parser = argparse.ArgumentParser(description='Post Yahoo weather to Twitter.', epilog="Report any bugs to [email protected]", prog='Program')
parser.add_argument('-a', '--add', nargs=1, help='Add a new account. Use the desired account name as an argument.')
parser.add_argument('-e', '--edit', nargs=1, choices=accountListSTR[:-1], help='Edit an account. Use the desired account name as an argument.')
parser.add_argument('-g', '--getweather', nargs='*', choices=accountListSTR, help='Get weather and post here. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-p', '--post', nargs='*', choices=accountListSTR, help='Post weather to Twitter. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-c', '--custompost', nargs=2, help='Post a custom message. Specify an account then type the message. Make sure you use "" around the message. Use "all" for all accounts.')
parser.add_argument('-l', '--list', action='store_const', const='all', help='List all accounts.')
parser.add_argument('--version', action='version', version='%(prog)s 0.3.3')
args = parser.parse_args()
useArguments()
Currently my code looks like this. It allows me to parse multiple parameters my program script gets. Is there a different way that is closer to 'best practices'? I haven't seen code actually using the output of argparse
, only how to set it up.
def useArguments():
x = 0
while x <= 5:
if x == 0:
if args.getweather != None:
getWeather(args.getweather)
if x == 1:
if args.post != None:
post(args.post)
if x == 2:
if args.custompost != None:
custompost(args.custompost)
if x == 3:
if args.list != None:
listAccounts(args.list)
if x == 4:
if args.add != None:
addAccount(args.add[0])
if x == 5:
if args.edit != None:
editAccount(args.edit[0])
x = x + 1
if __name__ == '__main__':
updateConfig()
parser = argparse.ArgumentParser(description='Post Yahoo weather to Twitter.', epilog="Report any bugs to [email protected]", prog='Program')
parser.add_argument('-a', '--add', nargs=1, help='Add a new account. Use the desired account name as an argument.')
parser.add_argument('-e', '--edit', nargs=1, choices=accountListSTR[:-1], help='Edit an account. Use the desired account name as an argument.')
parser.add_argument('-g', '--getweather', nargs='*', choices=accountListSTR, help='Get weather and post here. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-p', '--post', nargs='*', choices=accountListSTR, help='Post weather to Twitter. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-c', '--custompost', nargs=2, help='Post a custom message. Specify an account then type the message. Make sure you use "" around the message. Use "all" for all accounts.')
parser.add_argument('-l', '--list', action='store_const', const='all', help='List all accounts.')
parser.add_argument('--version', action='version', version='%(prog)s 0.3.3')
args = parser.parse_args()
useArguments()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为参数提供自定义 action ,我引用:
You could supply a custom action for an argument by, and I quote:
请参阅 http://docs.python.org/library/argparse.html#sub-命令:
简而言之:
这里我们为命令
get-weather
创建一个子解析器,并将函数get_weather
分配给它。请注意,文档说关键字/属性被命名为
func
但从 argparse 1.1 开始它绝对是function
。生成的代码有点太冗长了,所以我发布了一个小包 "argh" ,使得事情更简单,例如:
“Argh”可以做更多事情,但我会让堆栈溢出来回答这个问题。 :-)
See http://docs.python.org/library/argparse.html#sub-commands:
In a nutshell:
Here we create a subparser for the command
get-weather
and assign the functionget_weather
to it.Note that the documentation says that the keyword/attribute is named
func
but it's definitelyfunction
as of argparse 1.1.The resulting code is a bit too wordy so I've published a small package "argh" that makes things simpler, e.g.:
"Argh" can do more but I'll let stack overflow answer that. :-)
除了
--version
(这是一个非常常见的选项)之外,您提供的操作最好被视为“子命令”。我不知道 argparse 的具体细节,因为我还没有尝试过 Python 2.7,但您可以看一下 svn 命令作为示例,这里有一些命令行的伪代码
: >in:
是特定于该命令的选项。使用 optparse (类似),这意味着在调用parse_args
时,您的命令将在args
中返回,允许您执行如下操作:我发现此模式对于调试特别有用,我可以从命令行提供对内部函数的访问,并传递各种参数进行测试。根据您自己的项目调整命令处理程序的选择。
With the exception of
--version
, which is very commonly an option, the actions you've provided are better off treated as "subcommands".I'm unaware of the argparse specifics, as I have yet to try Python 2.7, but you might take a look at the
svn
command as an example, here's some pseudocode for the command line:Where
<command>
in:And
<command opts>
are options specific to that command. Using optparse (which is similar), this would mean that your command would be returned inargs
, when callingparse_args
, allowing you to do something like this:I find this pattern particularly useful for debugging, where I'd provide access to internal functions from the command line, and pass various arguments for testing. Adjust the selection of the command handler as appropriate for your own project.