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

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

Към профила на Даниела Русева

Резултати

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

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
@option_with_parameters = []
end
def argument(name, &block)
@arguments.push(Argument.new(name, block))
end
def option(short_name, full_name, description, &block)
@options.push(Option.new(short_name, full_name, description, block))
end
def option_with_parameter(short_name, full_name, description, holder, &block)
@option_with_parameters.push(OptionWithParameter.new(
short_name,
full_name,
description,
holder,
block
)
)
end
def parse(command_runner, argv)
@arguments.each_with_index { |n, i| n.parse(command_runner, argv[i]) }
@options.each { |n| n.parse(command_runner, argv) }
@option_with_parameters.each { |n| n.parse(command_runner, argv) }
end
def help
message = ''
if @arguments
message << 'Usage: ' + @command_name
@arguments.each { |x| x.help(message) }
end
@options.each { |x| x.help(message) }
@option_with_parameters.each { |x| x.help(message) }
message
end
end
class Argument
def initialize(name, block)
@name = name
@block = block
end
def parse(command_runner, argv)
@block.call(command_runner, argv)
end
def help(message)
message << ' [' + @name + ']'
end
end
class Option
def initialize(short_name, full_name, description, block)
@short_name = short_name
@full_name = full_name
@description = description
@block = block
end
def parse(command_runner, argv)
if argv.any? { |x| x[1..-1] == @short_name || x[2..-1] == @full_name }
@block.call(command_runner, true)
end
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + ' ' + @description
end
end
class OptionWithParameter
def initialize(short_name, full_name, description, holder, block)
@short_name = short_name
@full_name = full_name
@description = description
@holder = holder
@block = block
end
def parse(command_runner, argv)
if argv.any? { |x| x[2..(@full_name.size + 1)] == @full_name }
@block.call(command_runner, argv[0][(3 + @full_name.size)..-1])
elsif argv.any? { |x| x[1] == @short_name }
@block.call(command_runner, argv[0][2..-1])
end
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + '=' + @holder + ' ' + @description
end
end

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

.............F.

Failures:

  1) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:first_file]).to eq 'first.rb'
       
       expected: "first.rb"
            got: "--all"
       
       (compared using ==)
     # /tmp/d20161113-27983-1e4sv5z/spec.rb:171: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.00969 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161113-27983-1e4sv5z/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly

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

