在Jython中使用optparse添加子选项

时间:2013-04-03 04:47:39

标签: java python jython optparse

我使用optparse为Jython脚本添加选项。他们就像

$script.py -clear
$script.py -start
$script.py -stop 

等等,现在我的问题是如何将子选项添加到任何特定选项,如

$script.py -start -name <name> -type <type>,

因为可以有很多选项,我无法通过捕获单个级别的所有选项来验证或提供帮助。我发现一些脚本有像

这样的选项
$script.py -start --help

- &GT;这将只提供可以提供给父“start”选项的选项。所以请帮助我如何实现这个

谢谢, 查克里

3 个答案:

答案 0 :(得分:0)

没有办法添加&#34; sub&#34;选项。通常,帮助选项由$script.py --help start之类的东西完成 - 其中start只是一个字符串选项,您可以阅读并显示适当的信息。

答案 1 :(得分:0)

我最近遇到了同样的问题。对我有用的解决方案如下:

  • 按照惯例,使用第一个参数来表示您想要的命令 执行(这将是您的startclearstop

  • 通过sys.argv[1]获取此命令(我知道这很难看 解决方法)

  • 创建多个optparse.OptionParser()(每个命令一个)

  • 仅调用相应parse_args()

  • OptionParser方法
  • 如果命令无效或缺失,请创建另一个选项解析器以显示用法

以下是示例代码:

# parse.py

import sys
import optparse

def parse_my_command_line():

    opt_parser = {}
    opt_parser['start'] = optparse.OptionParser()
    opt_parser['clear'] = optparse.OptionParser()
    opt_parser['stop']  = optparse.OptionParser()

    # Start
    opt_parser['start'].usage = 'Help for the START command'
    opt_parser['start'].add_option('--name', default = None, type = 'str',
        help = 'help for --name option', metavar = '<name>')
    opt_parser['start'].add_option('--type', default = None, type = 'str',
        help = 'help for --type option', metavar = '<type>')

    # Clear
    opt_parser['clear'].usage = 'Help for the CLEAR command'
    opt_parser['clear'].add_option('--foo', default = None, type = 'str',
        help = 'help for --foo option', metavar = '<foo>')

    # Stop
    opt_parser['stop'].usage = 'Help for the STOP command'
    opt_parser['stop'].add_option('--name', default = None, type = 'str',
        help = 'help for --name option', metavar = '<name>')
    opt_parser['stop'].add_option('--type', default = None, type = 'str',
        help = 'help for --type option', metavar = '<type>')

    # Get the command from sys.argv
    try:
        command = sys.argv[1]
    except IndexError:
        command = None

    # Get the appropriate option parser
    try:
        parser = opt_parser[command]
        # Done. Parse arguments and return.
        return parser.parse_args()
    except KeyError:
        # Invalid command. Create a parser to show default usage
        parser       = optparse.OptionParser()
        parser.usage  = '%prog [command] [options]\n\n'
        parser.usage += 'Description:\n'
        parser.usage += '  General help for %prog\n\n'
        parser.usage += 'Commands:\n'
        for cmd in ['start', 'clear', 'stop']:
            parser.usage += '  %s\n' % cmd
            parser.usage += '    %s\n\n' % opt_parser[cmd].usage
        parser.usage = parser.usage.strip()
        if command is None:
            parser.error('command cannot be empty')
        else:
            parser.error('invalid command: %s' % command)



def main():
    options, args = parse_my_command_line()
    print 'Args    = %s' % args
    print 'Options = %s' % options

if __name__ == '__main__':
    main()

这里有一些结果:

[usr@localhost ~]$ python parse.py
Usage: parse.py [command] [options]

Description:
  General help for parse.py

Commands:
  start
    Help for the START command

  clear
    Help for the CLEAR command

  stop
    Help for the STOP command

parse.py: error: command cannot be empty

[usr@localhost ~]$ python parse.py start
Args    = ['start']
Options = {'type': None, 'name': None}

[usr@localhost ~]$ python parse.py stop
Args    = ['stop']
Options = {'type': None, 'name': None}

[usr@localhost ~]$ python parse.py clear
Args    = ['clear']
Options = {'foo': None}

[usr@localhost ~]$ python parse.py start --help
Usage: Help for the START command

Options:
  -h, --help     show this help message and exit
  --name=<name>  help for --name option
  --type=<type>  help for --type option
[usr@localhost ~]$ python parse.py stop -- help
Args    = ['stop', 'help']
Options = {'type': None, 'name': None}

[usr@localhost ~]$ python parse.py clear --help
Usage: Help for the CLEAR command

Options:
  -h, --help   show this help message and exit
  --foo=<foo>  help for --foo option

答案 2 :(得分:0)

这正是OptionParser旨在处理的问题!

请考虑以下代码:

import sys
import os

from optparse import OptionParser

__all__ = []
__version__ = 0.1
__date__ = '2014-01-21'
__updated__ = '2014-01-21'


def main(argv=None):
    '''Command line options.'''

    program_name = os.path.basename(sys.argv[0])
    program_version = "v0.1"
    program_build_date = "%s" % __updated__

    program_version_string = '%%prog %s (%s)' % (program_version, program_build_date)

    program_longdesc = '''This will be displayed beneath your help information''' # optional - give further explanation about what the program does
    program_license = "Copyright 2014 Code Tester (Jython Dev Inc.)                                            \
                Licensed under the Apache License 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0"

    if argv is None:
        argv = sys.argv[1:]
    try:
        # setup option parser
        parser = OptionParser(version=program_version_string, epilog=program_longdesc, description=program_license)
        parser.add_option("-i", "--in", dest="infile", help="set input path [default: %default]", metavar="FILE")
        parser.add_option("-o", "--out", dest="outfile", help="set output path [default: %default]", metavar="FILE")
        parser.add_option("-d", "--debug", dest="modeDebug", action="store_true", help="Prints additional debug information")
        parser.add_option("-m", "--map", dest="listKVPs", action="append", help="Key value pair mappings (i.e. left=right)", metavar="KVP")
        parser.add_option("-r", "--role", dest="listProtectedRoles", action="append", help="Protected system roles to use (multiple allowed)", metavar="Role")
        parser.add_option("-u", "--user", dest="listProtectedUsers", action="append", help="Protected system users to be used (multiple allowed)", metavar="User")

        # set defaults
        parser.set_defaults(outfile="./out.txt", infile="./in.txt")

        # process options
        (opts, args) = parser.parse_args(argv)

        if opts.modeDebug:
            print("Running in DEBUG mode")
        if opts.infile:
            print("infile = %s" % opts.infile)
        if opts.outfile:
            print("outfile = %s" % opts.outfile)
        if opts.listKVPs:
            print("KVP's = %s" % opts.listKVPs)
        if opts.listProtectedRoles:
            print("Protected Roles = %s" % opts.listProtectedRoles)
        if opts.listProtectedUsers:
            print("Protected Users = %s" % opts.listProtectedUsers)

        '''
        Do something interesting with the parameters here!
        '''


    except Exception, e:
        indent = len(program_name) * " "
        sys.stderr.write(program_name + ": " + repr(e) + "\n")
        sys.stderr.write(indent + "  for help use --help")
        return 2


if __name__ == "__main__":
    sys.exit(main())

如果使用-h(或--help)运行它,则会得到以下结果:

Usage: OptParseExample.py [options]

Copyright 2014 Code Tester (Jython Dev Inc.)
Licensed under the Apache License 2.0
http://www.apache.org/licenses/LICENSE-2.0

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -i FILE, --in=FILE    set input path [default: ./in.txt]
  -o FILE, --out=FILE   set output path [default: ./out.txt]
  -d, --debug           Prints additional debug information
  -m KVP, --map=KVP     Key value pair mappings (i.e. left=right)
  -r Role, --role=Role  Protected system roles to use (multiple allowed)
  -u User, --user=User  Protected system users to be used (multiple allowed)

This will be displayed beneath your help information

现在,如果我们使用更多参数运行它,即:

testScript.py -d -i input.txt -o output.txt -m jython=cool -r Admins -r DomainAdmins

我们得到显示的输出:

Running in DEBUG mode
infile = input.txt
outfile = output.txt
KVP's = ['jython=cool']
Protected Roles = ['Admins', 'DomainAdmins']

请注意以下几点:

  • 命令行上的参数顺序并不重要(只要参数随关联标志一起提供)
  • 在这种情况下,-d选项用作“store_true”参数,以指示它是布尔标志
  • 其他参数均采用其他值
  • 如果带值的参数可以接受多个值,则可以将其声明为列表(使用追加操作)
  • 处理完所有参数后,您仍然可以通过'args'变量获得任何其他命令行参数。

有关详细信息,请查看http://jython.org/docs/library/optparse.html