Решение на Трета задача - Четене на командни аргументи от Александър Илиев

Обратно към всички решения

Към профила на Александър Илиев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
@options_with_paramenter = []
end
def argument(argument, &block)
@arguments.push([argument, block])
end
def option(short, long, description, &block)
@options.push([short, long, description, block])
end
def option_with_parameter(short, long, description, paramenter, &block)
@options_with_paramenter
.push([short, long, description, paramenter, block])
end
def parse(command_runner, argv)
argv.each do |variable|
if variable[0] == '-'
parse_option(command_runner, variable)
else
parse_argument(command_runner, variable)
end
end
end
def help
lines = "Usage: #{@command_name}"
@arguments.each { |x| lines << " [#{x[0]}]" }
@options.each { |x| lines << "\n -#{x[0]}, --#{x[1]} #{x[2]}" }
@options_with_paramenter
.each { |x| lines << "\n -#{x[0]}, --#{x[1]}=#{x[3]} #{x[2]}" }
lines
end
private
def parse_argument(command_runner, argument)
@arguments.first[1].call(command_runner, argument)
@arguments = @arguments.drop(1)
end
def parse_option(command_runner, argument)
if argument.include?('=') || (argument[1] != '-' && argument.length > 2)
parse_option_with_parameter(command_runner, argument)
else
argument = argument[1..-1] while argument[0] == '-'
end
@options.each do |variable|
variable[3].call(command_runner, true) if variable.include?(argument)
end
end
def parse_option_with_parameter(command_runner, argument)
argument = argument[1..-1] while argument[0] == '-'
argument.insert(1, "=") unless argument.include?('=')
argument = argument.split('=')
@options_with_paramenter.each do |variable|
if variable.include?(argument[0])
variable[4].call(command_runner, argument[1])
end
end
end
end

Лог от изпълнението

...............

Finished in 0.00836 seconds
15 examples, 0 failures

История (1 версия и 0 коментара)

Александър обнови решението на 03.11.2016 16:12 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = []
+ @options = []
+ @options_with_paramenter = []
+ end
+
+ def argument(argument, &block)
+ @arguments.push([argument, block])
+ end
+
+ def option(short, long, description, &block)
+ @options.push([short, long, description, block])
+ end
+
+ def option_with_parameter(short, long, description, paramenter, &block)
+ @options_with_paramenter
+ .push([short, long, description, paramenter, block])
+ end
+
+ def parse(command_runner, argv)
+ argv.each do |variable|
+ if variable[0] == '-'
+ parse_option(command_runner, variable)
+ else
+ parse_argument(command_runner, variable)
+ end
+ end
+ end
+
+ def help
+ lines = "Usage: #{@command_name}"
+ @arguments.each { |x| lines << " [#{x[0]}]" }
+ @options.each { |x| lines << "\n -#{x[0]}, --#{x[1]} #{x[2]}" }
+ @options_with_paramenter
+ .each { |x| lines << "\n -#{x[0]}, --#{x[1]}=#{x[3]} #{x[2]}" }
+ lines
+ end
+
+ private
+ def parse_argument(command_runner, argument)
+ @arguments.first[1].call(command_runner, argument)
+ @arguments = @arguments.drop(1)
+ end
+
+ def parse_option(command_runner, argument)
+ if argument.include?('=') || (argument[1] != '-' && argument.length > 2)
+ parse_option_with_parameter(command_runner, argument)
+ else
+ argument = argument[1..-1] while argument[0] == '-'
+ end
+
+ @options.each do |variable|
+ variable[3].call(command_runner, true) if variable.include?(argument)
+ end
+ end
+
+ def parse_option_with_parameter(command_runner, argument)
+ argument = argument[1..-1] while argument[0] == '-'
+ argument.insert(1, "=") unless argument.include?('=')
+ argument = argument.split('=')
+
+ @options_with_paramenter.each do |variable|
+ if variable.include?(argument[0])
+ variable[4].call(command_runner, argument[1])
+ end
+ end
+ end
+end