Даниела обнови решението на 07.11.2016 16:36 (преди над 7 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = []
+ @options = []
+ @option_with_parameters = []
+ end
+
+ def argument(name, &block)
+ @arguments.push(Argument.new(name, block))
+ end
+
+ def option(short_name, full_name, description, &block)
+ @options.push(Option.new(short_name, full_name, description, block))
+ end
+
+ def option_with_parameter(short_name, full_name, description, holder, &block)
+ @option_with_parameters.push(OptionWithParameter.new(
+ short_name,
+ full_name,
+ description,
+ holder,
+ block
+ )
+ )
+ end
+
+ def parse(command_runner, argv)
+ @arguments.each_with_index { |n, i| n.parse(command_runner, argv[i]) }
+ @options.each { |n| n.parse(command_runner, argv) }
+ @option_with_parameters.each { |n| n.parse(command_runner, argv) }
+ end
+
+ def help
+ message = ''
+ if @arguments
+ message << 'Usage: ' + @command_name
+ @arguments.each { |x| x.help(message) }
+ end
+ @options.each { |x| x.help(message) }
+ @option_with_parameters.each { |x| x.help(message) }
+ message
+ end
+end
+
+class Argument < CommandParser
+ def initialize(name, block)
+ @name = name
+ @block = block
+ end
+
+ def parse(command_runner, argv)
+ @block.call(command_runner, argv)
+ end
+
+ def help(message)
+ message << ' [' + @name + ']'
+ end
+end
+
+class Option < CommandParser
+ def initialize(short_name, full_name, description, block)
+ @short_name = short_name
+ @full_name = full_name
+ @description = description
+ @block = block
+ end
+
+ def parse(command_runner, argv)
+ argv.any? { |x| x[1..-1] == @short_name || x[2..-1] == @full_name }
+ @block.call(command_runner, true)
+ end
+
+ def help(message)
+ message << "\n" + ' -' +
+ @short_name + ', --' +
+ @full_name + ' ' + @description
+ end
+end
+
+class OptionWithParameter < CommandParser
+ def initialize(short_name, full_name, description, holder, block)
+ @short_name = short_name
+ @full_name = full_name
+ @description = description
+ @holder = holder
+ @block = block
+ end
+
+ def parse(command_runner, argv)
+ if argv.any? { |x| x[2..(@full_name.size + 1)] == @full_name }
+ @block.call(command_runner, argv[0][(3 + @full_name.size)..-1])
+ elsif argv.any? { |x| x[1] == @short_name }
+ @block.call(command_runner, argv[0][2..-1])
+ end
+ end
+
+ def help(message)
+ message << "\n" + ' -' +
+ @short_name + ', --' +
+ @full_name + '=' + @holder + ' ' + @description
+ end
+end

Даниела обнови решението на 07.11.2016 16:39 (преди над 7 години)

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
@option_with_parameters = []
end
def argument(name, &block)
@arguments.push(Argument.new(name, block))
end
def option(short_name, full_name, description, &block)
@options.push(Option.new(short_name, full_name, description, block))
end
def option_with_parameter(short_name, full_name, description, holder, &block)
@option_with_parameters.push(OptionWithParameter.new(
short_name,
full_name,
description,
holder,
block
)
)
end
def parse(command_runner, argv)
@arguments.each_with_index { |n, i| n.parse(command_runner, argv[i]) }
@options.each { |n| n.parse(command_runner, argv) }
@option_with_parameters.each { |n| n.parse(command_runner, argv) }
end
def help
message = ''
if @arguments
message << 'Usage: ' + @command_name
@arguments.each { |x| x.help(message) }
end
@options.each { |x| x.help(message) }
@option_with_parameters.each { |x| x.help(message) }
message
end
end
-class Argument < CommandParser
+class Argument
def initialize(name, block)
@name = name
@block = block
end
def parse(command_runner, argv)
@block.call(command_runner, argv)
end
def help(message)
message << ' [' + @name + ']'
end
end
-class Option < CommandParser
+class Option
def initialize(short_name, full_name, description, block)
@short_name = short_name
@full_name = full_name
@description = description
@block = block
end
def parse(command_runner, argv)
argv.any? { |x| x[1..-1] == @short_name || x[2..-1] == @full_name }
@block.call(command_runner, true)
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + ' ' + @description
end
end
-class OptionWithParameter < CommandParser
+class OptionWithParameter
def initialize(short_name, full_name, description, holder, block)
@short_name = short_name
@full_name = full_name
@description = description
@holder = holder
@block = block
end
def parse(command_runner, argv)
if argv.any? { |x| x[2..(@full_name.size + 1)] == @full_name }
@block.call(command_runner, argv[0][(3 + @full_name.size)..-1])
elsif argv.any? { |x| x[1] == @short_name }
@block.call(command_runner, argv[0][2..-1])
end
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + '=' + @holder + ' ' + @description
end
end

Даниела обнови решението на 07.11.2016 16:44 (преди над 7 години)

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
@option_with_parameters = []
end
def argument(name, &block)
@arguments.push(Argument.new(name, block))
end
def option(short_name, full_name, description, &block)
@options.push(Option.new(short_name, full_name, description, block))
end
def option_with_parameter(short_name, full_name, description, holder, &block)
@option_with_parameters.push(OptionWithParameter.new(
short_name,
full_name,
description,
holder,
block
)
)
end
def parse(command_runner, argv)
@arguments.each_with_index { |n, i| n.parse(command_runner, argv[i]) }
@options.each { |n| n.parse(command_runner, argv) }
@option_with_parameters.each { |n| n.parse(command_runner, argv) }
end
def help
message = ''
if @arguments
message << 'Usage: ' + @command_name
@arguments.each { |x| x.help(message) }
end
@options.each { |x| x.help(message) }
@option_with_parameters.each { |x| x.help(message) }
message
end
end
class Argument
def initialize(name, block)
@name = name
@block = block
end
def parse(command_runner, argv)
@block.call(command_runner, argv)
end
def help(message)
message << ' [' + @name + ']'
end
end
class Option
def initialize(short_name, full_name, description, block)
@short_name = short_name
@full_name = full_name
@description = description
@block = block
end
def parse(command_runner, argv)
- argv.any? { |x| x[1..-1] == @short_name || x[2..-1] == @full_name }
- @block.call(command_runner, true)
+ if argv.any? { |x| x[1..-1] == @short_name || x[2..-1] == @full_name }
+ @block.call(command_runner, true)
+ end
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + ' ' + @description
end
end
class OptionWithParameter
def initialize(short_name, full_name, description, holder, block)
@short_name = short_name
@full_name = full_name
@description = description
@holder = holder
@block = block
end
def parse(command_runner, argv)
if argv.any? { |x| x[2..(@full_name.size + 1)] == @full_name }
@block.call(command_runner, argv[0][(3 + @full_name.size)..-1])
elsif argv.any? { |x| x[1] == @short_name }
@block.call(command_runner, argv[0][2..-1])
end
end
def help(message)
message << "\n" + ' -' +
@short_name + ', --' +
@full_name + '=' + @holder + ' ' + @description
end
end