Module: RakeCommander::Options::Arguments::ClassMethods
- Includes:
- Name
- Defined in:
- lib/rake-commander/options/arguments.rb
Constant Summary
Constants included from Name
Name::BOOLEAN_NAME_REGEX, Name::BOOLEAN_TOKEN, Name::DOUBLE_HYPHEN_REGEX, Name::HYPEN_REGEX, Name::HYPHEN_START_REGEX, Name::OPTIONAL_REGEX, Name::SINGLE_HYPHEN_REGEX, Name::UNDERSCORE_REGEX, Name::WORD_DELIMITER
Instance Method Summary collapse
-
#argv_cropping_for_rake(value = :not_used) ⇒ Boolean
Configuration setting Whether the additional arguments (extended options) managed by this gem should be removed/consumed from
ARGV
beforeRake
processes option arguments. -
#argv_extended_options(argv = ARGV.dup) ⇒ Array<String>
It returns the part of
ARGV
that are arguments ofRakeCommander::Options
parsing. -
#argv_pre_parsed(argv = ARGV, options:) ⇒ Array<String>
Options with arguments should not take another option as value.
-
#argv_rake_native_arguments(argv = ARGV.dup) ⇒ Array<String>
It slices from the original
ARGV
the extended_options of this gem. -
#argv_with_enhanced_syntax?(argv = ARGV) ⇒ Boolean
Whether enhanced parsing should be switched on or off.
-
#parse_options(argv = ARGV, *args, **kargs, &block) ⇒ Object
Re-open
parse_options
method, provided that we sliceARGV
to only include the extended options of this gem, which start atRAKE_COMMAND_EXTENDED_OPTIONS_START
.
Methods included from Name
#argument_optional?, #argument_required?, #boolean_name?, #capture_argument_with!, #capture_arguments_name!, #capture_arguments_short!, #double_hyphen?, #name_argument, #name_argument?, #name_hyphen, #name_sym, #name_word_sym, #short_hyphen, #short_sym, #single_hyphen?, #valid_name?, #valid_short?
Instance Method Details
#argv_cropping_for_rake(value = :not_used) ⇒ Boolean
- When
true
it will enable- A patch on
Rake::Application
, provided thatARGV
is cropped beforeRake
identifies **tasks and rake native options. Note that this specific patch only works if rake commander was loaded BEFORERake::Application#run
is invoked.
- A patch on
- When
false
, an implicitexit(0)
is added at the end of a rake task defined viaRakeCommander
, as a work-around that preventsRake
from chaining option arguments as if they were actual tasks.
- This only refers to what comes after
RAKE_COMMAND_EXTENDED_OPTIONS_START
(--
)
Configuration setting
Whether the additional arguments (extended options) managed by this gem
should be removed/consumed from ARGV
before Rake
processes option arguments.
41 42 43 44 45 |
# File 'lib/rake-commander/options/arguments.rb', line 41 def argv_cropping_for_rake(value = :not_used) @argv_cropping_for_rake = true if @argv_cropping_for_rake.nil? return @argv_cropping_for_rake if value == :not_used @argv_cropping_for_rake = !!value end |
#argv_extended_options(argv = ARGV.dup) ⇒ Array<String>
please observe that Rake
has it's own options. For this reason using
a delimiter (RAKE_COMMAND_EXTENDED_OPTIONS_START
) shows up to be necessary to
create some sort of command line argument namespacing.
It returns the part of ARGV
that are arguments of RakeCommander::Options
parsing.
53 54 55 56 57 58 59 |
# File 'lib/rake-commander/options/arguments.rb', line 53 def (argv = ARGV.dup) if idx = argv.index(RAKE_COMMAND_EXTENDED_OPTIONS_START) argv[idx+1..-1] else [] end end |
#argv_pre_parsed(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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rake-commander/options/arguments.rb', line 107 def argv_pre_parsed(argv = ARGV, options:) 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 |
#argv_rake_native_arguments(argv = ARGV.dup) ⇒ Array<String>
this is necessary to prevent Rake
to interpret them.
It slices from the original ARGV
the extended_options of this gem.
64 65 66 67 68 69 70 |
# File 'lib/rake-commander/options/arguments.rb', line 64 def argv_rake_native_arguments(argv = ARGV.dup) return argv unless argv_cropping_for_rake if idx = argv.index(RAKE_COMMAND_EXTENDED_OPTIONS_START) argv = argv[0..idx] end argv end |
#argv_with_enhanced_syntax?(argv = ARGV) ⇒ Boolean
it assumes ARGV
has been left unaltered.
Returns whether enhanced parsing should be switched on or off.
21 22 23 24 |
# File 'lib/rake-commander/options/arguments.rb', line 21 def argv_with_enhanced_syntax?(argv = ARGV) return false unless argv.is_a?(Array) argv.include?(RAKE_COMMAND_EXTENDED_OPTIONS_START) end |
#parse_options(argv = ARGV, *args, **kargs, &block) ⇒ Object
- Without this
ARGV
cut, it will throwOptionParser::InvalidOption
error- So some tidy up is necessary and the head of the command (i.e.
rake some:task --
) should be excluded from arguments to input to the options parser.
- So some tidy up is necessary and the head of the command (i.e.
Re-open parse_options
method, provided that we slice ARGV
to only include the extended options of this gem, which start at
RAKE_COMMAND_EXTENDED_OPTIONS_START
.
80 81 82 83 84 |
# File 'lib/rake-commander/options/arguments.rb', line 80 def (argv = ARGV, *args, **kargs, &block) argv = (argv) argv = argv_pre_parsed(argv, options: (with_implicit: true)) super(argv, *args, **kargs, &block) end |