Class: UserChoices::CommandLineSource
- Inherits:
-
AbstractSource
- Object
- Hash
- AbstractSource
- UserChoices::CommandLineSource
- Defined in:
- lib/user-choices/command-line-source.rb
Overview
Treat the command line (including the arguments) as a source of choices.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from AbstractSource
Instance Method Summary collapse
-
#add_help_line(string) ⇒ Object
Add a single line composed of string to the current position in the help output.
-
#adjust(all_choices) ⇒ Object
:nodoc:.
-
#apply(all_choice_conversions) ⇒ Object
:nodoc:.
-
#deep_copy(conversions) ⇒ Object
:nodoc:.
-
#exit_upon_error ⇒ Object
:nodoc:.
-
#fill ⇒ Object
Public for testing.
-
#help ⇒ Object
Called in the case of command-line error or explicit request (–help) to print usage information.
-
#help_banner(banner, *more) ⇒ Object
:nodoc:.
-
#initialize ⇒ CommandLineSource
constructor
A new instance of CommandLineSource.
-
#source ⇒ Object
:nodoc:.
-
#usage(*usage_lines) ⇒ Object
The usage_lines will be used to produce the output from –help (or on error).
-
#use_strategy(choice, strategy) ⇒ Object
:nodoc:.
-
#uses_arg(choice) ⇒ Object
The single argument required argument is turned into a string indexed by choice.
-
#uses_arglist(choice) ⇒ Object
Bundle up all non-option and non-switch arguments into an array of strings indexed by choice.
-
#uses_option(choice, *args) ⇒ Object
Describes how a particular choice is represented on the command line.
-
#uses_optional_arg(choice) ⇒ Object
If a single argument is present, it (as a string) is the value of choice.
-
#uses_switch(choice, *args) ⇒ Object
A switch is an option that doesn’t take a value.
Methods inherited from AbstractSource
Constructor Details
#initialize ⇒ CommandLineSource
Returns a new instance of CommandLineSource.
13 14 15 16 17 |
# File 'lib/user-choices/command-line-source.rb', line 13 def initialize super @parser = OptionParser.new @arglist_handler = NoArguments.new(self) end |
Instance Method Details
#add_help_line(string) ⇒ Object
Add a single line composed of string to the current position in the help output.
96 97 98 |
# File 'lib/user-choices/command-line-source.rb', line 96 def add_help_line(string) @parser.separator(string) end |
#adjust(all_choices) ⇒ Object
:nodoc:
120 121 122 123 124 |
# File 'lib/user-choices/command-line-source.rb', line 120 def adjust(all_choices) # :nodoc: exit_upon_error do @arglist_handler.adjust(all_choices) end end |
#apply(all_choice_conversions) ⇒ Object
:nodoc:
110 111 112 113 114 115 116 117 118 |
# File 'lib/user-choices/command-line-source.rb', line 110 def apply(all_choice_conversions) # :nodoc: safely_modifiable_conversions = deep_copy(all_choice_conversions) @arglist_handler.claim_conversions(safely_modifiable_conversions) exit_upon_error do @arglist_handler.apply_claimed_conversions super(safely_modifiable_conversions) end end |
#deep_copy(conversions) ⇒ Object
:nodoc:
140 141 142 143 144 145 |
# File 'lib/user-choices/command-line-source.rb', line 140 def deep_copy(conversions) # :nodoc: copy = conversions.dup copy.each do |k, v| copy[k] = v.collect { |conversion| conversion.dup } end end |
#exit_upon_error ⇒ Object
:nodoc:
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/user-choices/command-line-source.rb', line 156 def exit_upon_error # :nodoc: begin yield rescue SystemExit raise rescue Exception => ex = if ex..has_exact_prefix?(error_prefix) ex. else error_prefix + ex. end $stderr.puts() help end end |
#fill ⇒ Object
Public for testing.
103 104 105 106 107 108 |
# File 'lib/user-choices/command-line-source.rb', line 103 def fill # :nodoc: exit_upon_error do remainder = @parser.parse(ARGV) @arglist_handler.fill(remainder) end end |
#help ⇒ Object
Called in the case of command-line error or explicit request (–help) to print usage information.
32 33 34 35 |
# File 'lib/user-choices/command-line-source.rb', line 32 def help $stderr.puts @parser exit end |
#help_banner(banner, *more) ⇒ Object
:nodoc:
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/user-choices/command-line-source.rb', line 126 def (, *more) # :nodoc: @parser. = more.each do | line | add_help_line(line) end add_help_line '' add_help_line 'Options:' @parser.on_tail("-?", "-h", "--help", "Show this message.") do help end end |
#source ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/user-choices/command-line-source.rb', line 19 def source # :nodoc: "the command line" end |
#usage(*usage_lines) ⇒ Object
The usage_lines will be used to produce the output from –help (or on error).
25 26 27 28 |
# File 'lib/user-choices/command-line-source.rb', line 25 def usage(*usage_lines) (*usage_lines) self end |
#use_strategy(choice, strategy) ⇒ Object
:nodoc:
147 148 149 150 151 152 153 |
# File 'lib/user-choices/command-line-source.rb', line 147 def use_strategy(choice, strategy) # :nodoc: # The argument list choice probably does not need a name. # (Currently, the name is unused.) But I'll give it one, just # in case, and for debugging. external_names[choice] = "the argument list" @arglist_handler = strategy.new(self, choice) end |
#uses_arg(choice) ⇒ Object
The single argument required argument is turned into a string indexed by choice. Any other case is an error.
83 84 85 |
# File 'lib/user-choices/command-line-source.rb', line 83 def uses_arg(choice) use_strategy(choice, OneRequiredArg) end |
#uses_arglist(choice) ⇒ Object
Bundle up all non-option and non-switch arguments into an array of strings indexed by choice.
77 78 79 |
# File 'lib/user-choices/command-line-source.rb', line 77 def uses_arglist(choice) use_strategy(choice, ArbitraryArglist) end |
#uses_option(choice, *args) ⇒ Object
Describes how a particular choice is represented on the command line. The args are passed to OptionParser. Each arg will either describe one variant of option (such as "-s" or "--show VALUE") or is a line of help text about the option (multiple lines are allowed).
If the option takes an array of values, separate the values by commas: –files a,b,c There’s currently no way to escape a comma and no cleverness about quotes.
49 50 51 52 53 54 |
# File 'lib/user-choices/command-line-source.rb', line 49 def uses_option(choice, *args) external_names[choice] = '--' + extract_switch_raw_name(args) @parser.on(*args) do | value | self[choice] = value end end |
#uses_optional_arg(choice) ⇒ Object
If a single argument is present, it (as a string) is the value of choice. If no argument is present, choice has no value. Any other case is an error.
90 91 92 |
# File 'lib/user-choices/command-line-source.rb', line 90 def uses_optional_arg(choice) use_strategy(choice, OneOptionalArg) end |
#uses_switch(choice, *args) ⇒ Object
A switch is an option that doesn’t take a value. A switch described as "--switch" has these effects:
-
If it is not given, the choice is the default value or is not present in the hash that holds all the choices.
-
If it is given as
--switch, the choice has the value"true". (If the choice was described in ChoicesBuilder#add_choice as having a:type => :boolean, that value is converted from a string totrue.) -
If it is given as
--no-switch, the choice has the value"false".
66 67 68 69 70 71 72 73 |
# File 'lib/user-choices/command-line-source.rb', line 66 def uses_switch(choice, *args) external_name = extract_switch_raw_name(args) external_names[choice] = '--' + external_name args = change_name_to_switch(external_name, args) @parser.on(*args) do | value | self[choice] = value.to_s end end |