首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby OptionParser抛出MissingArgument

Ruby OptionParser抛出MissingArgument
EN

Stack Overflow用户
提问于 2012-05-09 14:02:32
回答 1查看 3.1K关注 0票数 0

让我以两件事作为开始。

1)我已经在Stack Overflow和Google上搜索了这个答案。

2)今天是我第一次尝试和Ruby打交道。

好的,酷。因此,我要做的就是构建一个Ruby脚本,我可以使用它在Homebrew中通过一个命令安装MySQL、PHP、Redis和xdebug。此命令如下所示:

./phpserver.rb --with-php --with-mysql --with-xdebug --with-redis

然而,当我运行上面的命令时,我收到了"missing argument:--with-redis“。如果我通过"--with- Redis =1.0“强制redis的版本,或者如果我没有包含这个选项,这个错误就会消失。

这些选项都不是强制的(是的,我知道脚本还不能处理零选项)。

我完全被难住了,如果有一个明显的答案,你想链接到我,那是很好的。我为我缺乏Ruby知识而道歉,这是一个辅助项目,可以帮助我更好地了解Ruby。

下面是我的脚本:

代码语言:javascript
复制
#!/usr/bin/ruby
prefix = `brew --prefix`.gsub("\n", "");
$LOAD_PATH << prefix+"/Library/Homebrew/"

# Required scripts
require 'optparse'
require 'global'

# Handle Ctrl+C
trap('INT') {
  opoo "Installation failed. Please try again!"
  exit!
}

# Start big try statement
begin

# Options
options = {}
OptionParser.new do |opts|
  banner = "\nUsage: phpserver.rb [options]\n\n"

  opts.banner = banner
  opts.on("--with-mysql=VERSION", "Install MySQL with VERSION",
          "Run `brew versions mysql` to see available versions.") do |v|
    options[:mysql] = v
  end
  opts.on("--with-php", "Install PHP") do |v|
    options[:php] = v
  end
  opts.on("--with-xdebug", "Install xdebug for PHP debugging") do |v|
    options[:xdebug] = v
  end
  opts.on("--with-redis=VERSION", "Install Redis with VERSION",
          "Run `brew versions redis` to see available versions.") do |v|
    options[:redis] = v
  end
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    puts "\n"
    exit
  end
end.parse!

# Need to run brew update first?
ohai "Making sure Homebrew is up to do."
versions = `brew versions --compact a2ps`
if versions.include? "Please `brew install git` first"
  ohai "Need to install Git before the script can run; we'll do that for you."
  system 'brew install git'

  ohai "Now we need to pull down the Homebrew repository, we'll do that for you as well."
  system 'brew update'
elsif versions.include? "Please `brew update"
  ohai "Homebrew needs to be updated, we'll take care of that."
  system 'brew update'
end

# Tap them kegs!
if !File.directory?("#{prefix}/Library/Taps/josegonzalez-php")
  ohai "Tapping josegonzalez/homebrew-php"
  if system 'brew tap josegonzalez/homebrew-php'
    oh1 "Success!"
  else
    opoo "Failed to tap the keg. Please report this."
  end
else
  oh1 "The josegonzalez/homebrew-php keg has already been tapped. Continuing..."
end

# Installing MySQL?
if options[:mysql] != nil
  ohai "Installing MySQL."

  revert = false
  command = false
  if options[:mysql] != true
    versions = `brew versions mysql`

    errors = []
    versions.each do |version|
      if version.start_with? options[:mysql]
        revert = true
        command = version.gsub(options[:mysql], "").gsub(prefix+"/", "").strip!
        break
      else
        errors << "- "+version
      end
    end

    if command == false
      error = "Invalid MySQL version. The available versions for MySQL are:\n\n"
      error += errors.join()
      opoo error
    else
      %x(cd #{prefix} && #{command})
    end
  end

  system 'brew install mysql'

  if revert
    command = command.split(" ").last
    %x(cd #{prefix} && #{command})
  end
end

# Installing PHP?
if options[:php] != nil
  ohai "Installing PHP."

  install = 'brew install php';
  if options[:mysql] != nil
    install += ' --with-mysql';
  end
  if options[:php] == true or options[:php].to_f < 5.4
    install += ' --with-suhosin';
  end

  system install
end

# Installing xdebug?
if options[:xdebug] != nil
  ohai "Installing xdebug."
  system 'brew install xdebug-php'
end

# Installing Redis?
if options[:redis] != nil
  ohai "Installing Redis."
  system 'brew install redis'
end

rescue OptionParser::NeedlessArgument
  opoo "Invalid arguments. Run `./phpserver.php -h` for help."
rescue StandardError => message
  if !["exit"].include? message
    opoo message
  end
end 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-09 14:26:46

你也应该收到关于--with-mysql缺少参数的投诉。从fine manual

Long style开关:

指定采用强制参数、可选参数或无参数的长型开关。它是一个以下形式的字符串:

"--switch =强制“或”--switch强制“”--switch=可选“"--switch”

所以你想使用--with-mysql[=VERSION]和类似的东西:

代码语言:javascript
复制
opts.on("--with-mysql[=VERSION]", "Install MySQL with VERSION",
        "Run `brew versions mysql` to see available versions.") do |v|
  options[:mysql] = v
end
#...
opts.on("--with-redis[=VERSION]", "Install Redis with VERSION",
        "Run `brew versions redis` to see available versions.") do |v|
  options[:redis] = v
end

如果你说--with-redis就会得到options[:redis] = nil,如果你说--with-redis=1.0就会得到options[:redis] = '1.0'

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10510598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档