Class: Cuprum::Cli::Integrations::Thor::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/cli/integrations/thor/arguments_parser.rb

Overview

Utility for parsing command-line arguments captured by Thor tasks.

Any unrecognized command line flags or options are appended as-is to the arguments array by Thor. Therefore, to handle cases such as variadic options where flags or options cannot be pre-parsed by Thor, we need an additional parsing step to pull any remaining flags or options out of the arguments.

This parser supports the following formats:

  • -a, --all: Sets the a or all flag to true.
  • -abc: Sets the a, b, and c flags to true.
  • --skip-all, --no-all: Sets the a flag to false.
  • -a=value, --a=value: Sets the a option to "value".

The following formats are specifically not supported:

  • --foo bar: --foo is assumed to be a flag, bar is assumed to be a positional argument.
  • --str[]=foo --str[]=bar: Array arguments are not supported.
  • --str[foo]=foo --str[bar]=bar: Hash arguments are not supported.

In addition, parsed option values are coerced into their most likely intended types.

Instance Method Summary collapse

Instance Method Details

#call(*inputs) ⇒ Array<Array<String>, Hash{Symbol=>Object}>

Parses the given argument inputs into arguments and options.

Parameters:

  • inputs (Array<String>)

    the arguments captured by Thor.

Returns:

  • (Array<Array<String>, Hash{Symbol=>Object}>)

    the parsed arguments and options.



36
37
38
39
40
# File 'lib/cuprum/cli/integrations/thor/arguments_parser.rb', line 36

def call(*inputs)
  raw_options, arguments = inputs.partition { |str| str.start_with?('-') }

  [arguments, parse_options(raw_options)]
end