开发者

Named command line arguments in Ruby

开发者 https://www.devze.com 2023-01-31 05:35 出处:网络
Is there a way to pass named arguments to a Ruby script? I u开发者_Go百科nderstand the ARGV method of passing parameters, but this requires them to be in a certain order. What I\'d like to do is pass

Is there a way to pass named arguments to a Ruby script?

I u开发者_Go百科nderstand the ARGV method of passing parameters, but this requires them to be in a certain order. What I'd like to do is pass named arguments, similar to other command-line operations. For instance:

$ ruby someRubyScript.rb -a argumentA -b argumentB

Any thoughts?


There are a couple options.

  • OptionParser, in the Standard Library, is one of the most popular. It can do exactly what you want, and the API is nice.

  • GetOptLong is also in the Standard Library, and it reimplements POSIX style command lines. If you want to emulate a Unix command line application, this can do it all.

  • Ara T. Howard's Main is a nifty gem for creating command-line scripts. It goes beyond parsing arguments and creates automatic usage and help prompts, all with a nice DSL for specifying the command line options.

2014 Update

A couple new gems have risen to popularity:

  • Slop provides a fantastically simple API which minimizes the amount of code you would have to write using OptionParser.

  • Highline is not technically a command-line argument parser but instead a way to prompt users for data, complete with validations. This can be combined with one of the above to provide a full interactive CLI.


You can use OptionParser to easily perform some args parsing.

require 'optparse'

hash_options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: your_app [options]"
  opts.on('-a [ARG]', '--argument_a [ARG]', "Specify the argument_a") do |v|
    hash_options[:argument_a] = v
  end
  opts.on('-b [ARG]', '--argument_b [ARG]', "Specify the argument_b") do |v|
    hash_options[:argument_b] = v
  end
  opts.on('--version', 'Display the version') do 
    puts "VERSION"
    exit
  end
  opts.on('-h', '--help', 'Display this help') do 
    puts opts
    exit
  end
end.parse!

Then your application will need to be launch as :

ruby application -a=12 -b=42 or
ruby application --argument_a=12 --argument_b=42

Here is the Documentation :

http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/optparse/rdoc/classes/OptionParser.html


The Ruby standard library comes with GetOptLong which should do what you want.

GetoptLong allows for POSIX-style options like —file as well as single letter options like -f


Trollop has not been mentioned yet: featured, declarative and compact. Although it's obviously usable as a gem, you can always copy it to your project since it's a (relatively small) single file.

require 'trollop'
opts = Trollop::options do
  opt :monkey, "Use monkey mode"                     # flag --monkey, default false
  opt :goat, "Use goat mode", :default => true       # flag --goat, default true
  opt :num_limbs, "Number of limbs", :default => 4   # integer --num-limbs <i>, default to 4
  opt :num_thumbs, "Number of thumbs", :type => :int # integer --num-thumbs <i>, default nil
end
#=> {:monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil}


For a serious CLI application you can use the gem thor available at https://github.com/wycats/thor

0

精彩评论

暂无评论...
验证码 换一张
取 消