Class: WifiWand::CommandLineParser

Inherits:
Object
  • Object
show all
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

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

#parseObject



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
  options = CommandLineOptions.new
  specified_invocation_options = []
  command_options = {}
  help_requested = false
  prepend_env_options(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|
      specified_invocation_options << :verbose
      options.verbose = value
    end

    parser.on(
      '-u',
      '--utc BOOLEAN',
      TrueClass,
      'Use UTC for timestamps (default: false, for local time)'
    ) do |value|
      specified_invocation_options << :utc
      options.utc = value
    end

    parser.on('-o', '--output-format FORMAT', '--output_format FORMAT', 'Format output data') do |value|
      specified_invocation_options << :output_format
      options.output_format = resolve_format_code(value)
      options.post_processor = formatter_for(value)
    end

    parser.on('-p', '--wifi-interface INTERFACE', 'WiFi interface name') do |value|
      specified_invocation_options << :wifi_interface
      options.wifi_interface = value
    end

    parser.on('-h', '--help', 'Show help') do
      specified_invocation_options << :help
      help_requested = true
    end

    parser.on('-V', '--version', 'Show version') do
      specified_invocation_options << :version
      options.version_requested = true
    end

    add_command_options(parser, selected_command, command_options)
  end
  parse_options!(parser, args, selected_command)
  options.specified_invocation_options = specified_invocation_options.uniq
  options.invocation_option_sources = invocation_option_sources_for(
    options.specified_invocation_options,
    cli_args: raw_cli_args
  )
  selected_command_argv = command_argv_for(args, selected_command)
  unless help_or_version?(help_requested, options)
    validate_command_options!(selected_command, options, command_options, selected_command_argv)
  end

  # Help and version are handled as dedicated top-level flags.
  if help_requested
    options.help_requested = true
    options.argv = help_argv_for(selected_command)
  else
    options.argv = selected_command_argv
  end
  options.command_options = command_options
  options.raw_argv = raw_cli_args.dup
  options.wifi_wand_opts_env = @env['WIFIWAND_OPTS']

  options
end