Class: CLI::Mastermind::ArgParse
- Inherits:
-
Object
- Object
- CLI::Mastermind::ArgParse
- 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
-
.extra_options ⇒ Array
readonly
A set of extra options added to the argument parser.
Instance Attribute Summary collapse
-
#pattern ⇒ Regexp
readonly
The pattern to use when filtering plans for display.
-
#plan_arguments ⇒ Array<String>
readonly
Additional command line arguements passed into the executed plan.
Class Method Summary collapse
-
.add_option(*args, &block) ⇒ Void
Adds arbitrary options to the argument parser.
Instance Method Summary collapse
-
#ask? ⇒ Boolean
If the user should be asked for confirmation prior to plan execution.
-
#display_plans? ⇒ Boolean
If the user has requested plan display.
-
#display_ui? ⇒ Boolean
If the UI is displayed.
-
#do_command_expansion!(config) ⇒ Void
Uses configured user aliases to perform command expansion.
-
#dump_config? ⇒ Boolean
If the user requested their configuration be displayed.
-
#get_next_plan_name ⇒ String
Removes and returns the plan name at the beginning of the argument list.
-
#has_additional_plan_names? ⇒ Boolean
If additional plan names exist in mastermind’s arguments.
-
#initialize(arguments = ARGV) ⇒ ArgParse
constructor
A new instance of ArgParse.
-
#insert_base_plan!(base_plan) ⇒ Object
Adds the given base plan to the beginning of the arguments array.
-
#parser ⇒ OptionParser
The parser to process command line arguments with.
-
#resolve_callable_attributes? ⇒ Boolean
If callable attributes should be resolved prior to being displayed.
Constructor Details
#initialize(arguments = ARGV) ⇒ ArgParse
Returns a new instance of ArgParse.
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_options ⇒ Array (readonly)
Returns a set of extra options added to the argument parser.
16 17 18 |
# File 'lib/cli/mastermind/arg_parse.rb', line 16 def end |
Instance Attribute Details
#pattern ⇒ Regexp (readonly)
Returns 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_arguments ⇒ Array<String> (readonly)
Returns 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.
26 27 28 29 |
# File 'lib/cli/mastermind/arg_parse.rb', line 26 def add_option(*args, &block) ||= [] << [args, block] end |
Instance Method Details
#ask? ⇒ Boolean
Returns 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.
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.
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.
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| (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.
122 123 124 |
# File 'lib/cli/mastermind/arg_parse.rb', line 122 def dump_config? @show_config end |
#get_next_plan_name ⇒ String
Removes and returns the plan name at the beginning of the argument list.
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.
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
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 |
#parser ⇒ OptionParser
Returns 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. = '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..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.
127 128 129 |
# File 'lib/cli/mastermind/arg_parse.rb', line 127 def resolve_callable_attributes? @call_blocks end |