Module: Rubikon::HasArguments

Includes:
Parameter
Included in:
Command, Option
Defined in:
lib/rubikon/has_arguments.rb

Overview

This module is included in all classes used for parsing command-line arguments

See Also:

Author:

  • Sebastian Staudt

Since:

  • 0.4.0

Constant Summary collapse

ARGUMENT_MATCHERS =

Provides a number of predefined regular expressions to check arguments against

See Also:

Since:

  • 0.6.0

{
  # Allow only alphanumeric characters
  :alnum   => /[[:alnum:]]+/,
  # Allow only floating point numbers as arguments
  :float   => /-?[0-9]+(?:\.[0-9]+)?/,
  # Allow only alphabetic characters
  :letters => /[a-zA-Z]+/,
  # Allow only numeric arguments
  :numeric => /-?[0-9]+/
}

Instance Attribute Summary

Attributes included from Parameter

#aliases, #description, #name

Instance Method Summary collapse

Methods included from Parameter

#active?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

If a named argument with the specified method name exists, a call to that method will return the value of the argument.

Examples:

option :user, [:name] do
  @user = name
end

See Also:

Since:

  • 0.4.0



247
248
249
250
251
252
253
# File 'lib/rubikon/has_arguments.rb', line 247

def method_missing(name, *args, &block)
  if args.empty? && !block_given? && @arg_names.include?(name)
    @args[name]
  else
    super
  end
end

Instance Method Details

#[](arg) ⇒ Object

Access the arguments of this parameter using a numeric or symbolic index

Parameters:

  • arg (Numeric, Symbol)

    The name or index of the argument to return. Numeric indices can be used always while symbolic arguments are only available for named arguments.

Returns:

  • The argument with the specified index

See Also:

Since:

  • 0.4.0



144
145
146
# File 'lib/rubikon/has_arguments.rb', line 144

def [](arg)
  @args[arg]
end

#argsArray<String>, Hash<Symbol, String> Also known as: arguments

Returns the arguments given to this parameter. They are given as a Hash when there are named arguments or as an Array when there are no named arguments

Returns:

  • (Array<String>, Hash<Symbol, String>)

    The arguments given to this parameter

Since:

  • 0.6.0



155
156
157
# File 'lib/rubikon/has_arguments.rb', line 155

def args
  @arg_names.empty? ? @args.values : @args
end

#initialize(app, name, *options, &block) ⇒ Object

Creates a new parameter with arguments with the given name and an optional code block

Parameters:

  • app (Application::Base)

    The application this parameter belongs to

  • name (Symbol, #to_sym)

    The name of the parameter

  • options (Array)

    A range allows any number of arguments inside the limits of the range or array (-1 stands for an arbitrary number of arguments). A positive number indicates the exact amount of required arguments while a negative argument count indicates the amount of required arguments, but allows additional, optional arguments. A argument count of 0 means there are no required arguments, but it allows optional arguments. An array of symbols enables named arguments where the argument count is the size of the array and each argument is named after the corresponding symbol. Finally a hash may be used to specify options for named arguments. The keys of the hash will be the names of the arguments and the values are options for this argument. You may specify multiple options as an array. Possible options are:

    • :optional makes the argument optional

    • :remainder makes the argument take all remaining arguments as an array

    • One or more strings will cause the argument to be checked to be equal to one of the strings

    • One or more regular expressions will cause the argument to be checked to match one of the expressions

    • Other symbols may reference to a predefined regular expression from ARGUMENT_MATCHERS

  • block (Proc)

    An optional code block to be executed if this option is used

Since:

  • 0.4.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubikon/has_arguments.rb', line 68

def initialize(app, name, *options, &block)
  super(app, name, &block)

  @arg_names  = []
  @arg_values = {}
  @args       = {}

  @description = options.shift if options.first.is_a? String

  if options.size == 1 && (options.first.nil? ||
     options.first.is_a?(Fixnum) || options.first.is_a?(Range))
    options = options.first
  end

  if options.is_a? Fixnum
    if options > 0
      @min_arg_count = options
      @max_arg_count = options
    elsif options <= 0
      @min_arg_count = -options
      @max_arg_count = -1
    end
  elsif options.is_a? Range
    @min_arg_count = options.first
    @max_arg_count = options.last
  elsif options.is_a? Array
    @arg_names = []
    @max_arg_count = 0
    @min_arg_count = 0
    options.each do |arg|
      if arg.is_a? Hash
        arg = arg.map do |arg_name, opt|
          [arg_name, opt.is_a?(Array) ? opt : [opt]]
        end
        arg = arg.sort_by do |arg_name, opt|
          opt.include?(:optional) ? 1 : 0
        end
        arg.each do |arg_name, opt|
          matchers = opt.reject { |o| [:optional, :remainder].include? o }
          opt -= matchers
          @arg_names << arg_name.to_sym
          if !matchers.empty?
            matchers.map! do |m|
              ARGUMENT_MATCHERS[m] || (m.is_a?(Regexp) ? m : m.to_s)
            end
            @arg_values[arg_name] = /^#{Regexp.union *matchers}$/
          end
          unless opt.include? :optional
            @min_arg_count += 1
          end
          if opt.include? :remainder
            @max_arg_count = -1
            break
          end
          @max_arg_count += 1
        end
      else
        @arg_names << arg.to_sym
        @min_arg_count += 1
        @max_arg_count += 1
      end
    end
  else
    @min_arg_count = 0
    @max_arg_count = 0
  end
end