class OptParseBuilder::ArgumentBuilder
Builds arguments using a builder style DSL. You never create an instance of this class yourself. Instead, an instance is yielded to you by OptParseBuilder
.
See the README for examples.
Public Instance Methods
Set the argument's default value. This it the value an argument has before parsing, or if parsing does not set the value.
If an argument's default value is not explicitly set, then the default value is `nil`.
# File lib/opt_parse_builder/argument_builder.rb, line 31 def default(v) @default = v end
Set a handler, a proc that will will process the argument's value. The proc takes two arguments:
-
The
Argument
-
The value
If no handler is set, it defaults to
`->(argument, value) { argument.value = value }
Applies to these argument types:
-
Simple option
-
Option with value
Example:
arg = OptParseBuilder.build_argument do |arg| arg.key :square arg.on "-v", "Increase verbosity (can give more than once)" arg.handler do |argument, _value| argument.value += 1 end end
# File lib/opt_parse_builder/argument_builder.rb, line 99 def handler(&block) @handler = block end
Set the argument's key. Accepts either a string or a symbol.
# File lib/opt_parse_builder/argument_builder.rb, line 22 def key(v) @key = v.to_sym end
Declares the argument to be an option that is handled by OptParse. The arguments are passed to OptParse exactly as you give them, except that the string DEFAULT is replaced with the argument's default value.
Simple example:
arg = OptParseBuilder.build_argument do |arg| arg.key :quiet arg.on "-q", "Be very veru quiet", "We're hunting rabbit!" end
You may split up a long argument list by calling this method more than once. This is equivalent to the above:
arg = OptParseBuilder.build_argument do |arg| arg.key :quiet arg.on "-q", "Be very veru quiet", arg.on "We're hunting rabbit!" end
So that the option's help may print the default without having to duplicate it, the string DEFAULT is replaced with the argument's default value:
arg = OptParseBuilder.build_argument do |arg| arg.key :size arg.default 1024 arg.on "--size=N", Integer, arg.on "Size in bytes (default _DEFAULT_)" end
When the `–help` text for this argument is printed, it will read:
--size-N Size in bytes (default 1024)
# File lib/opt_parse_builder/argument_builder.rb, line 71 def on(*option_args) @on.concat(option_args) end
Declare the operand to be an optional operand. An optional operand consumes one argument. If the argument is not present, value is either the default (if provided), or nil (if no default was provided).
# File lib/opt_parse_builder/argument_builder.rb, line 129 def optional_operand(help_name: nil) check_operand_class_not_set @operand_class = OptionalOperandArgument @operand_help_name = help_name end
Declare the operand to be a required operand. A required operand consumes one argument, generating an error if there is not one.
# File lib/opt_parse_builder/argument_builder.rb, line 138 def required_operand(help_name: nil) check_operand_class_not_set @operand_class = RequiredOperandArgument @operand_help_name = help_name end
Add to the separator text shown last in the –help output. You may call this more than once; each call adds another line of text to the separator.
Any type of argument may have separator text.
See also OptParseBuilder#separator
# File lib/opt_parse_builder/argument_builder.rb, line 121 def separator(line) @separator_lines << line end
Declare the argument to be a “splat” operand. A splat operand consumes all remaining arguments.
# File lib/opt_parse_builder/argument_builder.rb, line 146 def splat_operand(help_name: nil) check_operand_class_not_set @operand_class = SplatOperandArgument @operand_help_name = help_name end
Private Instance Methods
# File lib/opt_parse_builder/argument_builder.rb, line 185 def check_for_build_errors if !@on.empty? && @operand_class raise BuildError, "Argument cannot be both an option and an operand" end end
# File lib/opt_parse_builder/argument_builder.rb, line 179 def check_operand_class_not_set if @operand_class raise BuildError, "Argument is already an operand" end end