Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何配置OptPass以接受两个参数选项作为命令?_Ruby_Command Line_Optparse - Fatal编程技术网

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

Ruby 如何配置OptPass以接受两个参数选项作为命令?,ruby,command-line,optparse,Ruby,Command Line,Optparse,我正在使用Ruby的optparse库解析命令行应用程序的选项,但我不知道如何同时接受命令 应该是这样的: commit -f -d init options = {} OptionParser.new do |opts| # definitions of command-line options... # ... end.parse! subcommand = ARGV.shift || "init" print "options: " p options puts "subcomma

我正在使用Ruby的
optparse
库解析命令行应用程序的选项,但我不知道如何同时接受命令

应该是这样的:

commit -f -d init
options = {}

OptionParser.new do |opts|
# definitions of command-line options...
# ...
end.parse!

subcommand = ARGV.shift || "init"

print "options: "
p options
puts "subcommand: #{subcommand}"
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 to access") do |v|
    options[:email] = v
  end

  opts.on("-p", "--password [PASSWORD]", String, "The password to the PT account you want to access") do |v|
    options[:password] = v
  end

  opts.on("-f", '--finish', 'Finish the story you were currently working on after commit') do |v|
    options[:finish] = v
  end

  opts.on('-d', '--deliver', 'Deliver the story you were currently working on after commit') do |v|
    options[:deliver] = v
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts
    exit
  end

  opts.on_tail('-v', '--version', 'Show version') do
    puts "pivotal_committer version: #{PivotalCommitter::VERSION}"
    exit
  end

end.parse!
调用
OptionParser\parse之后,命令行参数(不是选项)位于
ARGV
ARGV
中提取选项。 因此,可以得到如下子命令:

commit -f -d init
options = {}

OptionParser.new do |opts|
# definitions of command-line options...
# ...
end.parse!

subcommand = ARGV.shift || "init"

print "options: "
p options
puts "subcommand: #{subcommand}"
如果您有许多子命令,gem可能会帮助您

而且,尽管这不是您问题的答案,但选项定义中的括号([])表示该选项的参数是可选的。 例如,根据您的定义,即使传递了以下选项,电子邮件和密码也可能为零:

$ pivotal_commit -e
options: {:email=>nil}
subcommand: init
如果在传递选项时需要参数,请删除括号:

# ...
  opts.on("-e", "--email EMAIL", String, "The email to the PT account you want to access") do |v|
    options[:email] = v
  end
# ...
现在需要电子邮件的参数:

$ pivotal_commit -e
pivotal_commit:6:in `<main>': missing argument: -e (OptionParser::MissingArgument)
$pivotal\u提交-e
提交:6:in`:缺少参数:-e(OptionParser::MissingArgument)
调用
OptionParser\parse之后,命令行参数(不是选项)在
ARGV
ARGV
中提取选项。 因此,可以得到如下子命令:

commit -f -d init
options = {}

OptionParser.new do |opts|
# definitions of command-line options...
# ...
end.parse!

subcommand = ARGV.shift || "init"

print "options: "
p options
puts "subcommand: #{subcommand}"
如果您有许多子命令,gem可能会帮助您

而且,尽管这不是您问题的答案,但选项定义中的括号([])表示该选项的参数是可选的。 例如,根据您的定义,即使传递了以下选项,电子邮件和密码也可能为零:

$ pivotal_commit -e
options: {:email=>nil}
subcommand: init
如果在传递选项时需要参数,请删除括号:

# ...
  opts.on("-e", "--email EMAIL", String, "The email to the PT account you want to access") do |v|
    options[:email] = v
  end
# ...
现在需要电子邮件的参数:

$ pivotal_commit -e
pivotal_commit:6:in `<main>': missing argument: -e (OptionParser::MissingArgument)
$pivotal\u提交-e
提交:6:in`:缺少参数:-e(OptionParser::MissingArgument)