Module: RakeCommander::Options::Arguments

Includes:
Name
Defined in:
lib/rake-commander/options/arguments.rb

Overview

Offers helpers to treat ARGV

Constant Summary

Constants included from Name

Name::DOUBLE_HYPHEN_REGEX, Name::HYPEN_REGEX, Name::HYPHEN_START_REGEX, Name::OPTIONAL_REGEX, Name::SINGLE_HYPHEN_REGEX, Name::SPACE_REGEX, Name::UNDERSCORE_REGEX

Instance Method Summary collapse

Methods included from Name

#argument_optional?, #argument_required?, #double_hyphen?, #name_argument, #name_argument?, #name_hyphen, #name_hyphen?, #name_sym, #name_word_sym, #short_hyphen, #short_hyphen?, #short_sym, #single_hyphen?, #valid_name?, #valid_short?

Instance Method Details

#pre_parse_arguments(argv = ARGV, options) ⇒ Array<String>

Note:
  1. Any word or letter with hypen -`or _double hypen_--` is interpreted as option(s)
  2. To overcome this limitation, you may enclose in double quotes and argument with that start (i,e, "--argument").

Options with arguments should not take another option as value. OptionParser can do this even if the the argument is optional. This method re-arranges the arguments based on options that receive parameters, provided that an option is not taken as a value of a previous option that accepts arguments. If an option with argument is missing the argument, but has a default value, that default value will be inserted after the option in the array to prevent the OptionParser::MissingArgument error to stop the parsing process.

Examples:

1. `-abc ARGUMENT` where only `c` receives the argument becomes `-ab -c ARGUMENT`
3. `-abc ARGUMENT` where `b` and `c` are argument receivers becomes `-a -b nil -c ARGUMENT`
2. `-acb ARGUMENT` where only `c` receives the argument becomes `-a -c nil -b ARGUMENT`
4. `-c --some-option ARGUMENT` where both options receive argument, becomes `-c nil --some-option ARGUMENT`
5. `-c --some-option -d ARGUMENT` where both options receive argument, becomes `-c nil --some-option nil -d ARGUMENT`
6. `-cd ARGUMENT` where `c` default is `"yeah"`, becomes `-c yeah -d ARGUMENT`

Parameters:

  • argv (Array<String>) (defaults to: ARGV)
  • options (Hash)

    the defined RakeCommander::Option to re-arrange argv with.

Returns:

  • (Array<String>)

    the re-arranged argv



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rake-commander/options/arguments.rb', line 28

def pre_parse_arguments(argv = ARGV, options)
  pre_parsed = explicit_argument_options(argv, options)
  compact_short = ''
  pre_parsed.each_with_object([]) do |(opt_ref, args), out|
    next out.push(*args) unless opt_ref.is_a?(Symbol)
    is_short = opt_ref.to_s.length == 1
    next compact_short << opt_ref.to_s if is_short && args.empty?
    out.push("-#{compact_short}") unless compact_short.empty?
    compact_short = ''
    opt_str = is_short ? "-#{opt_ref}" : name_hyphen(opt_ref)
    out.push(opt_str, *args)
  end.tap do |out|
    out.push("-#{compact_short}") unless compact_short.empty?
  end
end