标签: optparse

帮助我为我的小脚本提供实地选择

我试图让我的脚本根据选项做不同的事情.但是......我根本不认识红宝石.我甚至不能告诉你阵列到底是什么.这是我得到的:

require 'optparse'
require 'pp'

# the options eventually get put here
options = {}

optparse = OptionParser.new do|opts|

# the help info
opts.banner = "Usage: script.rb [options] input-file output-file"

# This sets the default of 'flag' to 'false' and says it should be
# 'true' if the '-f' option is present
options[:flag] = false
  opts.on( '-f', '--flag', "Flag has been set" ) do
  options[:flag] = true
  end
end

optparse.parse!

# if no input-file or output-file is given, spit out …
Run Code Online (Sandbox Code Playgroud)

ruby optparse

1
推荐指数
1
解决办法
469
查看次数

如何修复opt parse optparse.OptionError:无效的长选项字符串'-submitted.cl':必须以 - 开头,然后是非破折号

我使用OptionParser有以下选项

parser = OptionParser()
parser.add_option("-submitted.cl", "--change_list", dest="change_list",help="Submitted Change list")
parser.add_option("-submitted.cr", "--crlist", dest="cr_list",help="Submitted CR list")
parser.add_option("-build.location", "--sbl", dest="sbl",help="Source build location")
parser.add_option("-filer.location", "--dbl", dest="dbl",help="Filer location")
parser.add_option("-users", "--users",dest="users",help="Users")
(options, args) = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

我正在使用以下选项运行脚本并遇到以下错误,请提供有关如何修复它的输入.

python save_build_artifacts.py 12345 02384 \\ben\cnss_dev_integration\nfc_builds\LA_host_builds\8084\Build2  \\ben\cnss_dev_integration\temp gnakkala
Run Code Online (Sandbox Code Playgroud)

错误:-

Traceback (most recent call last):
  File "save_build_artifacts.py", line 75, in <module>
    main()
  File "save_build_artifacts.py", line 43, in main
    parser.add_option("-submitted.cl", "--change_list", dest="change_list",help="Submitted Change list")
  File "C:\Python27\lib\optparse.py", line 1012, in add_option
    option = self.option_class(*args, **kwargs)
  File "C:\Python27\lib\optparse.py", line 566, in __init__
    self._set_opt_strings(opts)
  File "C:\Python27\lib\optparse.py", …
Run Code Online (Sandbox Code Playgroud)

python optparse

1
推荐指数
1
解决办法
4053
查看次数

如何将optparse选项与python中的变量名称集成

我是python和optparse模块的新手.我已经想出了如何在python脚本中添加选项,optparse但是在python 中将选项与我的变量名称链接起来时遇到了问题.

import sys
from optparse import OptionParser

def main ():
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="in_filename",
                      help="Input fasta file", metavar="FILE")
    parser.add_option("-o", "--out", dest="out_filename",
                      help="Output fasta file", metavar="FILE")
    parser.add_option("-i", "--id", dest="id",
                      help="Id name to change", metavar="ID")
    (options,args) = parser.parse_args()

    with open(f, 'r') as fh_in:
        with open(o, 'w') as fh_out:
            id = i
            result = {}
            count = 1
            for line in fh_in:
                line = line.strip()
                if line.startswith(">"):
                    line = line[1:]
                    result[line] = id + str(count)
                    count …
Run Code Online (Sandbox Code Playgroud)

python optparse

1
推荐指数
1
解决办法
1095
查看次数

Optparse和很多,如果没有

我正在开发一个接收一些参数的程序,并且想要使它们中的一些成为必需,但我正在介入一些问题:

  1. 我真的需要使代码Python 2.4.x兼容,所以我(至少认为)只能使用optparse
  2. 想避免代码模糊

这是我做的:

def usage():
    parser = OptionParser()
    parser.add_option('-i', '--item', dest='item')
    parser.add_option('-m', '--monitor', dest='monitor')
    parser.add_option('-s', '--service', dest='service')
    parser.add_option('-u', '--status', dest='status')
    parser.add_option('-a', '--match', dest='match')
    parser.add_option('-v', '--value', dest='value')
    parser.add_option('-o', '--hostname', dest='hostname', default='')
    parser.add_option('-t', '--test', action='store_true', dest='test')
    parser.add_option('-U', '--url', dest='URL', default='')
    parser.add_option('--verbose', action='store_true', dest='verbose')

    (options, args) = parser.parse_args()

    if not options.item or not options.monitor or not options.service or \
                           not options.status or not options.match or \
                           not options.value:
        parser.print_help()
        sys.exit(-1)

    return options
Run Code Online (Sandbox Code Playgroud)

我想这没关系,但我真的不认为这是Pythonic.有没有更好的方法来进行这种条件检查?

干杯,

python optparse

1
推荐指数
1
解决办法
73
查看次数

如何配置optparse以接受两个参数选项作为命令?

我正在使用Ruby的optparse库来解析我的命令行应用程序的选项,但我无法弄清楚如何接受命令.

它会是这样的:

commit -f -d init
Run Code Online (Sandbox Code Playgroud)

init在这种情况下将是命令.它并不总是必需的,因为如果用户没有提供任何默认命令,则应该运行该命令.

这是我现在的代码:

OptionParser.new do |opts|
  opts.banner  = %Q!Usage:
  pivotal_commit                                          # to commit with a currently started issue
  pivotal_commit -f                                       # to commit with a currently started issue and finish it
  pivotal_commit -d                                       # to commit with a currently started issue and deliver it
  pivotal_commit init -e "me@gmail.com" -p my_password -l #to generate a config file at the current directory!

  opts.on("-e", "--email [EMAIL]", String, "The email to the PT account you want …
Run Code Online (Sandbox Code Playgroud)

ruby command-line optparse

0
推荐指数
1
解决办法
972
查看次数

parser.parse_args()[0] 返回什么样的对象?

正如我从 python 文档(http://docs.python.org/3.3/library/optparse.html)收集的那样,在表达式中

(options, args) = parser.parse_args()

options是一个对象,其属性由parser设置,它是optparserOptionParser的实例。

选项所属的类的名称是什么?

python parsing optparse

0
推荐指数
1
解决办法
8764
查看次数

Ruby 简单帮助选项实现

Ruby 是否支持 POD 文档或任何替代方案?

我对此很感兴趣,因为我不知道如何提供一些简单的“-h”选项帮助。也许我在这里错了,请给我很好的例子。

我的例子:

$ ruby rubyScript.rb -h
This message is shown if you pass parameter -h.
Run Code Online (Sandbox Code Playgroud)

我对此的实施将是:

require 'optparse'

def HelpMessage()
  puts <<-__HELP__
  This message is shown if you pass parameter -h.
  __HELP__
end
OptionParser.new do |opts|
  opts.banner = "Usage: rubyScript.rb [options]"
  opts.on('-h', '--help', 'Help option') { HelpMessage(); exit()}
end.parse!
Run Code Online (Sandbox Code Playgroud)

我认为这是一个有点丑陋的代码,但我不知道另一种创建简单帮助的方法

ruby optparse command-line-arguments output

0
推荐指数
1
解决办法
305
查看次数