class Argy::Parser
Parses command line arguments.
Attributes
The arguments that were declared @return [Array<Argument>]
The examples that were declared @return [Array<String>]
The flags that were declared @return [Array<Array(Array<String>, Proc)>]
The options that were declared @return [Array<Option>]
Public Class Methods
# File lib/argy/parser.rb, line 26 def initialize @usage = $0 @description = nil @arguments = [] @options = [] @flags = [] @examples = [] yield self if block_given? end
Public Instance Methods
Adds an argument @see Argument#initialize @example
Argy.new do |o| o.argument :input end
# File lib/argy/parser.rb, line 78 def argument(*args, **opts) @arguments << Argument.new(*args, **opts) end
Build the default values for the declared paramters. @return [Hash{Symbol => Object}]
# File lib/argy/parser.rb, line 119 def default_values parameters.reduce(unused_args: []) do |acc, opt| acc[opt.name] = opt.default acc end end
Gets or sets a description for your program. If the provided description is nil, the description will not change. @param description [String,nil] @return [String] @example
Argy.new do |o| o.description "a really useful program" end
# File lib/argy/parser.rb, line 58 def description(description = nil) @description = description if description @description end
Adds an example @example
Argy.new do |o| o.example "$ example foo" end
# File lib/argy/parser.rb, line 68 def example(example) @examples << example end
Generate help for this parser. @see Help#initialize @return [Help]
# File lib/argy/parser.rb, line 113 def help(**opts) Help.new(self, **opts) end
Adds a flag @example
Argy.new do |o| o.on "-v", "--version" do puts Example::VERSION exit end end
# File lib/argy/parser.rb, line 100 def on(*args, &action) @flags << [args, action] end
Adds an option @see Option#initialize @example
Argy.new do |o| o.option :verbose, type: :boolean end
# File lib/argy/parser.rb, line 88 def option(*args, **opts) @options << Option.new(*args, **opts) end
All parameters that are defined @return [Array<Argument, Option>]
# File lib/argy/parser.rb, line 106 def parameters arguments + options end
Build the default values for the declared paramters. @param argv [Array<String>] the command line arguments to parse @param strategy [Symbol,nil] can be either `:order` or `:permute`. See
`OptionParser#order!` and `OptionParser#permute!` for more info.
@raise [ParseError] when the arguments can't be parsed @return [Hash{Symbol => Object}]
# File lib/argy/parser.rb, line 132 def parse(argv, strategy: nil) argv = argv.dup values = default_values parser = build_parser(values) case strategy when :order parser.order!(argv) when :permute parser.permute!(argv) else parser.parse!(argv) end populate_arguments(values, argv) Options.new validate!(values) rescue OptionParser::ParseError => error raise ParseError.new(error) end
Gets or sets the usage for your program. If the provided usage is nil, the usage will not change. @param usage [String,nil] sets the usage if not nil @return [String] usage @example
Argy.new do |o| o.usage "example [INPUT]" end
# File lib/argy/parser.rb, line 44 def usage(usage = nil) @usage = usage if usage @usage end
Validate the values @param values [Hash{Symbol => Object}] @return [Hash{Symbol => Object}] @raise [ValidationError] when the input is invalid
# File lib/argy/parser.rb, line 156 def validate!(values) parameters.each do |param| param.validate(values[param.name]) end values end
Private Instance Methods
# File lib/argy/parser.rb, line 175 def build_parser(values) OptionParser.new do |o| options.each do |opt| o.on(*opt.to_option_parser) do |value| values[opt.name] = opt.coerce(value) end end flags.each do |flag, action| o.on(*flag, &action) end end end
# File lib/argy/parser.rb, line 165 def populate_arguments(values, argv) argv.zip(arguments).each do |value, arg| if arg.nil? values[:unused_args] << value else values[arg.name] = arg.coerce(value) end end end