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

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

Към профила на Йордан Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 12 успешни тест(а)
  • 3 неуспешни тест(а)

Код

class CommandParser
attr_reader :help
def initialize(command_name)
@argument_blocks = []
@options_hash = {}
@options_w_p_hash = {}
@help = "Usage: #{command_name}"
end
def argument(name, &block)
@help << " [#{name}]"
@argument_blocks << block
end
def argument_call(runner, values)
i = 0
@argument_blocks.each do |block|
block.call(runner, values[i]) if values[i]
i += 1
end
end
def option(short_name, name, description, &block)
@help << "\n -#{short_name}, --#{name} #{description}"
@options_hash["-" + short_name] = block
@options_hash["--" + name] = block
end
def option_call(runner, values)
values.each do |val|
@options_hash[val].call(runner, true) if @options_hash.key?(val)
end
end
def option_with_parameter(short_name, name, description, val, &block)
@help << "\n -#{short_name}, --#{name}=#{val} #{description}"
@options_w_p_hash["-" + short_name] = block
@options_w_p_hash["--" + name] = block
end
def option_with_parameter_call(runner, values)
values.each do |val|
key, v = val.split "="
@options_w_p_hash[key].call(runner, v) if @options_w_p_hash.key?(key)
end
end
def parse(command_runner, argv)
@argv_argument = argv.reject { |x| x.start_with? "-" }
@argv_opt = argv.select { |x| x.start_with? "-" }
@argv_opt = @argv_opt.reject { |x| x.include?("=") }
@argv_opt_w_p = argv - @argv_argument - @argv_opt
argument_call command_runner, @argv_argument
option_call command_runner, @argv_opt
option_with_parameter_call command_runner, @argv_opt_w_p
end
end

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

.....F.......FF

Failures:

  1) CommandParser#option_with_parameter parses a option with parameter in short format
     Failure/Error: expect(command_runner[:sort]).to eq 'time'
       
       expected: "time"
            got: :default
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"time"
       +:default
     # /tmp/d20161113-27983-1hy0qcr/spec.rb:89:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:sort]).to eq 'size'
       
       expected: "size"
            got: :default
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"size"
       +:default
     # /tmp/d20161113-27983-1hy0qcr/spec.rb:177:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) CommandParser when having options with and without values and parameters generates a correct help usage
     Failure/Error: expect(header).to eq 'Usage: ls [FIRST FILE] [SECOND FILE]'
       
       expected: "Usage: ls [FIRST FILE] [SECOND FILE]"
            got: "Usage: ls"
       
       (compared using ==)
     # /tmp/d20161113-27983-1hy0qcr/spec.rb:182:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.00935 seconds
15 examples, 3 failures

Failed examples:

rspec /tmp/d20161113-27983-1hy0qcr/spec.rb:83 # CommandParser#option_with_parameter parses a option with parameter in short format
rspec /tmp/d20161113-27983-1hy0qcr/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly
rspec /tmp/d20161113-27983-1hy0qcr/spec.rb:180 # CommandParser when having options with and without values and parameters generates a correct help usage

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

Йордан обнови решението на 09.11.2016 10:12 (преди над 7 години)

+class CommandParser
+ attr_reader :help
+ def initialize(command_name)
+ @argument_blocks = []
+ @options_hash = {}
+ @options_w_p_hash = {}
+ @help = "Usage: #{command_name}"
+ end
+
+ def argument(name, &block)
+ @help << " [#{name}]"
+ @argument_blocks << block
+ end
+
+ def argument_call(runner, values)
+ i = 0
+ @argument_blocks.each do |block|
+ block.call(runner, values[i]) if values[i]
+ i += 1
+ end
+ end
+
+ def option(short_name, name, description, &block)
+ @help << "\n -#{short_name}, --#{name} #{description}"
+ @options_hash["-" + short_name] = block
+ @options_hash["--" + name] = block
+ end
+
+ def option_call(runner, values)
+ values.each do |val|
+ @options_hash[val].call(runner, true) if @options_hash.key?(val)
+ end
+ end
+
+ def option_with_parameter(short_name, name, description, val, &block)
+ @help << "\n -#{short_name}, --#{name}=#{val} #{description}"
+ @options_w_p_hash["-" + short_name] = block
+ @options_w_p_hash["--" + name] = block
+ end
+
+ def option_with_parameter_call(runner, values)
+ values.each do |val|
+ key, v = val.split "="
+ @options_w_p_hash[key].call(runner, v) if @options_w_p_hash.key?(key)
+ end
+ end
+
+ def parse(command_runner, argv)
+ @argv_argument = argv.reject { |x| x.start_with? "-" }
+ @argv_opt = argv.select { |x| x.start_with? "-" }
+ @argv_opt = @argv_opt.reject { |x| x.include?("=") }
+ @argv_opt_w_p = argv - @argv_argument - @argv_opt
+ argument_call command_runner, @argv_argument
+ option_call command_runner, @argv_opt
+ option_with_parameter_call command_runner, @argv_opt_w_p
+ end
+end