class CLI::Mastermind::ArgParse

Processes command line arguments and provides a more useful representation of the provided options.

Attributes

pattern[R]

@return [Regexp] the pattern to use when filtering plans for display

plan_arguments[R]

@return [Array<String>] additional command line arguements passed into the executed plan

Public Class Methods

add_option(*args, &block) click to toggle source

Adds arbitrary options to the argument parser.

Mostly useful for tools wrapping mastermind to add options that all methods should have access to.

@param args arguments passed directly to OptionParser#on @param block [Proc] block passed as the handler for the above arguments @return [Void]

# File lib/cli/mastermind/arg_parse.rb, line 28
def add_option(*args, &block)
  @extra_options ||= []
  @extra_options << [args, block]
end
extra_options() click to toggle source

@see ArgParse.add_option @return [Array] a set of extra options added to the argument parser

# File lib/cli/mastermind/arg_parse.rb, line 16
def extra_options
  @extra_options ||= []
end
new(arguments=ARGV) click to toggle source

@param arguments [Array<String>] the arguements to parse

# File lib/cli/mastermind/arg_parse.rb, line 35
def initialize(arguments=ARGV)
  @initial_arguments = arguments
  @ask = true
  @display_ui = true
  @show_config = false
  @call_blocks = false

  parse_arguments
end

Public Instance Methods

ask?() click to toggle source

@return [Boolean] if the user should be asked for confirmation prior to plan execution

# File lib/cli/mastermind/arg_parse.rb, line 119
def ask?
  @ask
end
display_plans?() click to toggle source

@return [Boolean] if the user has requested plan display

# File lib/cli/mastermind/arg_parse.rb, line 97
def display_plans?
  !@pattern.nil?
end
display_ui?() click to toggle source

@return [Boolean] if the UI is displayed

# File lib/cli/mastermind/arg_parse.rb, line 114
def display_ui?
  @display_ui
end
do_command_expansion!(config) click to toggle source

Uses configured user aliases to perform command expansion.

For example, an alias defined in a masterplan like so:

define_alias 'foo', 'foobar'

when invoked like `mastermind foo` would operate as if the user had actually typed `mastermind foobar`.

User aliases (defined in a masterplan) are much more powerful than planfile aliases (defined in a planfile). Unlike planfile aliases, user aliases can define entire “plan stacks” and are recursively expanded.

For example, the following aliases:

define_alias 'foo', 'foobar'
define_alias 'bar', 'foo sub'

invoked as `mastermind bar` would operate as if the user had actually typed `mastermind foobar sub`.

Plan arguments can also be specified in a user alias. For example:

define_alias '2-add-2', 'calculator add -- 2 2'

would expand as expected with the extra arguements (`'2 2'`) being passed into the executed plan.

@param config [Configuration] the configuration object to use when expanding user aliases @return [Void]

# File lib/cli/mastermind/arg_parse.rb, line 75
def do_command_expansion!(config)
  @alias_arguments = []

  @mastermind_arguments.map! do |argument|
    expand_argument(config, argument)
  end

  @plan_arguments = @alias_arguments + @plan_arguments

  @mastermind_arguments.flatten!
  nil # prevent @mastermind_arguments from leaking
end
dump_config?() click to toggle source

@return [Boolean] if the user requested their configuration be displayed

# File lib/cli/mastermind/arg_parse.rb, line 124
def dump_config?
  @show_config
end
get_next_plan_name() click to toggle source

Removes and returns the plan name at the beginning of the argument list.

@return [String] the name of the next plan in the list of arguments

# File lib/cli/mastermind/arg_parse.rb, line 109
def get_next_plan_name
  @mastermind_arguments.shift
end
has_additional_plan_names?() click to toggle source

@return [Boolean] if additional plan names exist in mastermind's arguments

# File lib/cli/mastermind/arg_parse.rb, line 102
def has_additional_plan_names?
  @mastermind_arguments.any?
end
insert_base_plan!(base_plan) click to toggle source

Adds the given base plan to the beginning of the arguments array

@param base_plan [String] the base plan to add to the beginning of the arguments

# File lib/cli/mastermind/arg_parse.rb, line 91
def insert_base_plan!(base_plan)
  @mastermind_arguments.unshift base_plan
  nil # prevent @mastermind_arguments from leaking
end
parser() click to toggle source

@return [OptionParser] the parser to process command line arguments with

# File lib/cli/mastermind/arg_parse.rb, line 134
def parser
  @parser ||= OptionParser.new do |opt|
    opt.banner = 'Usage: mastermind [--help, -h] [--plans[ PATTERN], --tasks[ PATTERN], -T [PATTERN], -P [PATTERN] [PLAN[, PLAN[, ...]]] -- [PLAN ARGUMENTS]'

    opt.on('--help', '-h', 'Display this help') do
      puts opt
      exit
    end

    opt.on('-A', '--no-ask', "Don't ask before executing a plan") do
      @ask = false
    end

    opt.on('-U', '--no-fancy-ui', "Don't display the fancy UI") do
      @display_ui = false
    end

    opt.on('--plans [PATTERN]', '--tasks [PATTERN]', '-P', '-T', 'Display plans.  Optional pattern is used to filter the returned plans.') do |pattern|
      @pattern = Regexp.new(pattern || '.')
    end

    opt.on('-C', '--show-configuration', 'Load configuration and print final values.  Give multiple times to resolve lazy attributes as well.') do
      @call_blocks = @show_config
      @show_config = true
    end

    self.class.extra_options.each do |(arguments, block)|
      opt.on(*arguments, &block)
    end
  end
end
resolve_callable_attributes?() click to toggle source

@return [Boolean] if callable attributes should be resolved prior to being displayed

# File lib/cli/mastermind/arg_parse.rb, line 129
def resolve_callable_attributes?
  @call_blocks
end

Private Instance Methods

expand_argument(config, argument) click to toggle source

Performs alias expansion using the provided configuration object

@param config [Configuration] the configuration object used to perform expansion @param argument [String] the argument to be expanded

@return [Array<String>, String] the expanded arguments

# File lib/cli/mastermind/arg_parse.rb, line 174
def expand_argument(config, argument)
  dealiased = config.map_alias(argument)

  if dealiased.is_a? Array
    partitioned = dealiased.slice_before('--').to_a

    # Recursively expand plan names
    # NOTE: This does not defend against circular dependencies!
    plan_names = partitioned.shift.map { |arg|  expand_argument(config, arg) }.flatten

    plan_arguments = partitioned.shift

    if plan_arguments
      plan_arguments.shift # removes the --
      @alias_arguments.concat plan_arguments
    end

    dealiased = plan_names
  end

  dealiased
end
parse_arguments() click to toggle source

Splits the incoming arguments and processes those before the first `–`.

Arguments after the first `–` on the command line are passed verbatim into the executed plan.

# File lib/cli/mastermind/arg_parse.rb, line 201
def parse_arguments
  @mastermind_arguments = @initial_arguments.take_while { |arg| arg != '--' }
  @plan_arguments = @initial_arguments[(@mastermind_arguments.size + 1)..-1] || []

  unless @mastermind_arguments.empty?
    @mastermind_arguments = parser.parse *@mastermind_arguments
  end
end