本文介绍了获取/解析传递给Python脚本的命令行参数的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于解析Python命令行参数的最简单,最简单,最灵活的方法或库是什么?

解决方案

这个答案建议 optparse 这适用于较旧的Python版本。对于Python 2.7及以上版本, argparse 替换 optparse 。有关详情,请参见。



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



optparse,而有点



以下是一个典型的向解析器添加选项的行:

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

在处理时,它将接受-q或--query作为选项,将该参数存储在名为query的属性中,如果未指定,则具有默认值。它也是自我记录,因为你声明的帮助参数(当使用-h / - help运行时)使用选项。



通常你解析你的参数:

 选项,args = parser.parse_args()

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



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



只需执行

$ b就可以创建一个解析器
$ b

  parser = optparse.OptionParser()


$ b b

这些都是你需要的基础。这是一个完整的Python脚本,显示这一点:

  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行python,显示基本信息。



py,并用

运行一次

  python sample.py 

一次与

  python sample.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

这篇关于获取/解析传递给Python脚本的命令行参数的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 18:21