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

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

Към профила на Красимир Тренчев

Резултати

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

Код

class CommandParser
def initialize(command)
@command_name = command
@arg = []
@opt_w_p = {}
@arg_cnt = 0
@used_args = 0
end
def argument(name)
@arg[@arg_cnt] = [Proc.new, name]
@arg_cnt += 1
end
def option_with_parameter(short_name, full_name, description, value = true)
short_name = '-' << short_name
full_name = '--' << full_name
@opt_w_p[short_name] = [full_name, description, Proc.new, value]
@opt_w_p[full_name] = [short_name, description, Proc.new, value]
end
alias_method :option, :option_with_parameter
def parse(command_runner, argv)
argv.each do |x|
if x[0] != '-'
@arg[@used_args][0].call(command_runner, x) && @used_args += 1 && next
end
name, param = x.split('=')
@opt_w_p[name][2].call(command_runner, true) && next unless param
@opt_w_p[name][2].call(command_runner, param)
end
end
def help
end
# def help
# description = "Usage: #{@command_name}"
# @arg.each { |x| description << " |#{x[1]}|" }
# options = find_options
# description << "\n" << options
# description
# end
# private
# def find_options
# all_descriptions = ""
# @opt_w_p.each do |value|
# all_descriptions << option_definition(value)
# end
# all_descriptions
# end
# def option_definition(value)
# definition = ""
# if value[0].length <= 2
# definition << " " << value[0] << " " << @opt_w_p[value[0]][0]
# if value[3] != true
# definition << "=" << value[3].to_s
# end
# definition << " " << value[1].to_s << "\n"
# end
# definition
# end
end

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

.F...F.FFFFFFFF

Failures:

  1) CommandParser#argument parses multiple arguments
     Failure/Error: expect(command_runner[:first_file]).to eq 'first.rb'
       
       expected: "first.rb"
            got: "other/third.rb"
       
       (compared using ==)
     # /tmp/d20161113-27983-1aqwd1a/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)>'

  2) CommandParser#option_with_parameter parses a option with parameter in short format
     Failure/Error: parser.parse(command_runner, %w(-stime))
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:30:in `block in parse'
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:25:in `each'
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:25:in `parse'
     # /tmp/d20161113-27983-1aqwd1a/spec.rb:87: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'
       
       expected: "Usage: ls"
            got: nil
       
       (compared using ==)
     # /tmp/d20161113-27983-1aqwd1a/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]'
       
       expected: "Usage: ls [FILE]"
            got: nil
       
       (compared using ==)
     # /tmp/d20161113-27983-1aqwd1a/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]'
       
       expected: "Usage: ls [FIRST FILE] [SECOND FILE] [THIRD FILE]"
            got: nil
       
       (compared using ==)
     # /tmp/d20161113-27983-1aqwd1a/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 `lines' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1aqwd1a/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 `lines' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1aqwd1a/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 `lines' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/spec.rb:19:in `options_help_messages'
     # /tmp/d20161113-27983-1aqwd1a/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: parser.parse(command_runner, %w(--all -d -ssize first.rb second.rb))
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:30:in `block in parse'
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:25:in `each'
     # /tmp/d20161113-27983-1aqwd1a/solution.rb:25:in `parse'
     # /tmp/d20161113-27983-1aqwd1a/spec.rb:169: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 `lines' for nil:NilClass
     # /tmp/d20161113-27983-1aqwd1a/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.00989 seconds
15 examples, 10 failures

Failed examples:

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

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

Красимир обнови решението на 08.11.2016 18:42 (преди над 7 години)

+class CommandParser
+ def initialize(command)
+ @command_name = command
+ @arg = []
+ @opt_w_p = {}
+ @arg_cnt = 0
+ @used_args = 0
+ end
+
+ def argument(name)
+ @arg[@arg_cnt] = [Proc.new, name]
+ @arg_cnt += 1
+ end
+
+ def option_with_parameter(short_name, full_name, description, value = true)
+ @opt_w_p['-' << short_name] = [full_name, description, Proc.new, value]
+ @opt_w_p['--' << full_name] = [short_name, description, Proc.new, value]
+ end
+
+ alias_method :option, :option_with_parameter
+
+ def parse(command_runner, argv)
+ argv.each do |x|
+ if x[0] != '-'
+ @arg[@used_args][0].call(command_runner, x) && @used_args += 1
+ else
+ name, param = x.split('=')
+ if param then @opt_w_p[name][2].call(command_runner, param)
+ else @opt_w_p[name][2].call(command_runner, true)
+ end
+ end
+ end
+ end
+
+ def help
+ puts @opt_w_p
+ end
+end
+

Красимир обнови решението на 09.11.2016 16:59 (преди над 7 години)

class CommandParser
def initialize(command)
@command_name = command
@arg = []
@opt_w_p = {}
@arg_cnt = 0
@used_args = 0
end
def argument(name)
@arg[@arg_cnt] = [Proc.new, name]
@arg_cnt += 1
end
def option_with_parameter(short_name, full_name, description, value = true)
- @opt_w_p['-' << short_name] = [full_name, description, Proc.new, value]
- @opt_w_p['--' << full_name] = [short_name, description, Proc.new, value]
+ short_name = '-' << short_name
+ full_name = '--' << full_name
+ @opt_w_p[short_name] = [full_name, description, Proc.new, value]
+ @opt_w_p[full_name] = [short_name, description, Proc.new, value]
end
alias_method :option, :option_with_parameter
def parse(command_runner, argv)
argv.each do |x|
if x[0] != '-'
- @arg[@used_args][0].call(command_runner, x) && @used_args += 1
- else
- name, param = x.split('=')
- if param then @opt_w_p[name][2].call(command_runner, param)
- else @opt_w_p[name][2].call(command_runner, true)
- end
+ @arg[@used_args][0].call(command_runner, x) && @used_args += 1 && next
end
+ name, param = x.split('=')
+ @opt_w_p[name][2].call(command_runner, true) && next unless param
+ @opt_w_p[name][2].call(command_runner, param)
end
end
def help
- puts @opt_w_p
end
-end
+ # def help
+ # description = "Usage: #{@command_name}"
+ # @arg.each { |x| description << " |#{x[1]}|" }
+ # options = find_options
+ # description << "\n" << options
+ # description
+ # end
+
+ # private
+ # def find_options
+ # all_descriptions = ""
+ # @opt_w_p.each do |value|
+ # all_descriptions << option_definition(value)
+ # end
+ # all_descriptions
+ # end
+
+ # def option_definition(value)
+ # definition = ""
+ # if value[0].length <= 2
+ # definition << " " << value[0] << " " << @opt_w_p[value[0]][0]
+ # if value[3] != true
+ # definition << "=" << value[3].to_s
+ # end
+ # definition << " " << value[1].to_s << "\n"
+ # end
+ # definition
+ # end
+end