Class: CommandParser
- Inherits:
-
Object
- Object
- CommandParser
- Defined in:
- lib/mkit/client/command_parser.rb
Instance Method Summary collapse
- #_format(arg1, srg2) ⇒ Object
- #dict ⇒ Object
- #doIt(args) ⇒ Object
- #fill_cmd_args(args, argv, request, request_data) ⇒ Object
- #fill_flag_and_options_args(args, argv, request, request_data) ⇒ Object
- #fill_params_and_uri(arg, request) ⇒ Object
- #find_command(cmd) ⇒ Object
- #find_option_or_flag_index(arg, argv) ⇒ Object
- #format_arg_help_msg(arg) ⇒ Object
- #format_cmd_help_msg(command) ⇒ Object
- #help(cause: nil, cmd: nil) ⇒ Object
-
#initialize ⇒ CommandParser
constructor
A new instance of CommandParser.
- #parse(args) ⇒ Object
- #validate_command(command, request_data) ⇒ Object
Constructor Details
#initialize ⇒ CommandParser
Returns a new instance of CommandParser.
17 18 19 |
# File 'lib/mkit/client/command_parser.rb', line 17 def initialize @dict = YAML.safe_load(File.read("#{File.expand_path('..', __dir__)}/client/commands.yaml"), symbolize_names: true) end |
Instance Method Details
#_format(arg1, srg2) ⇒ Object
161 162 163 |
# File 'lib/mkit/client/command_parser.rb', line 161 def _format(arg1, srg2) format("%-12s %s\n", arg1, srg2) end |
#dict ⇒ Object
21 22 23 |
# File 'lib/mkit/client/command_parser.rb', line 21 def dict @dict end |
#doIt(args) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/mkit/client/command_parser.rb', line 146 def doIt(args) result = parse_args(args) puts result rescue InvalidParametersException => e help(cause: e) end |
#fill_cmd_args(args, argv, request, request_data) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mkit/client/command_parser.rb', line 72 def fill_cmd_args(args, argv, request, request_data) return if args.nil? # add to schema args.each do |arg| arg[:type] = 'value' unless arg[:type] end # flag and options (args, argv, request, request_data) idx = 0 args.each do |arg| if arg[:type].to_sym == :value request_data[arg[:name].to_sym] = argv[idx] fill_params_and_uri(arg, request) end idx += 1 end end |
#fill_flag_and_options_args(args, argv, request, request_data) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mkit/client/command_parser.rb', line 91 def (args, argv, request, request_data) # flags # checking flags first, avoids -n -f, with -f being the value of -n args.select { |arg| arg[:type].to_sym == :flag }.each do |arg| idx = find_option_or_flag_index(arg, argv) if idx fill_params_and_uri(arg, request) argv.delete_at(idx) request_data[arg[:name].to_sym] = arg[:param] end end # options args.select { |arg| arg[:type].to_sym == :option }.each do |arg| idx = find_option_or_flag_index(arg, argv) if idx fill_params_and_uri(arg, request) argv.delete_at(idx) request_data[arg[:name].to_sym] = argv[idx] end end end |
#fill_params_and_uri(arg, request) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/mkit/client/command_parser.rb', line 121 def fill_params_and_uri(arg, request) request[:uri] = request[:uri] + arg[:uri] unless arg[:uri].nil? unless arg[:param].nil? request[:params] ||= [] request[:params] << [ "#{arg[:name]}", "#{arg[:param]}"] end end |
#find_command(cmd) ⇒ Object
142 143 144 |
# File 'lib/mkit/client/command_parser.rb', line 142 def find_command(cmd) dict.select { |k| k[:cmd] == cmd }.first end |
#find_option_or_flag_index(arg, argv) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/mkit/client/command_parser.rb', line 113 def find_option_or_flag_index(arg, argv) idx = nil arg[:switch].each { | switch | idx ||= argv.index(switch) } idx end |
#format_arg_help_msg(arg) ⇒ Object
153 154 155 |
# File 'lib/mkit/client/command_parser.rb', line 153 def format_arg_help_msg(arg) _format(arg[:help][0], arg[:help][1]) end |
#format_cmd_help_msg(command) ⇒ Object
157 158 159 |
# File 'lib/mkit/client/command_parser.rb', line 157 def format_cmd_help_msg(command) _format(command[:cmd], command[:help]) end |
#help(cause: nil, cmd: nil) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mkit/client/command_parser.rb', line 165 def help(cause: nil, cmd: nil) msg = '' if cause.nil? my_cmd = cmd else msg += "MKIt: #{cause.message}\n" my_cmd = cause.command end if my_cmd.nil? msg += "\nUsage: mkit <command> [options]\n\n" msg += "Micro k8s on Ruby - a simple tool to mimic a (very) minimalistic k8 cluster\n\n" msg += "Commands:\n\n" dict.each do |c| msg += format_cmd_help_msg(c) end msg += "\n" msg += "Run ' mkit help <command>' for specific command information.\n\n" else # todo mkit help profile set msg += format("\nUsage: mkit %s %s\n\n", my_cmd[:cmd], my_cmd[:usage].nil? ? '' : my_cmd[:usage].join(' ')) msg += format("%s\n", my_cmd[:help]) if !my_cmd[:options].nil? || !my_cmd[:args].nil? msg += "\nOptions:\n" # command unless my_cmd[:options].nil? my_cmd[:options].each do |c| msg += format_cmd_help_msg(c) end end # args unless my_cmd[:args].nil? # values only first cmd_args = my_cmd[:args].select{ |arg| (arg[:type].nil? || arg[:type].to_sym == :value) && !arg[:help].nil?} cmd_args.each do |arg| msg += format_arg_help_msg(arg) end cmd_args = my_cmd[:args].select{ |arg| !arg[:type].nil? && (arg[:type].to_sym == :option || arg[:type].to_sym == :flag)} cmd_args.each do |arg| msg += format_arg_help_msg(arg) end end end msg += "\n" end puts msg exit 1 end |
#parse(args) ⇒ Object
25 26 27 28 29 30 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 |
# File 'lib/mkit/client/command_parser.rb', line 25 def parse(args) cmd = args[0] c = nil # short circuit for help if cmd == 'help' || args.empty? if args.size > 1 c = find_command(args[1]) raise InvalidParametersException, "'#{args[1]}' is not a valid help topic." if c.nil? end return help(cmd: c) else c = find_command(cmd) end raise InvalidParametersException, 'Command not found' if c.nil? command = c argv = args.dup argv.delete(cmd) request_data = {} request = command[:request] unless argv.empty? # options unless c[:options].nil? command = c[:options].select { |o| o[:cmd] == argv[0] }.first raise InvalidParametersException.new('Invalid parameters found.', c) if command.nil? || command.empty? argv.delete_at(0) request = command[:request] end fill_cmd_args(command[:args], argv, request, request_data) end raise InvalidParametersException.new('Invalid command or parameters.', c) if request.nil? validate_command(command, request_data) # { cmd: c[:cmd], request: request, data: request_data } end |
#validate_command(command, request_data) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mkit/client/command_parser.rb', line 129 def validate_command(command, request_data) return if command[:args].nil? command[:args].select { |arg| arg[:mandatory] == true }.each do |arg| if request_data[arg[:name].to_sym].nil? raise InvalidParametersException.new("Missing mandatory parameter: #{arg[:name]}", command) end end request_data.select{|key, value| value.nil? }.each do |key, value| raise InvalidParametersException.new("Missing parameter value for #{key}", command) end end |