Class: Sshez::Parser
- Inherits:
-
Object
- Object
- Sshez::Parser
- 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
-
#listener ⇒ Object
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.
Instance Method Summary collapse
-
#common_options(opts, options) ⇒ Object
Returns the standard options.
-
#init_options_parser(options) ⇒ Object
Initates an OptionParser with all of the possible options and how to handle them.
-
#initialize(listener) ⇒ Parser
constructor
Must have the methods mentioned above.
-
#options_for_add(opts, options) ⇒ Object
Returns the options specifice to the add command only.
-
#parse(args) ⇒ Object
Return a structure describing the command and its options.
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
#listener ⇒ Object (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 (opts, ) opts.separator '' opts.separator 'Common options:' # Another typical switch to print the version. opts.on('-v', '--version', 'Show version') do PRINTER.print Sshez.version .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 .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 () OptionParser.new do |opts| opts. = "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:' (opts, ) # signals that we are in testing mode opts.on('-t', '--test', 'Writes nothing') {.test = true} (opts, ) 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 (opts, ) opts.on('-p', '--port PORT', 'Specify a port') do |port| .file_content.port_text = " Port #{port}\n" end opts.on('-i', '--identity_file [key]', 'Add identity') do |key_path| .file_content.identity_file_text = " IdentityFile #{key_path}\n" end opts.on('-b', '--batch_mode', 'Batch Mode') do .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] = OpenStruct.new(file_content: OpenStruct.new) ().parse!(args) args.delete_at(0) return no_command_supplied unless(command && !.halt) command.args = args return parsing_succeeded(command, ) if command.valid?(args) parsing_failed(command) end |