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
-
#pre_parse_arguments(argv = ARGV, options) ⇒ Array<String>
Options with arguments should not take another option as value.
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>
- Any word or letter with hypen -`
or _double hypen_--` is interpreted as option(s) - 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.
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, ) pre_parsed = (argv, ) 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 |