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

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

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

Резултати

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

Код

class Array
def only_arg(result)
each do |e|
result << e if e[0] != '-'
end
result
end
end
class String
def match_first_part(with)
with.size.times do |i|
return false if with[i] != self[i]
end
true
end
def option_arg(with)
additional_part = self[with.size..-1] if with.size == 2
additional_part = self[(with.size + 1)..-1] if with.size > 2
additional_part
end
end
module BonusFunction
def add_options_with_parameters(command_runner, argv)
res = ''
@hash_option_plus_args.each do |k, v|
argv.each do |e|
res = e.option_arg('--' + k[1]) if e.match_first_part('--' + k[1])
res = e.option_arg('-' + k[0]) if e.match_first_part('-' + k[0])
end
v.call command_runner, res if res != ''
res = ''
end
end
end
class CommandParser
include BonusFunction
def initialize(command_name)
@command_name = command_name
@hash_args = {}
@hash_option = {}
@hash_option_plus_args = {}
end
def argument(name, &block)
@hash_args.merge!({name => block})
end
def option(short_name, full_name, discription, &block)
arr_option_names = [short_name, full_name, discription]
@hash_option.merge!({arr_option_names => block})
end
def option_with_parameter(s_name, f_name, discription, placeholder, &block)
arr_option_names = [s_name, f_name, discription, placeholder]
@hash_option_plus_args.merge!({arr_option_names => block})
end
def parse(command_runner, argv)
work = []
argv.only_arg(work)
@hash_args.each_with_index { |e, i| e[1].call command_runner, work[i] }
@hash_option.each do |k, v|
if argv.include?('-' + k[0]) || argv.include?('--' + k[1])
v.call command_runner, true
end
end
add_options_with_parameters(command_runner, argv)
end
def help
arg_row = "Usage: #{@command_name}"
@hash_args.each { |k, _| arg_row += " [#{k}]" }
arg_row += "\n"
@hash_option.each { |k, _| arg_row += " -#{k[0]}, --#{k[1]} #{k[2]}\n" }
@hash_option_plus_args.each do |k, _|
arg_row += " -#{k[0]}, --#{k[1]}=#{k[3]} #{k[2]}\n"
end
arg_row
end
end

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

.......FFF.....

Failures:

  1) 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-1um93xu/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)>'

  2) 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-1um93xu/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)>'

  3) 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-1um93xu/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)>'

Finished in 0.00915 seconds
15 examples, 3 failures

Failed examples:

rspec /tmp/d20161113-27983-1um93xu/spec.rb:103 # CommandParser#help shows basic usage message
rspec /tmp/d20161113-27983-1um93xu/spec.rb:107 # CommandParser#help shows single argument
rspec /tmp/d20161113-27983-1um93xu/spec.rb:113 # CommandParser#help shows multiple arguments

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

Стамен обнови решението на 04.11.2016 14:40 (преди над 7 години)

+class Array
+ def only_arg(result)
+ each do |e|
+ result << e if e[0] != '-'
+ end
+ result
+ end
+end
+
+class String
+ def match_first_part(with)
+ with.size.times do |i|
+ return false if with[i] != self[i]
+ end
+ true
+ end
+
+ def option_arg(with)
+ additional_part = self[with.size..-1] if with.size == 2
+ additional_part = self[(with.size + 1)..-1] if with.size > 2
+ additional_part
+ end
+end
+
+module BonusFunction
+ def add_options_with_parameters(command_runner, argv)
+ res = ''
+ @hash_option_plus_args.each do |k, v|
+ argv.each do |e|
+ res = e.option_arg('--' + k[1]) if e.match_first_part('--' + k[1])
+ res = e.option_arg('-' + k[0]) if e.match_first_part('-' + k[0])
+ end
+ v.call command_runner, res if res != ''
+ res = ''
+ end
+ end
+end
+
+class CommandParser
+ include BonusFunction
+ def initialize(command_name)
+ @command_name = command_name
+ @hash_args = {}
+ @hash_option = {}
+ @hash_option_plus_args = {}
+ end
+
+ def argument(name, &block)
+ @hash_args.merge!({name => block})
+ end
+
+ def option(short_name, full_name, discription, &block)
+ arr_option_names = [short_name, full_name, discription]
+ @hash_option.merge!({arr_option_names => block})
+ end
+
+ def option_with_parameter(s_name, f_name, discription, placeholder, &block)
+ arr_option_names = [s_name, f_name, discription, placeholder]
+ @hash_option_plus_args.merge!({arr_option_names => block})
+ end
+
+ def parse(command_runner, argv)
+ work = []
+ argv.only_arg(work)
+ @hash_args.each_with_index { |e, i| e[1].call command_runner, work[i] }
+
+ @hash_option.each do |k, v|
+ if argv.include?('-' + k[0]) || argv.include?('--' + k[1])
+ v.call command_runner, true
+ end
+ end
+
+ add_options_with_parameters(command_runner, argv)
+ end
+
+ def help
+ arg_row = "Usage: #{@command_name}"
+ @hash_args.each { |k, _| arg_row += " [#{k}]" }
+ arg_row += "\n"
+ @hash_option.each { |k, _| arg_row += " -#{k[0]}, --#{k[1]} #{k[2]}\n" }
+ @hash_option_plus_args.each do |k, _|
+ arg_row += " -#{k[0]}, --#{k[1]}=#{k[3]} #{k[2]}\n"
+ end
+ arg_row
+ end
+end