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
- #fill_request_defaults(request, request_data) ⇒ 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
- #split_argv(argv) ⇒ 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.('..', __dir__)}/client/commands.yaml"), symbolize_names: true) end |
Instance Method Details
#_format(arg1, srg2) ⇒ Object
200 201 202 |
# File 'lib/mkit/client/command_parser.rb', line 200 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
185 186 187 188 189 190 |
# File 'lib/mkit/client/command_parser.rb', line 185 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
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 |
# File 'lib/mkit/client/command_parser.rb', line 86 def fill_cmd_args(args, argv, request, request_data) return if args.nil? args.each do |arg| arg[:type] = 'value' unless arg[:type] end split = split_argv(argv) argv = split[0] varargs = split[1] varargs = nil if varargs.empty? # find vararg and fill it vararg = args.select { |arg| arg[:type].to_sym == :varargs }.first if vararg request_data[vararg[:name].to_sym] = varargs request[:params] ||= [] request[:params] << vararg[:name].to_sym unless request[:params].include?(vararg[:name].to_sym) request_data[vararg[:name].to_sym] = varargs 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mkit/client/command_parser.rb', line 130 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
160 161 162 163 164 165 166 |
# File 'lib/mkit/client/command_parser.rb', line 160 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].to_sym unless request[:params].include?(arg[:name].to_sym) end end |
#fill_request_defaults(request, request_data) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mkit/client/command_parser.rb', line 70 def fill_request_defaults(request, request_data) if !request.nil? && !request[:defaults].nil? request[:defaults].each do |key, value| request[:params] ||= [] unless request[:params].include?(key.to_sym) request[:params] << key.to_sym request_data[key.to_sym] = value end end end end |
#find_command(cmd) ⇒ Object
181 182 183 |
# File 'lib/mkit/client/command_parser.rb', line 181 def find_command(cmd) dict.select { |k| k[:cmd] == cmd }.first end |
#find_option_or_flag_index(arg, argv) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/mkit/client/command_parser.rb', line 152 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
192 193 194 |
# File 'lib/mkit/client/command_parser.rb', line 192 def format_arg_help_msg(arg) _format(arg[:help][0], arg[:help][1]) end |
#format_cmd_help_msg(command) ⇒ Object
196 197 198 |
# File 'lib/mkit/client/command_parser.rb', line 196 def format_cmd_help_msg(command) _format(command[:cmd], command[:help]) end |
#help(cause: nil, cmd: nil) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/mkit/client/command_parser.rb', line 204 def help(cause: nil, cmd: nil) msg = '' if cause.nil? my_cmd = cmd else msg += "MKIt: #{cause.}\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 67 68 |
# 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? fill_request_defaults(request, request_data) validate_command(command, request_data) # { cmd: c[:cmd], request: request, data: request_data } end |
#split_argv(argv) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mkit/client/command_parser.rb', line 118 def split_argv(argv) separator_index = argv.index('--') if separator_index left_side = argv[0...separator_index] right_side = argv[(separator_index + 1)..-1] else left_side = argv right_side = [] end [left_side, right_side] end |
#validate_command(command, request_data) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/mkit/client/command_parser.rb', line 168 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 |