Class: Homebrew::CLI::Parser
- Inherits:
-
Object
- Object
- Homebrew::CLI::Parser
- Defined in:
- Library/Homebrew/cli/parser.rb
Instance Attribute Summary collapse
-
#hide_from_man_page ⇒ Object
readonly
Returns the value of attribute hide_from_man_page.
-
#processed_options ⇒ Object
readonly
Returns the value of attribute processed_options.
Class Method Summary collapse
Instance Method Summary collapse
- #cask_options ⇒ Object
- #comma_array(name, description: nil) ⇒ Object
- #conflicts(*options) ⇒ Object
- #env?(env) ⇒ Boolean
- #flag(*names, description: nil, required_for: nil, depends_on: nil) ⇒ Object
- #formula_options ⇒ Object
- #generate_help_text ⇒ Object
- #hide_from_man_page! ⇒ Object
-
#initialize(&block) ⇒ Parser
constructor
A new instance of Parser.
- #max_named(count) ⇒ Object
- #min_named(count_or_type) ⇒ Object
- #name_to_option(name) ⇒ Object
- #named(count_or_type) ⇒ Object
- #option_to_description(*names) ⇒ Object
- #option_to_name(option) ⇒ Object
- #parse(argv = ARGV.freeze, ignore_invalid_options: false) ⇒ Object
- #parse_remaining(argv, ignore_invalid_options: false) ⇒ Object
- #switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil, method: :on) ⇒ Object (also: #switch_option)
- #usage_banner(text) ⇒ Object
- #usage_banner_text ⇒ Object
Constructor Details
#initialize(&block) ⇒ Parser
Returns a new instance of Parser.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'Library/Homebrew/cli/parser.rb', line 106 def initialize(&block) @parser = OptionParser.new @parser.summary_indent = " " * 2 # Disable default handling of `--version` switch. @parser.base.long.delete("version") # Disable default handling of `--help` switch. @parser.base.long.delete("help") @args = Homebrew::CLI::Args.new @constraints = [] @conflicts = [] @switch_sources = {} @processed_options = [] @max_named_args = nil @min_named_args = nil @min_named_type = nil @hide_from_man_page = false @formula_options = false self.class..each do |short, long, desc| switch short, long, description: desc, env: option_to_name(long), method: :on_tail end instance_eval(&block) if block_given? end |
Instance Attribute Details
#hide_from_man_page ⇒ Object (readonly)
Returns the value of attribute hide_from_man_page
17 18 19 |
# File 'Library/Homebrew/cli/parser.rb', line 17 def hide_from_man_page @hide_from_man_page end |
#processed_options ⇒ Object (readonly)
Returns the value of attribute processed_options
17 18 19 |
# File 'Library/Homebrew/cli/parser.rb', line 17 def @processed_options end |
Class Method Details
.from_cmd_path(cmd_path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'Library/Homebrew/cli/parser.rb', line 19 def self.from_cmd_path(cmd_path) cmd_args_method_name = Commands.args_method_name(cmd_path) begin Homebrew.send(cmd_args_method_name) if require?(cmd_path) rescue NoMethodError => e raise if e.name != cmd_args_method_name nil end end |
.global_cask_options ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'Library/Homebrew/cli/parser.rb', line 31 def self. [ [:flag, "--appdir=", { description: "Target location for Applications. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:appdir]}`", }], [:flag, "--colorpickerdir=", { description: "Target location for Color Pickers. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:colorpickerdir]}`", }], [:flag, "--prefpanedir=", { description: "Target location for Preference Panes. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:prefpanedir]}`", }], [:flag, "--qlplugindir=", { description: "Target location for QuickLook Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:qlplugindir]}`", }], [:flag, "--mdimporterdir=", { description: "Target location for Spotlight Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:mdimporterdir]}`", }], [:flag, "--dictionarydir=", { description: "Target location for Dictionaries. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:dictionarydir]}`", }], [:flag, "--fontdir=", { description: "Target location for Fonts. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:fontdir]}`", }], [:flag, "--servicedir=", { description: "Target location for Services. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:servicedir]}`", }], [:flag, "--input_methoddir=", { description: "Target location for Input Methods. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:input_methoddir]}`", }], [:flag, "--internet_plugindir=", { description: "Target location for Internet Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:internet_plugindir]}`", }], [:flag, "--audio_unit_plugindir=", { description: "Target location for Audio Unit Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:audio_unit_plugindir]}`", }], [:flag, "--vst_plugindir=", { description: "Target location for VST Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:vst_plugindir]}`", }], [:flag, "--vst3_plugindir=", { description: "Target location for VST3 Plugins. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:vst3_plugindir]}`", }], [:flag, "--screen_saverdir=", { description: "Target location for Screen Savers. " \ "Default: `#{Cask::Config::DEFAULT_DIRS[:screen_saverdir]}`", }], [:comma_array, "--language", { description: "Set language of the Cask to install. The first matching " \ "language is used, otherwise the default language on the Cask. " \ "The default value is the `language of your system`", }], ] end |
.global_options ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'Library/Homebrew/cli/parser.rb', line 97 def self. [ ["-d", "--debug", "Display any debugging information."], ["-q", "--quiet", "Suppress any warnings."], ["-v", "--verbose", "Make some output more verbose."], ["-h", "--help", "Show this message."], ] end |
Instance Method Details
#cask_options ⇒ Object
327 328 329 330 331 332 |
# File 'Library/Homebrew/cli/parser.rb', line 327 def self.class..each do |method, *args, **| send(method, *args, **) conflicts "--formula", args.last end end |
#comma_array(name, description: nil) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'Library/Homebrew/cli/parser.rb', line 176 def comma_array(name, description: nil) name = name.chomp "=" description = option_to_description(name) if description.nil? process_option(name, description) @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list| @args[option_to_name(name)] = list end end |
#conflicts(*options) ⇒ Object
205 206 207 |
# File 'Library/Homebrew/cli/parser.rb', line 205 def conflicts(*) @conflicts << .map { |option| option_to_name(option) } end |
#env?(env) ⇒ Boolean
161 162 163 164 165 |
# File 'Library/Homebrew/cli/parser.rb', line 161 def env?(env) return if env.blank? Homebrew::EnvConfig.try(:"#{env}?") end |
#flag(*names, description: nil, required_for: nil, depends_on: nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'Library/Homebrew/cli/parser.rb', line 185 def flag(*names, description: nil, required_for: nil, depends_on: nil) required = if names.any? { |name| name.end_with? "=" } OptionParser::REQUIRED_ARGUMENT else OptionParser::OPTIONAL_ARGUMENT end names.map! { |name| name.chomp "=" } description = option_to_description(*names) if description.nil? process_option(*names, description) @parser.on(*names, *wrap_option_desc(description), required) do |option_value| names.each do |name| @args[option_to_name(name)] = option_value end end names.each do |name| set_constraints(name, required_for: required_for, depends_on: depends_on) end end |
#formula_options ⇒ Object
334 335 336 |
# File 'Library/Homebrew/cli/parser.rb', line 334 def @formula_options = true end |
#generate_help_text ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 |
# File 'Library/Homebrew/cli/parser.rb', line 315 def generate_help_text Formatter.wrap( @parser.to_s.gsub(/^ - (`[^`]+`\s+)/, " \\1"), # Remove `-` from `cask` subcommand listing. COMMAND_DESC_WIDTH, ) .sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ") .gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}") .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) } .gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}") .gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}") end |
#hide_from_man_page! ⇒ Object
370 371 372 |
# File 'Library/Homebrew/cli/parser.rb', line 370 def hide_from_man_page! @hide_from_man_page = true end |
#max_named(count) ⇒ Object
338 339 340 341 342 |
# File 'Library/Homebrew/cli/parser.rb', line 338 def max_named(count) raise TypeError, "Unsupported type #{count.class.name} for max_named" unless count.is_a?(Integer) @max_named_args = count end |
#min_named(count_or_type) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'Library/Homebrew/cli/parser.rb', line 344 def min_named(count_or_type) case count_or_type when Integer @min_named_args = count_or_type @min_named_type = nil when Symbol @min_named_args = 1 @min_named_type = count_or_type else raise TypeError, "Unsupported type #{count_or_type.class.name} for min_named" end end |
#name_to_option(name) ⇒ Object
215 216 217 218 219 220 221 |
# File 'Library/Homebrew/cli/parser.rb', line 215 def name_to_option(name) if name.length == 1 "-#{name}" else "--#{name.tr("_", "-")}" end end |
#named(count_or_type) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'Library/Homebrew/cli/parser.rb', line 357 def named(count_or_type) case count_or_type when Integer @max_named_args = @min_named_args = count_or_type @min_named_type = nil when Symbol @max_named_args = @min_named_args = 1 @min_named_type = count_or_type else raise TypeError, "Unsupported type #{count_or_type.class.name} for named" end end |
#option_to_description(*names) ⇒ Object
223 224 225 |
# File 'Library/Homebrew/cli/parser.rb', line 223 def option_to_description(*names) names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max end |
#option_to_name(option) ⇒ Object
209 210 211 212 213 |
# File 'Library/Homebrew/cli/parser.rb', line 209 def option_to_name(option) option.sub(/\A--?(\[no-\])?/, "") .tr("-", "_") .delete("=") end |
#parse(argv = ARGV.freeze, ignore_invalid_options: false) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'Library/Homebrew/cli/parser.rb', line 261 def parse(argv = ARGV.freeze, ignore_invalid_options: false) raise "Arguments were already parsed!" if @args_parsed # If we accept formula options, parse once allowing invalid options # so we can get the remaining list containing formula names. if @formula_options remaining, = parse_remaining(argv, ignore_invalid_options: true) argv = [*remaining, "--", *] formulae(argv).each do |f| next if f..empty? f..each do |o| name = o.flag description = "`#{f.name}`: #{o.description}" if name.end_with? "=" flag name, description: description else switch name, description: description end conflicts "--cask", name end end end remaining, = parse_remaining(argv, ignore_invalid_options: ) named_args = if [] else remaining + end unless check_constraint_violations check_named_args(named_args) end @args.freeze_named_args!(named_args) @args.freeze_remaining_args!(.empty? ? remaining : [*remaining, "--", ]) @args.(@processed_options) @args_parsed = true if ! && @args.help? puts generate_help_text exit end @args end |
#parse_remaining(argv, ignore_invalid_options: false) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'Library/Homebrew/cli/parser.rb', line 227 def parse_remaining(argv, ignore_invalid_options: false) i = 0 remaining = [] argv, = (argv) while i < argv.count begin begin arg = argv[i] remaining << arg unless @parser.parse([arg]).empty? rescue OptionParser::MissingArgument raise if i + 1 >= argv.count args = argv[i..(i + 1)] @parser.parse(args) i += 1 end rescue OptionParser::InvalidOption if remaining << arg else $stderr.puts generate_help_text raise end end i += 1 end [remaining, ] end |
#switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil, method: :on) ⇒ Object Also known as: switch_option
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'Library/Homebrew/cli/parser.rb', line 136 def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil, method: :on) global_switch = names.first.is_a?(Symbol) return if global_switch description = option_to_description(*names) if description.nil? process_option(*names, description) @parser.public_send(method, *names, *wrap_option_desc(description)) do |value| value = if names.any? { |name| name.start_with?("--[no-]") } value else true end set_switch(*names, value: value, from: :args) end names.each do |name| set_constraints(name, required_for: required_for, depends_on: depends_on) end env_value = env?(env) set_switch(*names, value: env_value, from: :env) unless env_value.nil? end |
#usage_banner(text) ⇒ Object
167 168 169 |
# File 'Library/Homebrew/cli/parser.rb', line 167 def (text) @parser. = "#{text}\n" end |
#usage_banner_text ⇒ Object
171 172 173 174 |
# File 'Library/Homebrew/cli/parser.rb', line 171 def @parser. .gsub(/^ - (`[^`]+`)\s+/, "\n- \\1 \n ") # Format `cask` subcommands as MarkDown list. end |