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

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

Към профила на Здравко Петров

Резултати

  • 2 точки от тестове
  • 1 отнета точка
  • 1 точка общо
  • 5 успешни тест(а)
  • 10 неуспешни тест(а)

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@args = []
@opts = []
@a_blocks = []
@o_blocks = {}
@a_idx = 0
@o_idx = 0
@num_o = 0
end
def argument(argument_name, &arg_block)
@args << argument_name
@args
@a_blocks << arg_block
end
def option(short, long, helper_str, &opts_block)
@o_blocks[long] = opts_block
@opts << [short, long, helper_str]
@num_o += 1
@opts
end
def option_with_parameter(short, long, helper_str, parameter)
end
def check_opt(value, command_runner)
if @num_o != 0 && @o_blocks.include?(value[2, value.length])
@o_blocks[value[2, value.length]].call(command_runner, true)
elsif @num_o != 0 && @opts[@o_idx].include?(value[1, value.length])
@o_blocks[@opts[@o_idx][1]].call(command_runner, true)
@o_idx += 1
end
end
def parse(command_runner, argv)
argv.each do |value|
if value.start_with?('-')
check_opt(value, command_runner)
else
@a_blocks[@a_idx].call(command_runner, value)
@a_idx += 1
end
end
end
end

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

.....FFFFFFFFFF

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-1c7yjyj/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#option_with_parameter parses a option with parameter in long 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-1c7yjyj/spec.rb:98: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#help shows basic usage message
     Failure/Error: expect(parser.help).to eq 'Usage: ls'
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960d003b0>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:104: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)>'

  4) CommandParser#help shows single argument
     Failure/Error: expect(parser.help).to eq 'Usage: ls [FILE]'
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960ce1af0>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:110: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)>'

  5) CommandParser#help shows multiple arguments
     Failure/Error: expect(parser.help).to eq 'Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]'
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960cda908>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:118: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)>'

  6) CommandParser#help shows single option help
     Failure/Error: parser.help.lines.map(&:chomp).drop(1)
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960cada20>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:125: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)>'

  7) CommandParser#help shows multiple options help
     Failure/Error: parser.help.lines.map(&:chomp).drop(1)
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960ca4bc8>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:137: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)>'

  8) CommandParser#help shows options with parameter
     Failure/Error: parser.help.lines.map(&:chomp).drop(1)
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960c92590>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:148: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)>'

  9) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:dir]).to be true
       
       expected #<TrueClass:20> => true
            got #<Symbol:252508> => :default
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       `expect(actual).to eq(expected)` if you don't care about
       object identity in this example.
       
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +:default
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:175: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)>'

  10) CommandParser when having options with and without values and parameters generates a correct help usage
     Failure/Error: header = parser.help.lines.first.chomp
     NoMethodError:
       undefined method `help' for #<CommandParser:0x007f6960bf5628>
     # /tmp/d20161113-27983-1c7yjyj/spec.rb:181: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.00932 seconds
15 examples, 10 failures

Failed examples:

rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:83 # CommandParser#option_with_parameter parses a option with parameter in short format
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:92 # CommandParser#option_with_parameter parses a option with parameter in long format
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:103 # CommandParser#help shows basic usage message
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:107 # CommandParser#help shows single argument
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:113 # CommandParser#help shows multiple arguments
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:121 # CommandParser#help shows single option help
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:130 # CommandParser#help shows multiple options help
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:143 # CommandParser#help shows options with parameter
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly
rspec /tmp/d20161113-27983-1c7yjyj/spec.rb:180 # CommandParser when having options with and without values and parameters generates a correct help usage

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

Здравко обнови решението на 09.11.2016 16:38 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @args = []
+ @opts = []
+ @a_blocks = []
+ @o_blocks = {}
+ @a_idx = 0
+ @o_idx = 0
+ @num_o = 0
+ end
+
+ def argument(argument_name, &arg_block)
+ @args << argument_name
+ @args
+ @a_blocks << arg_block
+ end
+
+ def option(short, long, helper_str, &opts_block)
+ @o_blocks[long] = opts_block
+ @opts << [short, long, helper_str]
+ @num_o += 1
+ @opts
+ end
+
+ def option_with_parameter(short, long, helper_str, parameter)
+
+ end
+
+ def check_opt(value, command_runner)
+ if @num_o != 0 && @o_blocks.include?(value[2, value.length])
+ @o_blocks[value[2, value.length]].call(command_runner, true)
+ elsif @num_o != 0 && @opts[@o_idx].include?(value[1, value.length])
+ @o_blocks[@opts[@o_idx][1]].call(command_runner, true)
+ @o_idx += 1
+ end
+ end
+
+ def parse(command_runner, argv)
+ argv.each do |value|
+ if value.start_with?('-')
+ check_opt(value, command_runner)
+ else
+ @a_blocks[@a_idx].call(command_runner, value)
+ @a_idx += 1
+ end
+ end
+ end
+end

Взех ти 1 точка заради именуването - a_blocks, o_blocks, a_index, o_index. Използвай пълни имена - правят кода по-лесен за четене и по-разбираем. Използвайки съкращения пестиш само писане, но затрудняваш четенето на кода значително.