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

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

Към профила на Мариян Асенов

Резултати

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

Код

class CommandParser
def initialize(name)
@comand_name = name
@container = {argument: [], arg_name: [], option: [], option_with_arg: []}
end
def argument(name, &block)
@container[:argument] << block
@container[:arg_name] << "[#{name}]"
end
def option(short_name, name, doc, &block)
make_option(short_name, name, &block)
@container[:option] << " " * 4 + "-#{short_name}, --#{name} #{doc}"
end
def option_with_parameter(short_name, name, doc, arg, &block)
make_option(short_name, name, &block)
@container[:option_with_arg] << " " * 4 +
"-#{short_name}, --#{name}=#{arg} #{doc}"
end
def parse(runner, argv)
argv.map do |item|
key = @container.keys.select { |key| item.start_with?(key.to_s) }.first
if item.start_with?('-' || '--') && !item.include?('.') && !key.nil?
@container[key].call(runner, true)
elsif item.start_with?('-' || '--') && !key.nil?
@container[key].call(runner, item.gsub(key, "").delete("="))
elsif !item.start_with?('-' || '--')
@container[:argument].shift.call(runner, item)
end
end
end
def help
arguments = "Usage: #{@comand_name} #{@container[:arg_name].join(" ")}"
options = @container[:option]
options_with_arg = @container[:option_with_arg].join("\n")
unless options_with_arg == ""
options_with_arg << "\n"
arguments << "\n"
end
arguments.to_s << "#{options_with_arg}" \
"#{options.join("\n")}"
end
private
@comand_name
@container
def make_option(short_name, name, &block)
@container["--#{name}"] = block
@container["-#{short_name}"] = block
end
end

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

.....FFF..FF.F.

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: true
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"time"
       +true
     # /tmp/d20161113-27983-yp0ls7/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: true
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"time"
       +true
     # /tmp/d20161113-27983-yp0ls7/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'
       
       expected: "Usage: ls"
            got: "Usage: ls "
       
       (compared using ==)
     # /tmp/d20161113-27983-yp0ls7/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 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:    []
       the missing elements were:      ["    -a, --all do not ignore entries starting with ."]
     # /tmp/d20161113-27983-yp0ls7/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)>'

  5) 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 ."]
     # /tmp/d20161113-27983-yp0ls7/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)>'

  6) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:sort]).to eq 'size'
       
       expected: "size"
            got: true
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"size"
       +true
     # /tmp/d20161113-27983-yp0ls7/spec.rb:177: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.01108 seconds
15 examples, 6 failures

Failed examples:

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

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

Мариян обнови решението на 07.11.2016 23:43 (преди около 8 години)

+class CommandParser
+ def initialize(name)
+ @comand_name = name
+ @container = {argument: [], arg_name: [], option: [], option_with_arg: []}
+ end
+
+ def argument(name, &block)
+ @container[:argument] << block
+ @container[:arg_name] << "[#{name}]"
+ end
+
+ def option(short_name, name, doc, &block)
+ make_option(short_name, name, &block)
+ @container[:option] << " " * 4 + "-#{short_name}, --#{name} #{doc}"
+ end
+
+ def option_with_parameter(short_name, name, doc, arg, &block)
+ make_option(short_name, name, &block)
+ @container[:option_with_arg] << " " * 4 +
+ "-#{short_name}, --#{name}=#{arg} #{doc}"
+ end
+
+ def parse(runner, argv)
+ argv.map do |item|
+ key = @container.keys.select { |key| item.start_with?(key.to_s) }.first
+ if item.start_with?('-' || '--') && !item.include?('.')
+ @container[key].call(runner, true)
+ elsif item.start_with?('-' || '--')
+ @container[key].call(runner, item.gsub(key, "").delete("="))
+ else @container[:argument].shift.call(runner, item)
+ end
+ end
+ end
+
+def help
+ arguments = "Usage: #{@comand_name} #{@container[:arg_name].join("")}\n"
+ options = @container[:option]
+ options_with_arg = @container[:option_with_arg]
+ arguments.to_s << "#{options_with_arg.join("\n")}\n" \
+ "#{options.join("\n")}\n"
+end
+ private
+ @comand_name
+ @container
+ def make_option(short_name, name, &block)
+ @container["--#{name}"] = block
+ @container["-#{short_name}"] = block
+ end
+end

Мариян обнови решението на 09.11.2016 12:54 (преди около 8 години)

class CommandParser
def initialize(name)
@comand_name = name
@container = {argument: [], arg_name: [], option: [], option_with_arg: []}
end
def argument(name, &block)
@container[:argument] << block
@container[:arg_name] << "[#{name}]"
end
def option(short_name, name, doc, &block)
make_option(short_name, name, &block)
@container[:option] << " " * 4 + "-#{short_name}, --#{name} #{doc}"
end
def option_with_parameter(short_name, name, doc, arg, &block)
make_option(short_name, name, &block)
@container[:option_with_arg] << " " * 4 +
"-#{short_name}, --#{name}=#{arg} #{doc}"
end
def parse(runner, argv)
argv.map do |item|
key = @container.keys.select { |key| item.start_with?(key.to_s) }.first
- if item.start_with?('-' || '--') && !item.include?('.')
- @container[key].call(runner, true)
- elsif item.start_with?('-' || '--')
- @container[key].call(runner, item.gsub(key, "").delete("="))
- else @container[:argument].shift.call(runner, item)
- end
+ if item.start_with?('-' || '--') && !item.include?('.') && !key.nil?
+ @container[key].call(runner, true)
+ elsif item.start_with?('-' || '--') && !key.nil?
+ @container[key].call(runner, item.gsub(key, "").delete("="))
+ elsif !item.start_with?('-' || '--')
+ @container[:argument].shift.call(runner, item)
+ end
end
end
-def help
- arguments = "Usage: #{@comand_name} #{@container[:arg_name].join("")}\n"
- options = @container[:option]
- options_with_arg = @container[:option_with_arg]
- arguments.to_s << "#{options_with_arg.join("\n")}\n" \
- "#{options.join("\n")}\n"
-end
+ def help
+ arguments = "Usage: #{@comand_name} #{@container[:arg_name].join(" ")}"
+ options = @container[:option]
+ options_with_arg = @container[:option_with_arg].join("\n")
+ unless options_with_arg == ""
+ options_with_arg << "\n"
+ arguments << "\n"
+ end
+ arguments.to_s << "#{options_with_arg}" \
+ "#{options.join("\n")}"
+ end
private
@comand_name
@container
- def make_option(short_name, name, &block)
- @container["--#{name}"] = block
- @container["-#{short_name}"] = block
- end
+ def make_option(short_name, name, &block)
+ @container["--#{name}"] = block
+ @container["-#{short_name}"] = block
+ end
end