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.
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.
-
.extra_options ⇒ Array
A set of extra options added 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.
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
#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.
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_options ⇒ Array
Returns a set of extra options added to the argument parser.
16 17 18 |
# File 'lib/cli/mastermind/arg_parse.rb', line 16 def @extra_options ||= [] end |
Instance Method Details
#ask? ⇒ Boolean
Returns 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.
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.
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.
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| (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.
124 125 126 |
# File 'lib/cli/mastermind/arg_parse.rb', line 124 def dump_config? @show_config end |
#get_next_plan_name ⇒ String
Removes and returns the plan name at the beginning of the argument list.
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.
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
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 |
#parser ⇒ OptionParser
Returns 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. = '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.
129 130 131 |
# File 'lib/cli/mastermind/arg_parse.rb', line 129 def resolve_callable_attributes? @call_blocks end |