class Argy::Parser

Parses command line arguments.

Attributes

arguments[R]

The arguments that were declared @return [Array<Argument>]

examples[R]

The examples that were declared @return [Array<String>]

flags[R]

The flags that were declared @return [Array<Array(Array<String>, Proc)>]

options[R]

The options that were declared @return [Array<Option>]

Public Class Methods

new() { |self| ... } click to toggle source
# 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

argument(*args, **opts) click to toggle source

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
default_values() click to toggle source

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
description(description = nil) click to toggle source

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
example(example) click to toggle source

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
help(**opts) click to toggle source

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
on(*args, &action) click to toggle source

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
option(*args, **opts) click to toggle source

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
parameters() click to toggle source

All parameters that are defined @return [Array<Argument, Option>]

# File lib/argy/parser.rb, line 106
def parameters
  arguments + options
end
parse(argv, strategy: nil) click to toggle source

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
usage(usage = nil) click to toggle source

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!(values) click to toggle source

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

build_parser(values) click to toggle source
# 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
populate_arguments(values, argv) click to toggle source
# 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