解析命令行参数的最佳方法是什么? [英] What's the best way to parse command line arguments?

查看:55
本文介绍了解析命令行参数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析Python命令行参数的最简单最有趣和最灵活的方法或库是什么?

解决方案

此答案建议 optparse 适用于较早的Python版本。对于Python 2.7及更高版本, argparse 会替换 optparse 。有关更多信息,请参见此答案



正如其他人指出的那样,使用optparse而不是getopt更好。 getopt几乎是标准getopt(3)C库函数的一对一映射,并且不太容易使用。



optparse,虽然有点



这是向解析器添加选项的典型行:

  parser.add_option('-q','--query',
action = store,dest = query,
help =查询字符串,默认为垃圾邮件)

这几乎可以说明一切;在处理时,它将接受-q或--query作为选项,将参数存储在名为query的属性中,如果未指定则具有默认值。这也是自记录的,您可以在该选项附近声明help参数(在与-h /-help一起运行时使用)。



通常您可以使用以下参数解析参数:

 选项,args = parser.parse_args()

默认情况下,这将解析传递给脚本的标准参数(sys.argv [1:])



options.query将被设置为您传递给脚本的值。



您只需执行

$ b即可创建解析器
$ b

  parser = optparse.OptionParser()

这些都是您需要的所有基础知识。这是显示此内容的完整Python脚本:

  import optparse 

parser = optparse.OptionParser()

parser.add_option('-q','--query',
action = store,dest = query,
help =查询字符串, default = spam)

选项,args = parser.parse_args()

打印查询字符串:,options.query

5行Python,向您展示基本知识。



将其保存在示例中。 py,然后使用

  python sample.py 


并使用

  python一次。py--query myquery 

除此之外,您还会发现optparse非常容易扩展。
在我的一个项目中,我创建了一个Command类,该类使您可以轻松地将子命令嵌套在命令树中。它大量使用optparse将命令链接在一起。这不是我可以在几行中轻松解释的内容,但是可以随时在我的存储库中浏览主类,以及使用它和选项解析器的类


What's the easiest, tersest, and most flexible method or library for parsing Python command line arguments?

解决方案

This answer suggests optparse which is appropriate for older Python versions. For Python 2.7 and above, argparse replaces optparse. See this answer for more information.

As other people pointed out, you are better off going with optparse over getopt. getopt is pretty much a one-to-one mapping of the standard getopt(3) C library functions, and not very easy to use.

optparse, while being a bit more verbose, is much better structured and simpler to extend later on.

Here's a typical line to add an option to your parser:

parser.add_option('-q', '--query',
            action="store", dest="query",
            help="query string", default="spam")

It pretty much speaks for itself; at processing time, it will accept -q or --query as options, store the argument in an attribute called query and has a default value if you don't specify it. It is also self-documenting in that you declare the help argument (which will be used when run with -h/--help) right there with the option.

Usually you parse your arguments with:

options, args = parser.parse_args()

This will, by default, parse the standard arguments passed to the script (sys.argv[1:])

options.query will then be set to the value you passed to the script.

You create a parser simply by doing

parser = optparse.OptionParser()

These are all the basics you need. Here's a complete Python script that shows this:

import optparse

parser = optparse.OptionParser()

parser.add_option('-q', '--query',
    action="store", dest="query",
    help="query string", default="spam")

options, args = parser.parse_args()

print 'Query string:', options.query

5 lines of python that show you the basics.

Save it in sample.py, and run it once with

python sample.py

and once with

python sample.py --query myquery

Beyond that, you will find that optparse is very easy to extend. In one of my projects, I created a Command class which allows you to nest subcommands in a command tree easily. It uses optparse heavily to chain commands together. It's not something I can easily explain in a few lines, but feel free to browse around in my repository for the main class, as well as a class that uses it and the option parser

这篇关于解析命令行参数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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