module OptParseBuilder
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.banner "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
Constants
- VERSION
Library version. Follows Semantic Versioning 2.0
Public Class Methods
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.
# File lib/opt_parse_builder.rb, line 117 def self.build_argument builder = ArgumentBuilder.new yield builder builder.argument end
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.
# File lib/opt_parse_builder.rb, line 150 def self.build_bundle bundler = ArgumentBundleBuilder.new yield bundler bundler.argument end
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
# 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