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

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

Към профила на Христо Христов

Резултати

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

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@list_of_argument_blocks = []
@list_of_option_blocks = {}
@arg_documentation = []
@opt_documentation = []
end
def argument(option_name, &block)
@list_of_argument_blocks.push(block)
@arg_documentation.push(option_name)
end
def option(short_n, normal_n, version_num, &block)
option_with_parameter(short_n, normal_n, version_num, true, &block)
end
def option_with_parameter(short_n, normal_n, version_num, value, &block)
@list_of_option_blocks['-' + short_n] = block
@list_of_option_blocks['--' + normal_n] = block
string = ' ' + '-' + short_n + ', --' + normal_n
string += '=' + value if value != true
string += ' ' + version_num
@opt_documentation.push(string)
end
def parse(command_runner, argv)
counter = -1
argv.each do |arg|
if arg[0] != '-' && counter += 1
@list_of_argument_blocks[counter].call command_runner, arg
else
name, value = set_name_and_value(arg, value, name)
@list_of_option_blocks[name].call command_runner, value
end
end
end
def help
string1 = ''
@arg_documentation.each { |elem| string1 += ' [' + elem + ']' }.to_s
string2 = ''
@opt_documentation.each { |elem| string2 += elem + "\n" }
'Usage:' + ' ' + @command_name + string1.to_s + "\n" + string2.to_s
end
private
def set_name_and_value(arg, value, name)
if arg[1] == '-' then name, value = arg.split('=')
else
name = arg.slice(0..1)
value = arg.slice(2..-1)
end
value = true if value == '' || value == nil
[name, value]
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-xg8qx2/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-xg8qx2/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-xg8qx2/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.01056 seconds
15 examples, 3 failures

Failed examples:

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

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

Христо обнови решението на 07.11.2016 14:39 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @list_of_blocks = []
+ @list_of_options = {}
+ @documentation = []
+ end
+
+ def argument(option_name, &block)
+ @list_of_blocks.push(block)
+ @documentation.push(option_name)
+ end
+
+ def option(short_n, normal_n, version_num, &block)
+
+ end
+
+ def option_with_parameter(option_name, block)
+
+ end
+
+ def parse(command_runner, argv)
+ operate_me = @list_of_blocks.zip(argv)
+
+ operate_me.each { |hash, argum| hash.call command_runner, argum }
+ end
+
+ def help
+ puts "Usage: #{@documentation}"
+ end
+end

Христо обнови решението на 08.11.2016 01:16 (преди над 7 години)

class CommandParser
def initialize(command_name)
@command_name = command_name
- @list_of_blocks = []
- @list_of_options = {}
- @documentation = []
+ @list_of_argument_blocks = []
+ @list_of_option_blocks = {}
+ @arg_documentation = []
+ @opt_documentation = {}
end
def argument(option_name, &block)
- @list_of_blocks.push(block)
- @documentation.push(option_name)
+ @list_of_argument_blocks.push(block)
+ @arg_documentation.push(option_name)
end
def option(short_n, normal_n, version_num, &block)
-
+ option_with_parameter(short_n, normal_n, version_num, true, &block)
end
- def option_with_parameter(option_name, block)
-
+ def option_with_parameter(short_n, normal_n, version_num, value, &block)
+ @list_of_option_blocks['-' + short_n] = block
+ @list_of_option_blocks['--' + normal_n] = block
+ version_num
+ value
end
-
- def parse(command_runner, argv)
- operate_me = @list_of_blocks.zip(argv)
- operate_me.each { |hash, argum| hash.call command_runner, argum }
+ def parse(command_runner, argv)
+ counter = -1
+ argv.each do |arg|
+ if arg[0] != '-' && counter += 1
+ @list_of_argument_blocks[counter].call command_runner, arg
+ else
+ name, value = set_name_and_value(arg, value, name)
+ @list_of_option_blocks[name].call command_runner, value
+ end
+ end
end
def help
- puts "Usage: #{@documentation}"
+ puts "Usage: "
end
-end
+
+ private
+
+ def set_name_and_value(arg, value, name)
+ if arg[1] == '-'
+ name, value = arg.split('=')
+ value = true if value == '' || value == nil
+ else
+ name = arg.slice(0..1)
+ value = arg.slice(2..-1)
+ end
+ [name, value]
+ end
+end

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

class CommandParser
def initialize(command_name)
@command_name = command_name
@list_of_argument_blocks = []
@list_of_option_blocks = {}
@arg_documentation = []
@opt_documentation = {}
end
def argument(option_name, &block)
@list_of_argument_blocks.push(block)
@arg_documentation.push(option_name)
end
def option(short_n, normal_n, version_num, &block)
option_with_parameter(short_n, normal_n, version_num, true, &block)
end
def option_with_parameter(short_n, normal_n, version_num, value, &block)
@list_of_option_blocks['-' + short_n] = block
@list_of_option_blocks['--' + normal_n] = block
version_num
value
end
def parse(command_runner, argv)
counter = -1
argv.each do |arg|
if arg[0] != '-' && counter += 1
@list_of_argument_blocks[counter].call command_runner, arg
else
name, value = set_name_and_value(arg, value, name)
@list_of_option_blocks[name].call command_runner, value
end
end
end
def help
puts "Usage: "
end
private
def set_name_and_value(arg, value, name)
- if arg[1] == '-'
- name, value = arg.split('=')
- value = true if value == '' || value == nil
+ if arg[1] == '-' then name, value = arg.split('=')
else
name = arg.slice(0..1)
value = arg.slice(2..-1)
end
+ value = true if value == '' || value == nil
[name, value]
end
-end
+end

Христо обнови решението на 08.11.2016 12:08 (преди над 7 години)

class CommandParser
def initialize(command_name)
@command_name = command_name
@list_of_argument_blocks = []
@list_of_option_blocks = {}
@arg_documentation = []
- @opt_documentation = {}
+ @opt_documentation = []
end
def argument(option_name, &block)
@list_of_argument_blocks.push(block)
@arg_documentation.push(option_name)
end
def option(short_n, normal_n, version_num, &block)
option_with_parameter(short_n, normal_n, version_num, true, &block)
end
def option_with_parameter(short_n, normal_n, version_num, value, &block)
@list_of_option_blocks['-' + short_n] = block
@list_of_option_blocks['--' + normal_n] = block
- version_num
- value
+ string = ' ' + '-' + short_n + ', --' + normal_n
+ string += '=' + value if value != true
+ string += ' ' + version_num
+ @opt_documentation.push(string)
end
def parse(command_runner, argv)
counter = -1
argv.each do |arg|
if arg[0] != '-' && counter += 1
@list_of_argument_blocks[counter].call command_runner, arg
else
name, value = set_name_and_value(arg, value, name)
@list_of_option_blocks[name].call command_runner, value
end
end
end
def help
- puts "Usage: "
+ string1 = ''
+ @arg_documentation.each { |elem| string1 += ' [' + elem + ']' }.to_s
+ string2 = ''
+ @opt_documentation.each { |elem| string2 += elem + "\n" }
+ 'Usage:' + ' ' + @command_name + string1.to_s + "\n" + string2.to_s
end
private
def set_name_and_value(arg, value, name)
if arg[1] == '-' then name, value = arg.split('=')
else
name = arg.slice(0..1)
value = arg.slice(2..-1)
end
value = true if value == '' || value == nil
[name, value]
end
end