Friday, 15 July 2011

Parse command line arguments in a Ruby script -



Parse command line arguments in a Ruby script -

i want phone call ruby script command line, , pass in parameters key/value pairs.

command line call:

$ ruby my_script.rb --first_name=donald --last_name=knuth

my_script.rb:

puts args.first_name + args.last_name

what standard ruby way this? in other languages have utilize alternative parser. in ruby saw have argf.read, not seem work key/value pairs in example.

optionparser looks promising, can't tell if supports case.

based on reply @martincortez here's short one-off makes hash of key/value pairs, values must joined = sign. supports flag arguments without values:

args = hash[ argv.join(' ').scan(/--?([^=\s]+)(?:=(\s+))?/) ]

…or alternatively…

args = hash[ argv.flat_map{|s| s.scan(/--?([^=\s]+)(?:=(\s+))?/) } ]

called -x=foo -h --jim=jam returns {"x"=>"foo", "h"=>nil, "jim"=>"jam"} can things like:

puts args['jim'] if args.key?('h') #=> jam

while there multiple libraries handle this—including getoptlong included ruby—i prefer roll own. here's pattern use, makes reasonably generic, not tied specific usage format, , flexible plenty allow intermixed flags, options, , required arguments in various orders:

usage = <<endusage usage: docubot [-h] [-v] [create [-s shell] [-f]] directory [-w writer] [-o output_file] [-n] [-l log_file] endusage help = <<endhelp -h, --help show help. -v, --version show version number (#{docubot::version}). create create starter directory filled illustration files; copies template easy modification, if desired. -s, --shell shell re-create from. available shells: #{docubot::shells.join(', ')} -f, --force forcefulness create on existing directory, deleting existing files. -w, --writer output type create [defaults 'chm'] available writers: #{docubot::writer::installed_writers.join(', ')} -o, --output file or folder (depending on writer) create. [default value depends on author chosen.] -n, --nopreview disable automatic preview of .chm. -l, --logfile specify filename log to. endhelp args = { :shell=>'default', :writer=>'chm' } # setting default values unflagged_args = [ :directory ] # bare arguments (no flag) next_arg = unflagged_args.first argv.each |arg| case arg when '-h','--help' args[:help] = true when 'create' args[:create] = true when '-f','--force' args[:force] = true when '-n','--nopreview' args[:nopreview] = true when '-v','--version' args[:version] = true when '-s','--shell' next_arg = :shell when '-w','--writer' next_arg = :writer when '-o','--output' next_arg = :output when '-l','--logfile' next_arg = :logfile else if next_arg args[next_arg] = arg unflagged_args.delete( next_arg ) end next_arg = unflagged_args.first end end puts "docubot v#{docubot::version}" if args[:version] if args[:help] or !args[:directory] puts usage unless args[:version] puts help if args[:help] exit end if args[:logfile] $stdout.reopen( args[:logfile], "w" ) $stdout.sync = true $stderr.reopen( $stdout ) end # etc.

ruby command-line

No comments:

Post a Comment