Class: Sshez::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sshez/parser.rb

Overview

handles parsing the arguments to meaningful actions

Parser.new(listener).parse(args)

Constant Summary collapse

PRINTER =
PrintingManager.instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listener) ⇒ Parser

Must have the methods mentioned above



19
20
21
# File 'lib/sshez/parser.rb', line 19

def initialize(listener)
  @listener = listener
end

Instance Attribute Details

#listenerObject (readonly)

to create an instance pass any Struct that handles the following methods

  • :start_exec(Command, OpenStruct(options))

  • :argument_error(Command)

  • :done_with_no_guarantee



14
15
16
# File 'lib/sshez/parser.rb', line 14

def listener
  @listener
end

Instance Method Details

#common_options(opts, options) ⇒ Object

Returns the standard options



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sshez/parser.rb', line 86

def common_options(opts, options)
  opts.separator ''
  opts.separator 'Common options:'
  # Another typical switch to print the version.
  opts.on('-v', '--version', 'Show version') do
    PRINTER.print Sshez.version
    options.halt = true
  end
  opts.on('-z', '--verbose', 'Verbose Output') do
    PRINTER.verbose!
  end
  # Prints everything
  opts.on_tail('-h', '--help', 'Show this message') do
    PRINTER.print opts
    options.halt = true
  end
end

#init_options_parser(options) ⇒ Object

Initates an OptionParser with all of the possible options and how to handle them



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sshez/parser.rb', line 47

def init_options_parser(options)
  OptionParser.new do |opts|
    opts.banner = "Usage:\n"\
    "\tsshez add <alias> (role@host) [options]\n"\
    "\tsshez connect <alias>\n"\
    "\tsshez remove <alias>\n\tsshez list\n"\
    "\tsshez reset\n"
    opts.separator ''
    opts.separator 'Specific options:'
    options_for_add(opts, options)
    # signals that we are in testing mode
    opts.on('-t', '--test', 'Writes nothing') {options.test = true}
    common_options(opts, options)
  end # OptionParser.new
end

#options_for_add(opts, options) ⇒ Object

Returns the options specifice to the add command only



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sshez/parser.rb', line 66

def options_for_add(opts, options)
  opts.on('-p', '--port PORT',
          'Specify a port') do |port|
    options.file_content.port_text = "  Port #{port}\n"
  end

  opts.on('-i', '--identity_file [key]',
          'Add identity') do |key_path|
    options.file_content.identity_file_text =
      "  IdentityFile #{key_path}\n"
  end

  opts.on('-b', '--batch_mode', 'Batch Mode') do
    options.file_content.batch_mode_text = "  BatchMode yes\n"
  end
end

#parse(args) ⇒ Object

Return a structure describing the command and its options. prints help if no args supplied command is the first argument passed in the commandline

The options specified on the command line will be collected in options. options.file_content will contain what should be added in the next step to the config file



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sshez/parser.rb', line 31

def parse(args)
  args[0] ||= '-h'
  command = Command::ALL[args.first]
  options = OpenStruct.new(file_content: OpenStruct.new)
  init_options_parser(options).parse!(args)
  args.delete_at(0)
  return no_command_supplied unless(command && !options.halt)
  command.args = args
  return parsing_succeeded(command, options) if command.valid?(args)
  parsing_failed(command)
end