Class: RVGP::Base::Command::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rvgp/base/command.rb

Overview

Option(s) are, as the name would imply, a command line option, that is prefixed with one or more dashes. Whereas some arguments are program targets, options typically expresses a global program setting, to take effect during this execution.

Some options are binaries, and are presumed ‘off’ if unspecified. Other options are key/value pairs, separated by an equal sign or space, in a form such as “-d ~/ledger” or “–dir=~/ledger”. Option keys are expected to exist in both a short and long form. In the previous example, both the “-d” and “–dir” examples are identical. The “-d” form is a short form and “–dir” is a long form, of the same Option.

This class offers common functions for specifying and parsing options on the command line, as well as for producing the documentation on an option.

Defined Under Namespace

Classes: UnexpectedEndOfArgs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(long, short, options = {}) ⇒ Option

Create a new Option

Parameters:

  • short (String)

    see #short

  • long (String)

    see #long

  • options (Hash) (defaults to: {})

    additional parameters to configure this Option with

Options Hash (options):

  • :has_value (TrueClass, FalseClass) — default: false

    This flag indicates that this option is expected to have a corresponding value, for its key.



193
194
195
196
197
# File 'lib/rvgp/base/command.rb', line 193

def initialize(long, short, options = {})
  @short = short.to_sym
  @long = long.to_sym
  @has_value = options[:has_value] if options.key? :has_value
end

Instance Attribute Details

#longSymbol (readonly)

A multi-character code, which identifies this option

Returns:

  • (Symbol)

    the current value of long



180
181
182
# File 'lib/rvgp/base/command.rb', line 180

def long
  @long
end

#shortSymbol (readonly)

A one character code, which identifies this option

Returns:

  • (Symbol)

    the current value of short



180
181
182
# File 'lib/rvgp/base/command.rb', line 180

def short
  @short
end

Class Method Details

.remove_options_from_args(options, args) ⇒ Array<Hash<Symbol,Object>,Array<String>>

Given program arguments, and an array of options that we wish to support, return the options and arguments that were encountered.

Parameters:

  • options (Array<RVGP::Base::Command::Option>)

    The options to that we want to parse, from out of the provided args

  • args (Array<String>)

    Program arguments, as would be provided by a typical ARGV

Returns:

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

    A two-element array. The first element is a Hash of Symbols To Objects (Either TrueClass or String). The second is an Array of Strings. The first element represents what options were parsed, with the key for those options being represented by their :long form (regardless of what was encountered) The second element contains the targets that were encountered.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rvgp/base/command.rb', line 225

def self.remove_options_from_args(options, args)
  ret_args = []
  ret_options = {}

  i = 0
  until i >= args.length
    arg = args[i]
    arg_value = nil

    if /\A([^=]+)=([^ ]+)/.match arg
      arg = ::Regexp.last_match 1
      arg_value = ::Regexp.last_match 2
    end

    option = options.find { |opt| opt.matches? arg }

    if option
      ret_options[option.long] = if option.value?
                                   if arg_value.nil?
                                     if i + 1 >= args.length
                                       raise UnexpectedEndOfArgs, I18n.t('error.end_of_args')
                                     end

                                     i += 1
                                     args[i]
                                   else
                                     arg_value
                                   end
                                 else
                                   true
                                 end
    else
      ret_args << args[i]
    end

    i += 1
  end

  [ret_options, ret_args]
end

Instance Method Details

#matches?(str) ⇒ TrueClass, FalseClass

Returns true, if either our short or long form, equals the provided string

Parameters:

  • str (String)

    an option. This is expected to include one or more dashes.

Returns:

  • (TrueClass, FalseClass)

    Whether or not we can handle the provided option.



202
203
204
# File 'lib/rvgp/base/command.rb', line 202

def matches?(str)
  ["--#{long}", "-#{short}"].include? str
end

#value?TrueClass, FalseClass

Returns true, if we expect our key to be paired with a value. This property is specified in the :has_value option in the constructor.

Returns:

  • (TrueClass, FalseClass)

    Whether or not we expect a pair



209
210
211
# File 'lib/rvgp/base/command.rb', line 209

def value?
  !@has_value.nil?
end