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.

Class Attribute Summary collapse

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



33
34
35
36
37
38
39
40
41
# File 'lib/cli/mastermind/arg_parse.rb', line 33

def initialize(arguments=ARGV)
  @initial_arguments = arguments
  @ask = true
  @display_ui = true
  @show_config = false
  @call_blocks = false

  parse_arguments
end

Class Attribute Details

.extra_optionsArray (readonly)

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 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)


26
27
28
29
# File 'lib/cli/mastermind/arg_parse.rb', line 26

def add_option(*args, &block)
  @extra_options ||= []
  @extra_options << [args, block]
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



117
118
119
# File 'lib/cli/mastermind/arg_parse.rb', line 117

def ask?
  @ask
end

#display_plans?Boolean

Returns if the user has requested plan display.

Returns:

  • (Boolean)

    if the user has requested plan display



95
96
97
# File 'lib/cli/mastermind/arg_parse.rb', line 95

def display_plans?
  !@pattern.nil?
end

#display_ui?Boolean

Returns if the UI is displayed.

Returns:

  • (Boolean)

    if the UI is displayed



112
113
114
# File 'lib/cli/mastermind/arg_parse.rb', line 112

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)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cli/mastermind/arg_parse.rb', line 73

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



122
123
124
# File 'lib/cli/mastermind/arg_parse.rb', line 122

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



107
108
109
# File 'lib/cli/mastermind/arg_parse.rb', line 107

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



100
101
102
# File 'lib/cli/mastermind/arg_parse.rb', line 100

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



89
90
91
92
# File 'lib/cli/mastermind/arg_parse.rb', line 89

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



132
133
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
# File 'lib/cli/mastermind/arg_parse.rb', line 132

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



127
128
129
# File 'lib/cli/mastermind/arg_parse.rb', line 127

def resolve_callable_attributes?
  @call_blocks
end