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

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

Към профила на Симеон Гергинов

Резултати

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

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
end
def argument(name_of_argument, &block)
@block = block
@name = name_of_argument
end
def option(short_option, full_option, help_text, &block)
@short_option = short_option
@full_option = full_option
@help_text = help_text
@block = block
@short = '-' + @short_option
@full = '--' + @full_option
end
def option_with_parameter(short_opt, full_opt, help_text, value_hold, &block)
@short_par = short_opt
@full_par = full_opt
@text = help_text
@place_holder = value_hold
@block = block
@short_param = '-' + @short_par
@full_param = '--' + @full_par + '='
end
def checker(arg)
if arg.include? @full_param
arg.sub!(@full_param, '')
elsif arg.include? @short_param
arg.sub!(@short_param, '')
end
end
def parse(command_runner, argv)
argv.each do |arg|
if arg.index('-') != 0 && arg.index('--') != 0
@block.call(command_runner, arg)
elsif arg.eql?(@short) || arg.eql?(@full)
@block.call(command_runner, true)
elsif checker(arg)
@block.call(command_runner, arg)
end
end
end
def help
first_line = "Usage: #{@command_name} [#{@name}]"
second_line = " -#{@short_option}, --#{@full_option} #{@help_text}"
third_line = " -#{@short_par}, --#{@full_par}=#{@place_holder} #{@text}"
first_line + "\n" + second_line + "\n" + third_line
end
end

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

Programming/ruby
FF..F..FFFFFFFF

Failures:

  1) CommandParser#argument parses a single argument
     Failure/Error: expect(command_runner[:file_name]).to eq 'Programming/ruby'
       
       expected: "Programming/ruby"
            got: nil
       
       (compared using ==)
     # /tmp/d20161113-27983-1cklm8q/spec.rb:34: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#argument parses multiple arguments
     Failure/Error: expect(command_runner[:first_file]).to eq 'first.rb'
       
       expected: "first.rb"
            got: :default
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"first.rb"
       +:default
     # /tmp/d20161113-27983-1cklm8q/spec.rb:44: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: parser.parse(command_runner, %w(--directory -a))
     TypeError:
       no implicit conversion of nil into String
     # /tmp/d20161113-27983-1cklm8q/solution.rb:31:in `include?'
     # /tmp/d20161113-27983-1cklm8q/solution.rb:31:in `checker'
     # /tmp/d20161113-27983-1cklm8q/solution.rb:44:in `block in parse'
     # /tmp/d20161113-27983-1cklm8q/solution.rb:39:in `each'
     # /tmp/d20161113-27983-1cklm8q/solution.rb:39:in `parse'
     # /tmp/d20161113-27983-1cklm8q/spec.rb:75: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 basic usage message
     Failure/Error: expect(parser.help).to eq 'Usage: ls'
       
       expected: "Usage: ls"
            got: "Usage: ls []\n    -, -- \n    -, --= "
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,4 @@
       -Usage: ls
       +Usage: ls []
       +    -, -- 
       +    -, --= 
     # /tmp/d20161113-27983-1cklm8q/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)>'

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

  6) 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 [THIRD FILE]\n    -, -- \n    -, --= "
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,4 @@
       -Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]
       +Usage: ls [THIRD FILE]
       +    -, -- 
       +    -, --= 
     # /tmp/d20161113-27983-1cklm8q/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)>'

  7) CommandParser#help shows single option help
     Failure/Error: expect(options_help_messages(parser)).to match_array([
       expected collection contained:  ["    -a, --all do not ignore entries starting with ."]
       actual collection contained:    ["    -, --= ", "    -a, --all do not ignore entries starting with ."]
       the extra elements were:        ["    -, --= "]
     # /tmp/d20161113-27983-1cklm8q/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)>'

  8) CommandParser#help shows multiple options help
     Failure/Error: expect(options_help_messages(parser)).to match_array([
       expected collection contained:  ["    -a, --all do not ignore entries starting with .", "    -d, --directory list directories themselves, not their contents"]
       actual collection contained:    ["    -, --= ", "    -d, --directory list directories themselves, not their contents"]
       the missing elements were:      ["    -a, --all do not ignore entries starting with ."]
       the extra elements were:        ["    -, --= "]
     # /tmp/d20161113-27983-1cklm8q/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)>'

  9) CommandParser#help shows options with parameter
     Failure/Error: expect(options_help_messages(parser)).to match_array([
       expected collection contained:  ["    -s, --sort=WORD sort by WORD instead of name"]
       actual collection contained:    ["    -, -- ", "    -s, --sort=WORD sort by WORD instead of name"]
       the extra elements were:        ["    -, -- "]
     # /tmp/d20161113-27983-1cklm8q/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)>'

  10) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:first_file]).to eq 'first.rb'
       
       expected: "first.rb"
            got: :default
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"first.rb"
       +:default
     # /tmp/d20161113-27983-1cklm8q/spec.rb:171: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)>'

  11) 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 [SECOND FILE]"
       
       (compared using ==)
     # /tmp/d20161113-27983-1cklm8q/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.01149 seconds
15 examples, 11 failures

Failed examples:

rspec /tmp/d20161113-27983-1cklm8q/spec.rb:26 # CommandParser#argument parses a single argument
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:37 # CommandParser#argument parses multiple arguments
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:69 # CommandParser#option parses multiple options
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:103 # CommandParser#help shows basic usage message
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:107 # CommandParser#help shows single argument
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:113 # CommandParser#help shows multiple arguments
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:121 # CommandParser#help shows single option help
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:130 # CommandParser#help shows multiple options help
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:143 # CommandParser#help shows options with parameter
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly
rspec /tmp/d20161113-27983-1cklm8q/spec.rb:180 # CommandParser when having options with and without values and parameters generates a correct help usage

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

Симеон обнови решението на 08.11.2016 10:36 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ end
+
+ def argument(name_of_argument, &block)
+ @block = block
+ @name = name_of_argument
+ end
+
+ def option(short_option, full_option, help_text, &block)
+ @short_option = short_option
+ @full_option = full_option
+ @help_text = help_text
+ @block = block
+ @short = '-' + @short_option
+ @full = '--' + @full_option
+ end
+
+ def option_with_parameter(short_opt, full_opt, help_text, value_hold, &block)
+ @short_par = short_opt
+ @full_par = full_opt
+ @text = help_text
+ @place_holder = value_hold
+ @block = block
+ @short_param = '-' + @short_par
+ @full_param = '--' + @full_par + '='
+ end
+
+ def checker(arg)
+ if arg.include? @full_param
+ arg.sub!(@full_param, '')
+ elsif arg.include? @short_param
+ arg.sub!(@short_param, '')
+ end
+ end
+
+ def parse(command_runner, argv)
+ argv.each do |arg|
+ if arg.index('-') != 0 && arg.index('--') != 0
+ @block.call(command_runner, arg)
+ elsif arg.eql?(@short) || arg.eql?(@full)
+ @block.call(command_runner, true)
+ elsif checker(arg)
+ @block.call(command_runner, arg)
+ end
+ end
+ end
+
+ def help
+ first_line = "Usage: #{@command_name} [#{@name}]"
+ second_line = " -#{@short_option}, --#{@full_option} #{@help_text}"
+ third_line = " -#{@short_par}, --#{@full_par}=#{@place_holder} #{@text}"
+ first_line + "\n" + second_line + "\n" + third_line
+ end
+end