Parse command line arguments in a Ruby script -
I want to call the ruby script from the command line, and I want to pass the key / value pairs in the parameter.
Command line call:
$ ruby my_script.rb --first_name = donald --last_name = knuth
my_script .rb:
puts argsfirst_name + args.last_name
What is the standard Ruby approach to doing this? In other languages, I usually have to use a choice parser. In Ruby I have seen that we have ARGF.read
but it seems that the key / value pairs do not work like this example .
Looks promising, but I can not tell if it really supports this case. Based on the answer given by @ Martin Cortez, this is a small one which makes the key / A hash of the value pair, where the values should be combined with a =
sign it also 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 with- x = Foo -h - jim = jam
this {"x" => "foo", "h" => zero, "jim" => gt; jam "}
So you can work like this:
puts args ['jim'] if args.key? ('H') # = & gt; jam
< / Pre>
There are several libraries to handle this - in-I personally prefer to roll my roll, the method I use here is that it is reasonably normal , Is not tied to a specific use format, and intermissions for adequate flexibility. Flags, options, and required arguments in different orders:
USAGE = & lt; & Lt; Endusage usage: docubot [-h] [-v] [create-shell] [-f] directory [-w authors] [-o output_file] [-n] [-l log_file] help_help = & lt; & Lt; ENDHELP -h, --help Show this help -v, --version Show version number (# {DocuBot :: VERSION}). Create a starter directory filled with examples files; Also available for easy modification copy of template, if shell-shell available to copy from desired -s, --shell: # {DocuBot :: SHELLS.join (',')} -f, - Force delete an existing file Force to create an existing directory -w, --writer ['chm' for default] Output type for available authors: # {DocuBot :: Writer :: INSTALLED_WRITERS.join (',')} -o, --output file or folder (depending on To author) [Default value depends on the selected author.] -n, --nopreview Disable automatic preview of .chm. -l, --logfile Specify the filename to log in ENDHELP ARGS = {: shell = & gt; 'Default' ,: Author = & gt; 'Chm'} # Default values are set to UNFLAGGED_ARGS = [: directory] #blog logic (no flag) next_arg = UNFLAGGED_ARGS.first ARGV.each do | Arg | ARGS [: help] = 'ARGS [: made] = true when' -f ',' - force 'then ARGS [: force] =' when ',' - nopreview 'then ARGS [: nopreview] ] = True when '-v', '- version' then ARGS [: version] = true when '-s', '- shell' then_ next_arg =: when '-w', '- writer' then next_arg =: Author when '-o', '- output' then next_arg =: output when '-l', '--logfile' occurs next_arg =: logfile else if next_arg ARGS [next_arg] = ARG UNFLAGGED_ARGS.delete (next_arg) end next_arg = UNFLAGGED_ARGS.first puts the end end "DocuBot v # {DocuBot :: VERSION}" if ARGS [: version] if ARGS [: help] or! ARGS [: Directory] ARGS [: help] If you exit ARGS [: help] then ARGS [: logfile] $ stdout.reopen (ARGS [: logfile], "W") $ stdout until HELP Puts. Sync = true $ stderr.reopen ($ stdout) end # etc.
Comments
Post a Comment