Решение на Трета задача - Четене на командни аргументи от Елеонора Кайкова

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

Към профила на Елеонора Кайкова

Резултати

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

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = {}
@options = {}
@full_to_short_name = {}
@options_with_parameters = {}
end
def argument(argument_name, &block)
@arguments[argument_name] = block
end
def option(short_name, full_name, description, &block)
@full_to_short_name[full_name] = short_name
@options[short_name] = {
full_name: full_name,
description: description,
block: block
}
end
def option_with_parameter(short_name, full_name, description, value, &block)
@full_to_short_name[full_name] = short_name
@options_with_parameters[short_name] = {
full_name: full_name,
description: description,
block: block,
value: value
}
end
def parse(command_runner, argv)
counter = 0
argv.each do |item|
if item[0] != '-'
@arguments[@arguments.keys[counter]].call(command_runner, item)
counter += 1
end
end
end
def help
a = "Usage: #{@command_name}"
a = a + @arguments.keys.reduce("") { |m, ob| m + " [#{ob}]" } + "\n"
help_1(a)
end
def help_1(a)
@options.each_key do |key|
a += " -#{key}, --#{@options[key][:full_name]}"
a = a + " " + @options[key][:description] + "\n"
end
@options_with_parameters.each_key do |key|
a += " -#{key}, --#{@options_with_parameters[key][:full_name]}="
a += @options_with_parameters[key][:value]
a = a + " " + @options_with_parameters[key][:description] + "\n"
end
a
end
end

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

..FFFFFFFF...F.

Failures:

  1) CommandParser#option parses a single option in short form
     Failure/Error: expect(command_runner[:all]).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-sogj6v/spec.rb:57: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 parses a single option in long form
     Failure/Error: expect(command_runner[:all]).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-sogj6v/spec.rb:66: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#option parses multiple options
     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-sogj6v/spec.rb:77: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#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-sogj6v/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)>'

  5) 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-sogj6v/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)>'

  6) CommandParser#help shows basic usage message
     Failure/Error: expect(parser.help).to eq 'Usage: ls'
       
       expected: "Usage: ls"
            got: "Usage: ls\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20161113-27983-sogj6v/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)>'

  7) CommandParser#help shows single argument
     Failure/Error: expect(parser.help).to eq 'Usage: ls [FILE]'
       
       expected: "Usage: ls [FILE]"
            got: "Usage: ls [FILE]\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20161113-27983-sogj6v/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)>'

  8) CommandParser#help shows multiple arguments
     Failure/Error: expect(parser.help).to eq 'Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]'
       
       expected: "Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]"
            got: "Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20161113-27983-sogj6v/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)>'

  9) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:all]).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-sogj6v/spec.rb:174: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.0168 seconds
15 examples, 9 failures

Failed examples:

rspec /tmp/d20161113-27983-sogj6v/spec.rb:51 # CommandParser#option parses a single option in short form
rspec /tmp/d20161113-27983-sogj6v/spec.rb:60 # CommandParser#option parses a single option in long form
rspec /tmp/d20161113-27983-sogj6v/spec.rb:69 # CommandParser#option parses multiple options
rspec /tmp/d20161113-27983-sogj6v/spec.rb:83 # CommandParser#option_with_parameter parses a option with parameter in short format
rspec /tmp/d20161113-27983-sogj6v/spec.rb:92 # CommandParser#option_with_parameter parses a option with parameter in long format
rspec /tmp/d20161113-27983-sogj6v/spec.rb:103 # CommandParser#help shows basic usage message
rspec /tmp/d20161113-27983-sogj6v/spec.rb:107 # CommandParser#help shows single argument
rspec /tmp/d20161113-27983-sogj6v/spec.rb:113 # CommandParser#help shows multiple arguments
rspec /tmp/d20161113-27983-sogj6v/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly

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

Елеонора обнови решението на 09.11.2016 16:56 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = {}
+ @options = {}
+ @full_to_short_name = {}
+ @options_with_parameters = {}
+ end
+
+ def argument(argument_name, &block)
+ @arguments[argument_name] = block
+ end
+
+ def option(short_name, full_name, description, &block)
+ @full_to_short_name[full_name] = short_name
+ @options[short_name] = {
+ full_name: full_name,
+ description: description,
+ block: block
+ }
+ end
+
+ def option_with_parameter(short_name, full_name, description, value, &block)
+ @full_to_short_name[full_name] = short_name
+ @options_with_parameters[short_name] = {
+ full_name: full_name,
+ description: description,
+ block: block,
+ value: value
+ }
+ end
+
+ def parse(command_runner, argv)
+ counter = 0
+ argv.each do |item|
+ if item[0] != '-'
+ @arguments[@arguments.keys[counter]].call(command_runner, item)
+ counter += 1
+ end
+ end
+ end
+
+ def help
+ a = "Usage: #{@command_name}"
+ a = a + @arguments.keys.reduce("") { |m, ob| m + " [#{ob}]" } + "\n"
+
+ help_1(a)
+ end
+
+ def help_1(a)
+ @options.each_key do |key|
+ a += " -#{key}, --#{@options[key][:full_name]}"
+ a = a + " " + @options[key][:description] + "\n"
+ end
+
+ @options_with_parameters.each_key do |key|
+ a += " -#{key}, --#{@options_with_parameters[key][:full_name]}="
+ a += @options_with_parameters[key][:value]
+ a = a + " " + @options_with_parameters[key][:description] + "\n"
+ end
+ a
+ end
+end