Class: CLI::Mastermind::ArgParse

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/mastermind/arg_parse.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = ARGV) ⇒ ArgParse

Returns a new instance of ArgParse.

Parameters:

  • arguments (Array<String>) (defaults to: ARGV)

    the arguements to parse



35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#patternRegexp (readonly)

Returns the pattern to use when filtering plans for display.

Returns:

  • (Regexp)

    the pattern to use when filtering plans for display



8
9
10
# File 'lib/cli/mastermind/arg_parse.rb', line 8

def pattern
  @pattern
end

#plan_argumentsArray<String> (readonly)

Returns additional command line arguements passed into the executed plan.

Returns:

  • (Array<String>)

    additional command line arguements passed into the executed plan



11
12
13
# File 'lib/cli/mastermind/arg_parse.rb', line 11

def plan_arguments
  @plan_arguments
end

Class Method Details

.add_option(*args, &block) ⇒ Void

Adds arbitrary options to the argument parser.

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

Parameters:

  • args

    arguments passed directly to OptionParser#on

  • block (Proc)

    block passed as the handler for the above arguments

Returns:

  • (Void)


28
29
30
31
# File 'lib/cli/mastermind/arg_parse.rb', line 28

def add_option(*args, &block)
  @extra_options ||= []
  @extra_options << [args, block]
end

.extra_optionsArray

Returns a set of extra options added to the argument parser.

Returns:

  • (Array)

    a set of extra options added to the argument parser

See Also:



16
17
18
# File 'lib/cli/mastermind/arg_parse.rb', line 16

def extra_options
  @extra_options ||= []
end

Instance Method Details

#ask?Boolean

Returns if the user should be asked for confirmation prior to plan execution.

Returns:

  • (Boolean)

    if the user should be asked for confirmation prior to plan execution



119
120
121
# File 'lib/cli/mastermind/arg_parse.rb', line 119

def ask?
  @ask
end

#display_plans?Boolean

Returns if the user has requested plan display.

Returns:

  • (Boolean)

    if the user has requested plan display



97
98
99
# File 'lib/cli/mastermind/arg_parse.rb', line 97

def display_plans?
  !@pattern.nil?
end

#display_ui?Boolean

Returns if the UI is displayed.

Returns:

  • (Boolean)

    if the UI is displayed



114
115
116
# File 'lib/cli/mastermind/arg_parse.rb', line 114

def display_ui?
  @display_ui
end

#do_command_expansion!(config) ⇒ Void

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.

Parameters:

  • config (Configuration)

    the configuration object to use when expanding user aliases

Returns:

  • (Void)


75
76
77
78
79
80
81
82
83
84
85
86
# 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?Boolean

Returns if the user requested their configuration be displayed.

Returns:

  • (Boolean)

    if the user requested their configuration be displayed



124
125
126
# File 'lib/cli/mastermind/arg_parse.rb', line 124

def dump_config?
  @show_config
end

#get_next_plan_nameString

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

Returns:

  • (String)

    the name of the next plan in the list of arguments



109
110
111
# File 'lib/cli/mastermind/arg_parse.rb', line 109

def get_next_plan_name
  @mastermind_arguments.shift
end

#has_additional_plan_names?Boolean

Returns if additional plan names exist in mastermind’s arguments.

Returns:

  • (Boolean)

    if additional plan names exist in mastermind’s arguments



102
103
104
# File 'lib/cli/mastermind/arg_parse.rb', line 102

def has_additional_plan_names?
  @mastermind_arguments.any?
end

#insert_base_plan!(base_plan) ⇒ Object

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

Parameters:

  • base_plan (String)

    the base plan to add to the beginning of the arguments



91
92
93
94
# 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

#parserOptionParser

Returns the parser to process command line arguments with.

Returns:

  • (OptionParser)

    the parser to process command line arguments with



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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?Boolean

Returns if callable attributes should be resolved prior to being displayed.

Returns:

  • (Boolean)

    if callable attributes should be resolved prior to being displayed



129
130
131
# File 'lib/cli/mastermind/arg_parse.rb', line 129

def resolve_callable_attributes?
  @call_blocks
end