Class: Toys::DSL::Flag
- Inherits:
-
Object
- Object
- Toys::DSL::Flag
- Defined in:
- lib/toys/dsl/flag.rb
Overview
DSL for a flag definition block. Lets you set flag attributes in a block instead of a long series of keyword arguments.
These directives are available inside a block passed to Tool#flag.
Example
tool "mytool" do
flag :value do
# The directives in here are defined by this class
flags "--value=VAL"
accept Integer
desc "An integer value"
end
# ...
end
Instance Method Summary collapse
-
#accept(spec = nil, **options, &block) ⇒ self
Set the acceptor for this flag's values.
-
#complete_flags(spec = nil, **options, &block) ⇒ self
Set the shell completion strategy for flag names.
-
#complete_values(spec = nil, **options, &block) ⇒ self
Set the shell completion strategy for flag values.
-
#default(default) ⇒ self
Set the default value.
-
#desc(desc) ⇒ self
Set the short description for the current flag.
-
#display_name(display_name) ⇒ self
Set the display name for this flag.
-
#flags(*flags) ⇒ self
Add flags in OptionParser format.
-
#group(group) ⇒ self
Set the group.
-
#handler(handler = nil, &block) ⇒ self
Set the optional handler for setting/updating the value when a flag is parsed.
-
#long_desc(*long_desc) ⇒ self
Add to the long description for the current flag.
-
#report_collisions(setting) ⇒ self
Set whether to raise an exception if a flag is requested that is already in use or marked as disabled.
Instance Method Details
#accept(spec = nil, **options, &block) ⇒ self
Set the acceptor for this flag's values. You can pass either the string name of an acceptor defined in this tool or any of its ancestors, or any other specification recognized by Acceptor.create.
100 101 102 103 |
# File 'lib/toys/dsl/flag.rb', line 100 def accept(spec = nil, **, &block) @acceptor = Acceptor.scalarize_spec(spec, , block) self end |
#complete_flags(spec = nil, **options, &block) ⇒ self
Set the shell completion strategy for flag names. You can pass one of the following:
- The string name of a completion defined in this tool or any of its ancestors.
- A hash of options to pass to the constructor of Flag::DefaultCompletion.
-
nil
or:default
to select the standard completion strategy (which is Flag::DefaultCompletion with no extra options). - Any other specification recognized by Completion.create.
149 150 151 152 |
# File 'lib/toys/dsl/flag.rb', line 149 def complete_flags(spec = nil, **, &block) @flag_completion = Completion.scalarize_spec(spec, , block) self end |
#complete_values(spec = nil, **options, &block) ⇒ self
Set the shell completion strategy for flag values. You can pass either the string name of a completion defined in this tool or any of its ancestors, or any other specification recognized by Completion.create.
165 166 167 168 |
# File 'lib/toys/dsl/flag.rb', line 165 def complete_values(spec = nil, **, &block) @value_completion = Completion.scalarize_spec(spec, , block) self end |
#default(default) ⇒ self
Set the default value.
111 112 113 114 |
# File 'lib/toys/dsl/flag.rb', line 111 def default(default) @default = default self end |
#desc(desc) ⇒ self
Set the short description for the current flag. The short description is displayed with the flag in online help.
The description is a WrappableString, which may be word-wrapped when displayed in a help screen. You may pass a WrappableString directly to this method, or you may pass any input that can be used to construct a wrappable string:
- If you pass a String, its whitespace will be compacted (i.e. tabs, newlines, and multiple consecutive whitespace will be turned into a single space), and it will be word-wrapped on whitespace.
- If you pass an Array of Strings, each string will be considered a literal word that cannot be broken, and wrapping will be done across the strings in the array. In this case, whitespace is not compacted.
Examples
If you pass in a sentence as a simple string, it may be word wrapped when displayed:
desc "This sentence may be wrapped."
To specify a sentence that should never be word-wrapped, pass it as the sole element of a string array:
desc ["This sentence will not be wrapped."]
214 215 216 217 |
# File 'lib/toys/dsl/flag.rb', line 214 def desc(desc) @desc = desc self end |
#display_name(display_name) ⇒ self
Set the display name for this flag. This may be used in help text and error messages.
266 267 268 269 |
# File 'lib/toys/dsl/flag.rb', line 266 def display_name(display_name) @display_name = display_name self end |
#flags(*flags) ⇒ self
Add flags in OptionParser format. This may be called multiple times, and the results are cumulative.
Following are examples of valid syntax.
-
-a
: A short boolean switch. When this appears as an argument, the value is set totrue
. -
--abc
: A long boolean switch. When this appears as an argument, the value is set totrue
. -
-aVAL
or-a VAL
: A short flag that takes a required value. These two forms are treated identically. If this argument appears with a value attached (e.g.-afoo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, the following argument is taken as the value (e.g. for-a foo
, the value is set to"foo"
.) The following argument is treated as the value even if it looks like a flag (e.g.-a -a
causes the string"-a"
to be taken as the value.) -
-a[VAL]
: A short flag that takes an optional value. If this argument appears with a value attached (e.g.-afoo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, the value is set totrue
. The following argument is never interpreted as the value. (Compare with-a [VAL]
.) -
-a [VAL]
: A short flag that takes an optional value. If this argument appears with a value attached (e.g.-afoo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, if the following argument does not look like a flag (i.e. it does not begin with a hyphen), it is taken as the value. (e.g.-a foo
causes the string"foo"
to be taken as the value.). If there is no following argument, or the following argument looks like a flag, the value is set totrue
. (Compare with-a[VAL]
.) -
--abc=VAL
or--abc VAL
: A long flag that takes a required value. These two forms are treated identically. If this argument appears with a value attached (e.g.--abc=foo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, the following argument is taken as the value (e.g. for--abc foo
, the value is set to"foo"
.) The following argument is treated as the value even if it looks like a flag (e.g.--abc --def
causes the string"--def"
to be taken as the value.) -
--abc[=VAL]
: A long flag that takes an optional value. If this argument appears with a value attached (e.g.--abc=foo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, the value is set totrue
. The following argument is never interpreted as the value. (Compare with--abc [VAL]
.) -
--abc [VAL]
: A long flag that takes an optional value. If this argument appears with a value attached (e.g.--abc=foo
), the attached string (e.g."foo"
) is taken as the value. Otherwise, if the following argument does not look like a flag (i.e. it does not begin with a hyphen), it is taken as the value. (e.g.--abc foo
causes the string"foo"
to be taken as the value.). If there is no following argument, or the following argument looks like a flag, the value is set totrue
. (Compare with--abc=[VAL]
.) -
--[no-]abc
: A long boolean switch that can be turned either on or off. This effectively creates two flags,--abc
which sets the value totrue
, and--no-abc
which sets the falue tofalse
.
84 85 86 87 |
# File 'lib/toys/dsl/flag.rb', line 84 def flags(*flags) @flags += flags.flatten self end |
#group(group) ⇒ self
Set the group. A group may be set by name or group object. Setting
nil
selects the default group.
254 255 256 257 |
# File 'lib/toys/dsl/flag.rb', line 254 def group(group) @group = group self end |
#handler(handler = nil, &block) ⇒ self
Set the optional handler for setting/updating the value when a flag is
parsed. A handler should be a Proc taking two arguments, the new given
value and the previous value, and it should return the new value that
should be set. You may pass the handler as a Proc (or an object
responding to the call
method) or you may pass a block.
127 128 129 130 |
# File 'lib/toys/dsl/flag.rb', line 127 def handler(handler = nil, &block) @handler = handler || block self end |
#long_desc(*long_desc) ⇒ self
Add to the long description for the current flag. The long description is displayed with the flag in online help. This directive may be given multiple times, and the results are cumulative.
A long description is a series of descriptions, which are generally displayed in a series of lines/paragraphs. Each individual description uses the form described in the #desc documentation, and may be word-wrapped when displayed. To insert a blank line, include an empty string as one of the descriptions.
Example
long_desc "This initial paragraph might get word wrapped.",
"This next paragraph is followed by a blank line.",
"",
["This line will not be wrapped."],
[" This indent is preserved."]
long_desc "This line is appended to the description."
242 243 244 245 |
# File 'lib/toys/dsl/flag.rb', line 242 def long_desc(*long_desc) @long_desc += long_desc self end |
#report_collisions(setting) ⇒ self
Set whether to raise an exception if a flag is requested that is already in use or marked as disabled.
177 178 179 180 |
# File 'lib/toys/dsl/flag.rb', line 177 def report_collisions(setting) @report_collisions = setting self end |