argparser
Yet another ruby command line argument parser library.
Installation
gem install argparser
as usual.
Usage by example
Suppose there's a file named example.rb
like this:
require 'argparser'
args= ArgParser.new( # Here goes the manifest.
:program => 'example.rb', # Use additional properties like these:
:version => '1.0', # :info, :copyright, :license,
:options => [{ # :package, :bugs, :homepage
:names => %w[mode m],
:param => 'first|second|third',
:default => 'first',
:multiple => true,
:help => 'Example mode.',
:validate => (lambda {|this, parser| # Validating value in-line
possible = this.argument.split('|')
this.value.select{|v| possible.include?(v)}.size == this.value.size })
}],
:arguments => [{
:name => 'file',
:required => true,
:help => 'Filename or - for stdin.',
:validate => (lambda {|this, parser|
if this.value == '-'
this.reset.set_value($stdin.read)
else
parser.terminate(2, 'No such file') unless File.exists?(this.value)
this.reset.set_value(File.read(this.value))
end })
}]
).parse # Uses ARGV by default, you may supply your own arguments.
# It exits if bad arguments given or they aren't validated.
puts args['mode'].value.inspect # So we could use our options...
puts args['file'].value # Prints contents of a file
This file located here: lib/argparser/examples/example.rb
.
Now, let's look at the output of example given in various cases.
$ ruby example.rb
is unsufficient:
example.rb [-m, --mode first|second|third]... file
Expected argument: file
$ ruby example.rb --help
helps:
example.rb [-m, --mode first|second|third]... file
[-m, --mode first|second|third]...
Example mode.
[--help]
Print this help and exit.
[--version]
Print version and exit.
file
Filename or - for stdin.
$ echo "content" | ruby example.rb -
does the trick:
["first"]
content
$ echo "content" | ruby example.rb --mode fourth -
oopses:
example.rb [-m, --mode first|second|third]... file
Invalid option: m
$ echo "content" | ruby example.rb -abcm first -
:
example.rb [-m, --mode first|second|third]... file
Unknown option: a
Consider more rules
-
--help
and--version
options provided unless specified explicitly - printed synopsis provided unless specified explicitly
-
:default
setting assigns default option's value if value isn't specified -
--
argument honored -
ArgParser#parse
supports block(option_name, option_value) to read options/arguments supplied
Tests
bundle exec rake test
Documentation
This README is all i could say in a rush. No other documentation provided at this moment, see the sources.
If you've found a bug or drawback
Don't hesistate to leave a report.
License
MIT for now.