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

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

Към профила на Лазар Дилов

Резултати

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

Код

class CommandParser
def initialize(command_name)
@name = command_name
@runner = {}
@options_with_param = []
@options = []
@arguments = []
end
def argument(opt)
arguments << opt
yield runner, opt
end
def option(short, long, descr)
options << {short: '-' + short, long: '--' + long, descr: descr}
yield runner, true
end
def option_with_parameter(short, long, descr, value)
options_with_param << {
short: '-' + short, long: '--' + long,
descr: descr, value: value
}
yield runner, value
end
def parse(command_runner, argv)
runner.keys.each do |value|
if argv[0][0] == '-' && !argv[0].nil?
command_runner[value] = parse_options(argv[0], value)
elsif !argv[0].nil?
command_runner[value] = argv.first
end
argv.shift(1)
end
end
def help
result = "Usage: #{name}"
arguments.each { |arg| result += " [#{arg}]" }
options.each do |opt|
result += "\n #{opt[:short]}, #{opt[:long]} #{opt[:descr]}"
end
options_with_param.each do |opt|
result +=
"\n #{opt[:short]}, #{opt[:long]}=#{opt[:value]} #{opt[:descr]}"
end
result
end
private
attr_accessor :runner, :options_with_param, :options
attr_accessor :arguments, :name
def parse_options(option, value)
if option[0] == '-' && option[1] != '-'
result = option[2..option.size - 1]
elsif option[0] == '-' && option[1] == '-'
result = option.split('=')[1]
end
if result.nil?
return runner[value]
else
return result
end
end
end

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

FILE 2
..F.F........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 #<String:69929587702320> => ""
       
       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
       +""
     # /tmp/d20161113-27983-1kadyim/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 multiple options
     Failure/Error: expect(command_runner[:dir]).to be true
       
       expected #<TrueClass:20> => true
            got #<String:69929586758480> => ""
       
       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
       +""
     # /tmp/d20161113-27983-1kadyim/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)>'

  3) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:dir]).to be true
       
       expected #<TrueClass:20> => true
            got #<String:69929586223780> => ""
       
       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
       +""
     # /tmp/d20161113-27983-1kadyim/spec.rb:175: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.00939 seconds
15 examples, 3 failures

Failed examples:

rspec /tmp/d20161113-27983-1kadyim/spec.rb:51 # CommandParser#option parses a single option in short form
rspec /tmp/d20161113-27983-1kadyim/spec.rb:69 # CommandParser#option parses multiple options
rspec /tmp/d20161113-27983-1kadyim/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly

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

Лазар обнови решението на 02.11.2016 11:08 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @name = command_name
+ @runner = {}
+ @options_with_param = []
+ @options = []
+ @arguments = []
+ end
+
+ def argument(opt)
+ arguments << opt
+ yield runner, opt
+ end
+
+ def option(short, long, descr)
+ options << {short: '-' + short, long: '--' + long, descr: descr}
+ yield runner, true
+ end
+
+ def option_with_parameter(short, long, descr, value)
+ options_with_param << {short: '-' + short, long: '--' + long, descr: descr}
+ yield runner, value
+ end
+
+ def parse(command_runner, argv)
+ runner.keys.each do |value|
+ if argv[0][0] == '-' && !argv[0].nil?
+ command_runner[value] = parse_options(argv[0], value)
+ elsif !argv[0].nil?
+ command_runner[value] = argv.first
+ end
+ argv.shift(1)
+ end
+ command_runner
+ end
+
+ def help
+ result = "Usage: #{name}"
+ arguments.each { |arg| result += " [#{arg}]" }
+ result += "\n"
+ options.each do |opt|
+ result += "\n #{opt[:short]}, #{opt[:long]} #{opt[:descr]}"
+ end
+ options_with_param.each do |opt|
+ result += "\n #{opt[:short]}, #{opt[:long]} #{opt[:descr]}"
+ end
+ result
+ end
+
+ private
+ attr_accessor :runner
+ attr_accessor :options_with_param
+ attr_accessor :options
+ attr_accessor :arguments
+ attr_accessor :name
+ def parse_options(option, value)
+ if option[0] == '-' && option[1] != '-'
+ result = option[2..option.size - 1]
+ elsif option[0] == '-' && option[1] == '-'
+ result = option.split('=')[1]
+ end
+ if result.nil?
+ return runner[value]
+ else
+ return result
+ end
+ end
+end

Лазар обнови решението на 02.11.2016 11:24 (преди над 7 години)

class CommandParser
def initialize(command_name)
@name = command_name
@runner = {}
@options_with_param = []
@options = []
@arguments = []
end
def argument(opt)
arguments << opt
yield runner, opt
end
def option(short, long, descr)
options << {short: '-' + short, long: '--' + long, descr: descr}
yield runner, true
end
def option_with_parameter(short, long, descr, value)
- options_with_param << {short: '-' + short, long: '--' + long, descr: descr}
+ options_with_param << {
+ short: '-' + short, long: '--' + long,
+ descr: descr, value: value
+ }
yield runner, value
end
def parse(command_runner, argv)
runner.keys.each do |value|
if argv[0][0] == '-' && !argv[0].nil?
command_runner[value] = parse_options(argv[0], value)
elsif !argv[0].nil?
command_runner[value] = argv.first
end
argv.shift(1)
end
- command_runner
end
def help
result = "Usage: #{name}"
arguments.each { |arg| result += " [#{arg}]" }
- result += "\n"
options.each do |opt|
result += "\n #{opt[:short]}, #{opt[:long]} #{opt[:descr]}"
end
options_with_param.each do |opt|
- result += "\n #{opt[:short]}, #{opt[:long]} #{opt[:descr]}"
+ result +=
+ "\n #{opt[:short]}, #{opt[:long]}=#{opt[:value]} #{opt[:descr]}"
end
result
end
private
- attr_accessor :runner
- attr_accessor :options_with_param
- attr_accessor :options
- attr_accessor :arguments
- attr_accessor :name
+ attr_accessor :runner, :options_with_param, :options
+ attr_accessor :arguments, :name
def parse_options(option, value)
if option[0] == '-' && option[1] != '-'
result = option[2..option.size - 1]
elsif option[0] == '-' && option[1] == '-'
result = option.split('=')[1]
end
if result.nil?
return runner[value]
else
return result
end
end
end