optarg
optarg
Yet another Crystal library for parsing command-line options and arguments.
optarg is good enough for parsing options. However there's no feature for formatting help, subcommands... etc. If you prefer a more feature-rich library, try cli.
Installation
Add this to your application's shard.yml
:
dependencies:
optarg:
github: mosop/optarg
Features
Accessor
class Model < Optarg::Model
string "--foo"
end
result = Model.parse(%w(--foo bar))
result.foo # => "bar"
Nilable Accessor
class Model < Optarg::Model
string "--foo"
end
result = Model.parse(%w())
result.foo? # => nil
result.foo # raises KeyError
Synonyms
class Model < Optarg::Model
string %w(-f --file)
end
result = Model.parse(%w(-f foo.cr))
result.f # => "foo.cr"
result.file # => "foo.cr"
Boolean
class Model < Optarg::Model
bool "-b"
end
result = Model.parse(%w(-b))
result.b? # => true
Array
class Model < Optarg::Model
array "-e"
end
result = Model.parse(%w(-e foo -e bar -e baz))
result.e # => ["foo", "bar", "baz"]
Concatenation
class Model < Optarg::Model
bool "-a"
bool "-b"
end
result = Model.parse(%w(-ab))
result.a? # => true
result.b? # => true
Default Value
class Model < Optarg::Model
string "-s", default: "string"
bool "-b", default: false
array "-a", default: %w(1 2 3)
end
result = Model.parse(%w())
result.s # => "string"
result.b? # => false
result.a # => ["1", "2", "3"]
Negation
class Model < Optarg::Model
bool "-b", default: true, not: "-B"
end
result = Model.parse(%w(-B))
result.b? # => false
Arguments
class Model < Optarg::Model
string "-s"
bool "-b"
end
result = Model.parse(%w(foo -s string bar -b baz))
result.args # => ["foo", "bar", "baz"]
Named Argument
class Model < Optarg::Model
arg "src_dir"
arg "build_dir"
end
result = Model.parse(%w(/path/to/src /path/to/build and more))
result.args.src_dir # => "/path/to/src"
result.args.build_dir # => "/path/to/build"
result.args # => ["/path/to/src", "/path/to/build", "and", "more"]
result.args.named # => {"src_dir" => "/path/to/src", "build_dir" => "/path/to/build"}
result.args.nameless # => ["and", "more"]
Inheritance (Reusable Model)
class Animal < Optarg::Model
bool "--sleep"
end
class Cat < Animal
bool "--mew"
end
class Dog < Animal
bool "--woof"
end
Cat.parse(%w()).responds_to?(:sleep?) # => true
Dog.parse(%w()).responds_to?(:sleep?) # => true
Handler
class Model < Optarg::Model
on("--goodbye") { goodbye! }
def goodbye!
raise "Goodbye, world!"
end
end
Model.parse %w(--goodbye) # raises "Goodbye, world!"
Required Arguments and Options
class Profile < Optarg::Model
string "--birthday", required: true
end
Profile.parse %w() # raises a RequiredArgumentError exception.
class Compile < Optarg::Model
arg "source_file", required: true
end
Compile.parse %w() # raises a RequiredOptionError exception.
Minimum Length of Array
class Multiply < Optarg::Model
array "-n", min: 2
def run
puts options.n.reduce{|n1, n2| n1 * n2}
end
end
Multiply.parse %w(-n 794) # raises a MinimumLengthError exception.
Custom Initializer
class The
def message
"Someday again!"
end
end
class Model < Optarg::Model
def initialize(argv, @the : The)
super argv
end
on("--goodbye") { raise @the.message }
end
Model.new(%w(--goodbye), The.new).parse # raises "Someday again!"
Stop and Termination
class Model < Optarg::Model
bool "-b", stop: true
end
result = Model.parse(%w(foo -b bar))
result.b? # => true
result.args # => ["foo"]
result.unparsed_args # => ["bar"]
class Model < Optarg::Model
terminator "--"
end
result = Model.parse(%w(foo -- bar))
result.args # => ["foo"]
result.unparsed_args # => ["bar"]
Usage
require "optarg"
and see Features.
Release Notes
- v0.3.0
- Stop and Termination
- (Breaking Change) "--" (double dash) is no longer a special argument by default. Use the
Model.terminator
method.
- v0.2.0
- (Breaking Change) Model#args separates arguments into named and nameless. #args itself returns both named and nameless arguments.
- v0.1.14
- Required Arguments and Options
- Minimum Length of Array
- v0.1.13
- Accessible Argument
- v0.1.12
- Array
- v0.1.9
- Concatenation
- v0.1.3
- Custom Initializer
- v0.1.2
- Synonyms
- v0.1.1
- Handler
Development
[WIP]
Contributing
- Fork it ( https://github.com/mosop/optarg/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- mosop - creator, maintainer