Module: OptParseBuilder
- Defined in:
- lib/opt_parse_builder.rb,
lib/opt_parse_builder/errors.rb,
lib/opt_parse_builder/parser.rb,
lib/opt_parse_builder/version.rb,
lib/opt_parse_builder/argument.rb,
lib/opt_parse_builder/has_value.rb,
lib/opt_parse_builder/stable_sort.rb,
lib/opt_parse_builder/null_argument.rb,
lib/opt_parse_builder/parser_builder.rb,
lib/opt_parse_builder/argument_bundle.rb,
lib/opt_parse_builder/argument_values.rb,
lib/opt_parse_builder/banner_argument.rb,
lib/opt_parse_builder/option_argument.rb,
lib/opt_parse_builder/argument_builder.rb,
lib/opt_parse_builder/constant_argument.rb,
lib/opt_parse_builder/separator_argument.rb,
lib/opt_parse_builder/formats_operand_name.rb,
lib/opt_parse_builder/splat_operand_argument.rb,
lib/opt_parse_builder/argument_bundle_builder.rb,
lib/opt_parse_builder/optional_operand_argument.rb,
lib/opt_parse_builder/required_operand_argument.rb more...
Overview
The namespace of this library, and the sole entry point. You never have to (and never should) explicitly refer to any other class or module of this library than this one. There are a few other classes you will use, but they will be created for you by methods of this module.
Minimal example:
arg_parser = OptParseBuilder.build_parser
arg_parser.parse!
An example with a little bit of everything
arg_parser = OptParseBuilder.build_parser do |p|
parser. "A short description of the program"
parser.add do |arg|
arg.key :output_path
arg.required_operand
end
parser.add do |arg|
arg.key :input_paths
arg.splat_operand
end
parser.add do |arg|
arg.key :quiet
arg.on "-q", "--quiet", "Be quiet"
end
parser.add do |arg|
arg.key :size
arg.default 1024
arg.on "--size=N", Integer
arg.on "Size in bytes (default _DEFAULT_)"
end
parser.separator "Explanatory text at the bottom"
end
arg_values = arg_parser.parse!
p arg_values.quiet # nil or true
p arg_values.size # An Integer
p arg_values.output_path # A string
p arg_values.input_paths # An array of strings
Defined Under Namespace
Modules: FormatsOperandName, HasValue, StableSort Classes: Argument, ArgumentBuilder, ArgumentBundle, ArgumentBundleBuilder, ArgumentValues, BannerArgument, BuildError, ConstantArgument, Error, NullArgument, OptionArgument, OptionalOperandArgument, Parser, ParserBuilder, RequiredOperandArgument, SeparatorArgument, SplatOperandArgument
Constant Summary collapse
- VERSION =
Library version. Follows Semantic Versioning 2.0
"0.1.0"
Class Method Summary collapse
-
.build_argument {|builder| ... } ⇒ Object
Build an argument that can be added to a parser.
-
.build_bundle {|bundler| ... } ⇒ Object
Build a bundle of arguments that can be added to a parser together.
-
.build_parser {|parser_builder| ... } ⇒ Object
Create a new parser.
Class Method Details
permalink .build_argument {|builder| ... } ⇒ Object
Build an argument that can be added to a parser. Yields an ArgumentBuilder. Returns the argument created by the builder.
VERBOSE = OptParseBuilder.build_argument do |arg|
arg.key :verbose
arg.on "-v", "--verbose", "Print extra output"
end
arg_parser = OptParseBuilder.build_parser do |args|
args.add VERBOSE
end
See the README for argument examples.
Raises BuildError if the argument cannot be built or added.
This is most useful when you are building a related suite of programs that share some command-line arguments in common. Most of the time you will just add the argument using the block form of OptParseBuilder#add.
117 118 119 120 121 |
# File 'lib/opt_parse_builder.rb', line 117 def self.build_argument builder = ArgumentBuilder.new yield builder builder.argument end |
permalink .build_bundle {|bundler| ... } ⇒ Object
Build a bundle of arguments that can be added to a parser together. Yields an ArgumentBundleBuilder.
This is useful when you have a group of arguments that go together:
bundle = OptParseBuilder.build_bundle do |args|
args.add do |arg|
arg.key :x
op.on "-x", Integer, "X coordinate"
end
args.add do |arg|
arg.key :y
op.on "-y", Integer, "Y coordinate"
end
end
arg_parser = OptParseBuilder.build_parser do |args|
args.add bundle
end
Raises BuildError if the argument cannot be built or added.
This is most useful when you are building a related suite of programs that share some command-line arguments in common. Most of the time you will just add the arguments using the block form of OptParseBuilder#add.
150 151 152 153 154 |
# File 'lib/opt_parse_builder.rb', line 150 def self.build_bundle bundler = ArgumentBundleBuilder.new yield bundler bundler.argument end |
permalink .build_parser {|parser_builder| ... } ⇒ Object
Create a new parser. If called without a block, returns a parser than you can then add arguments to:
arg_parser = OptParseBuilder.build_parser
arg_parser.add do |arg|
arg.key :force
arg.on "--force", "Force dangerous operation"
end
If called with a block, yields itself to the block:
arg_parser = OptParseBuilder.build_parser do |args|
args.add do |arg|
arg.key :force
arg.on "--force", "Force dangerous operation"
end
end
Note that the parser constructed using the block form can still be added onto:
arg_parser.add do |arg|
arg.key :size
arg.on "--size=N", Integer, "File size in bytes"
end
91 92 93 94 95 |
# File 'lib/opt_parse_builder.rb', line 91 def self.build_parser parser_builder = ParserBuilder.new yield parser_builder if block_given? parser_builder.parser end |