-
Notifications
You must be signed in to change notification settings - Fork 7
Accessing Values
mosop edited this page Jan 22, 2017
·
38 revisions
To access named values, use value accessors or value hash indexers.
If you define a named value, a corresponding value accessor is automatically defined.
A value accessor name is determined by a value name. Any leading minus signs (-) are eliminated and other minus signs are converted to underscore letters (_). For example, --option-name is converted to option_name.
If a value type is Bool, the corresponding accessor name has a trailing ? mark.
class Model < Optarg::Model
string "-s"
bool "-b"
array "-a"
arg "arg"
arg_array "args"
end
result = Model.parse(%w(-s foo -b -a bar -a baz blep blah boop))
result.s # "foo"
result.b? # true
result.a # ["bar", "baz"]
result.arg # "blep"
result.args # ["blah", "boop"]If a value type is Bool and the value is undefined, the corresponding value accessor returns false.
If a value type is String, a nilable accessor is also defined. The nilable accessor returns nil if the value is undefined.
class Model < Optarg::Model
string "-s"
bool "-b"
end
result = Model.parse(%w())
result.s # raises an error
result.s? # nil
result.b? # false