Class: WifiWand::CommandLineParser
- Inherits:
-
Object
- Object
- WifiWand::CommandLineParser
- Includes:
- WifiWand::Commands::Registry, StringPredicates
- Defined in:
- lib/wifi_wand/command_line_parser.rb
Constant Summary collapse
- FORMATTERS =
{ 'a' => ->(object) do require 'amazing_print' object.ai(plain: false) end, 'i' => ->(object) { object.inspect }, 'j' => ->(object) { object.to_json }, 'J' => ->(object) { JSON.pretty_generate(object) }, 'p' => ->(object) do sio = StringIO.new sio.puts(object) sio.string end, 'P' => ->(object) do sio = StringIO.new PP.pp(object, sio) sio.string end, 'y' => ->(object) { object.to_yaml }, }.freeze
- FORMAT_LONG_NAMES =
{ 'amazing_print' => 'a', 'inspect' => 'i', 'json' => 'j', 'pretty_json' => 'J', 'puts' => 'p', 'pretty_print' => 'P', 'yaml' => 'y', }.freeze
- VALUE_TAKING_INVOCATION_OPTIONS =
%w[ -v --verbose -u --utc -o --output-format --output_format -p --wifi-interface ].freeze
- INVOCATION_OPTION_ALIASES =
{ help: %w[-h --help], output_format: %w[-o --output-format --output_format], utc: %w[-u --utc], verbose: %w[-v --verbose], version: %w[-V --version], wifi_interface: %w[-p --wifi-interface], }.freeze
Instance Method Summary collapse
-
#initialize(argv, env, err_stream) ⇒ CommandLineParser
constructor
A new instance of CommandLineParser.
- #parse ⇒ Object
Methods included from StringPredicates
string_nil_or_blank?, string_nil_or_empty?
Methods included from WifiWand::Commands::Registry
#attempt_command_action, #commands, #find_command, #find_command_action, #resolve_command
Constructor Details
#initialize(argv, env, err_stream) ⇒ CommandLineParser
Returns a new instance of CommandLineParser.
61 62 63 64 65 |
# File 'lib/wifi_wand/command_line_parser.rb', line 61 def initialize(argv, env, err_stream) @argv = argv @env = env @err_stream = err_stream end |
Instance Method Details
#parse ⇒ Object
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 96 97 98 99 100 101 102 103 104 105 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 135 136 137 138 139 140 |
# File 'lib/wifi_wand/command_line_parser.rb', line 67 def parse raw_cli_args = Array(@argv) args = raw_cli_args.dup = CommandLineOptions.new = [] = {} help_requested = false (args) selected_command = selected_command_from(args) normalize_command_option_args!(args, selected_command) parser = OptionParser.new do |parser| parser.on('-v', '--verbose BOOLEAN', TrueClass, 'Run verbosely') do |value| << :verbose .verbose = value end parser.on( '-u', '--utc BOOLEAN', TrueClass, 'Use UTC for timestamps (default: false, for local time)' ) do |value| << :utc .utc = value end parser.on('-o', '--output-format FORMAT', '--output_format FORMAT', 'Format output data') do |value| << :output_format .output_format = resolve_format_code(value) .post_processor = formatter_for(value) end parser.on('-p', '--wifi-interface INTERFACE', 'WiFi interface name') do |value| << :wifi_interface .wifi_interface = value end parser.on('-h', '--help', 'Show help') do << :help help_requested = true end parser.on('-V', '--version', 'Show version') do << :version .version_requested = true end (parser, selected_command, ) end (parser, args, selected_command) . = .uniq .invocation_option_sources = invocation_option_sources_for( ., cli_args: raw_cli_args ) selected_command_argv = command_argv_for(args, selected_command) unless help_or_version?(help_requested, ) (selected_command, , , selected_command_argv) end # Help and version are handled as dedicated top-level flags. if help_requested .help_requested = true .argv = help_argv_for(selected_command) else .argv = selected_command_argv end . = .raw_argv = raw_cli_args.dup .wifi_wand_opts_env = @env['WIFIWAND_OPTS'] end |