2016-10-09 4 views
0

ルビーのOptionsParserでtutorialを使っています。私はOptionParser#onは、例えば定義された型に値を変換できることを理解この場合、OptionsParserとは何ですか?

option_parser = OptionParser.new do |opt| 
. 
. 
. 
server_list = %w[a b c] 
opt.on("-s SERVERS","--servers SERVERS", server_list, "which server will start between #{server_list.join(',')}") do |servers| 
    options[:servers] = servers 
end 

:そこ私が混乱しているコードの行である

. 
. 
. 
opt.on("-e","--environment ENVIRONMENT",Numeric, "which environment you want server run") do |environment| 
    puts environment #=> Fixnum 
    options[:environment] = environment 
end 

は、しかし、私はすることができていません上記のようにOptionParserを使用するRubyプログラムをうまく実行する方法を理解してください。私が試したことについては以下を参照してください。それは選択肢の一つが、ab、またはcではないので

➜ ruby-cli cat 04-with_optparse_convert_values 
#!/usr/bin/env ruby 

require 'optparse' 

options = {} 

option_parser = OptionParser.new do |opt| 
    opt.banner = "Usage: cats" 

    opt.on("-e", "--environment ENVIRONMENT", Numeric, "environment to run") do |env| 
    puts "env.class" 
    p env.class 
    options[:environment] = env 
    end 

    opt.on("--delay N", Float, "Delay N seconds before executing") do |n| 
    options[:delay] = n 
    end 

    opt.on("-j x,y,z","--jurisdictions x,y,z", Array, 
    "which jurisdiction will start") do |jurisdictions| 
    options[:jurisdictions] = jurisdictions 
    end 

    server_list = %w[a b c] 
    opt.on("-s SERVERS","--servers SERVERS", server_list, 
    "which server will start between #{server_list.join(',')}") do |servers| 
    options[:servers] = servers 
    end 

end 

option_parser.parse! 
p options 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs 
{:jurisdictions=>["cats", "and", "dogs"]} 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs -s cats,and,dogs 
./04-with_optparse_convert_values:33:in `<main>': invalid argument: -s cats,and,dogs (OptionParser::InvalidArgument) 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs -s cats 
./04-with_optparse_convert_values:33:in `<main>': invalid argument: -s cats (OptionParser::InvalidArgument) 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs -s "cats,and,dogs" 
./04-with_optparse_convert_values:33:in `<main>': invalid argument: -s cats,and,dogs (OptionParser::InvalidArgument) 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs -s "cats\nand\ndogs" 
./04-with_optparse_convert_values:33:in `<main>': invalid argument: -s cats\nand\ndogs (OptionParser::InvalidArgument) 
➜ ruby-cli ARRAY=(cats and dogs) 
➜ ruby-cli ./04-with_optparse_convert_values -j cats,and,dogs -s ${ARRAY[\*]} 
./04-with_optparse_convert_values:33:in `<main>': invalid argument: -s cats (OptionParser::InvalidArgument) 
➜ ruby-cli 
+1

有効なサーバーを '[" a "、" b "、" c "]'と定義した場合、なぜ '-s cats'が適切だろうと思いますか? '-s a'は動作するはずです(' -s b'または '-s c')。 – Amadan

答えて

0
server_list = %w[a b c] 
opt.on("-s SERVERS","--servers SERVERS", server_list, 
"which server will start between #{server_list.join(',')}") do |servers| 
    options[:servers] = servers 
end 

./04-with_optparse_convert_values -s catsが働かないので、コマンドラインオプション-sを経由してから選択する「サーバ」の配列を提供しています意志。例:

➜ ruby-cli ./04-with_optparse_convert_values -s a 
{:servers=>"a"} 
➜ ruby-cli ./04-with_optparse_convert_values -s b 
{:servers=>"b"} 
➜ ruby-cli ./04-with_optparse_convert_values -s c 
{:servers=>"c"} 
関連する問題