2012-05-09 15 views
0

私はこれに2つのことを序説しましょう。Ruby OptionParser MissingArgumentを投げる

1)私はこの回答をStack OverflowとGoogleで検索しました。
2)今日はRubyを使いこなそうとした初日です。

よろしくお願いします。だから、私がやっていることは、Homebrewを使ってMySQL、PHP、Redis、xdebugを1つのコマンドでインストールできるRubyスクリプトを構築しようとしていることです。私は、上記を実行すると:しかし、私は「--with-Redisの不足している引数を」受け取る

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

:このコマンドは、次のようになります。このエラーは、 "--with-redis = 1.0"を使ってRedisのバージョンを強制するか、オプションを含まないと消えてしまいます。

これらのオプションは必須ではありません(はい、スクリプトではまだゼロオプションは処理されません)。

私は完全に困惑しています。私にそれを結びつけたいという明白な答えがあれば、それは問題ありません。私はRubyの知識が不足していることを前もってお詫びします。これは私がRubyに頭を躍らせる手助けとなるサイドプロジェクトです。あなたにも--with-mysqlのための不足している引数についての苦情を取得する必要があります

#!/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 

答えて

4

は、ここに私のスクリプトです。 fine manualから:

ロングスタイルスイッチ:

は、必須、オプション、または全く引数を取る長いスタイルのスイッチを指定します。これは次の形式の文字列です:

"--switch=MANDATORY" or "--switch MANDATORY" 
"--switch[=OPTIONAL]" 
"--switch" 

ですから、--with-mysql[=VERSION]と同様のものを使用したい:

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=1.0を言うならば、あなたが--with-redisoptions[:redis] = '1.0'を言うならば、あなたにoptions[:redis] = nilを与えるだろう。

+0

ありがとうございました!私はそれが何かばかげていなければならないことを知っていた。なぜ私はMySQLについて苦情を受けていないのだろうかと思います。 OptionParserのバグかもしれないと思いますか? –

+0

@AndrewEllis:難しいと言いますが、私はあなたのオプション解析コードを実行して苦情を受けます。 './script --with-mysql'(つまり、他のスイッチはありません)と言うと文句を言いますか? –