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

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

Към профила на Петър Велев

Резултати

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

Код

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(argument, &block)
@arguments << Argument.new(argument, &block)
end
def option(sname, lname, help, &block)
@options << Option.new(sname, lname, help, &block)
end
def option_with_parameter(sname, lname, help, dependency, &block)
@options << OptionWithParam.new(sname, lname, help, dependency, &block)
end
def parse(command_runner, argv)
argv = parse_argv(argv)
parse_arguments(command_runner, argv[1])
argv[0].each { |el| parse_options(command_runner, el) }
end
def help
str = 'Usage: '
str << @command_name + " "
str << @arguments.each { |arg| arg.to_s }.join(" ")
str << "\n" unless @options.empty?
str << @options.each { |opt| opt.to_s }.join("\n")
end
private
def parse_options(command_runner, name)
@options.each { |opt| opt.execute_block(command_runner, name) }
end
def parse_arguments(command_runner, argument_values)
i = 0
@arguments.each do |arg|
arg.execute_block(command_runner, argument_values[i])
i += 1
end
end
def parse_argv(argv)
out = []
out[0] = argv.select { |element| element.include?('-') }
out[1] = argv.reject { |element| element.include?('-') }
out
end
end
class Argument
def initialize(name, &block)
@name = name
@block = block
end
def execute_block(command_runner, argvalue)
@block.call(command_runner, argvalue)
end
def to_s
"[#{@name}]"
end
end
class Option
def initialize(sname, lname, help, &block)
@sname = '-' + sname
@lname = '--' + lname
@help = help
@block = block
end
def execute_block(command_runner, name)
@block.call(command_runner, true) if name == @lname || name == @sname
end
def to_s
" #{@sname}, #{@lname} #{@help}"
end
end
class OptionWithParam < Option
def initialize(sname, lname, help, dependency, &block)
@sname = '-' + sname
@lname = '--' + lname
@help = help
@dependency = dependency
@block = block
end
def execute_block(command_runner, name)
name = parse_name(name)
@block.call(command_runner, name)
end
def to_s
" #{@sname}, #{@lname}=#{@dependency} #{@help}"
end
private
def parse_name(name)
if name.include?('=')
name.split('=')[1]
else
name[2..-1]
end
end
end

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

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

Failures:

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

Finished in 0.00876 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161113-27983-1sgytgs/spec.rb:103 # CommandParser#help shows basic usage message

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

Петър обнови решението на 09.11.2016 03:46 (преди около 8 години)

+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = []
+ @options = []
+ end
+
+ def argument(argument, &block)
+ @arguments << Argument.new(argument, &block)
+ end
+
+ def option(sname, lname, help, &block)
+ @options << Option.new(sname, lname, help, &block)
+ end
+
+ def option_with_parameter(sname, lname, help, dependency, &block)
+ @options << OptionWithParam.new(sname, lname, help, dependency, &block)
+ end
+
+ def parse(command_runner, argv)
+ argv = parse_argv(argv)
+ parse_arguments(command_runner, argv[1])
+ argv[0].each { |el| parse_options(command_runner, el) }
+ end
+
+ def help
+ print 'Usage: '
+ @arguments.each { |arg| print arg }
+ puts ""
+ @options.each { |opt| puts opt }
+ end
+
+ private
+ def parse_options(command_runner, name)
+ @options.each { |opt| opt.execute_block(command_runner, name) }
+ end
+
+ def parse_arguments(command_runner, argument_values)
+ i = 0
+ @arguments.each do |arg|
+ arg.execute_block(command_runner, argument_values[i])
+ i += 1
+ end
+ end
+
+ def parse_argv(argv)
+ out = []
+ out[0] = argv.select { |element| element.include?('-') }
+ out[1] = argv.reject { |element| element.include?('-') }
+ out
+ end
+end
+
+class Argument
+ def initialize(name, &block)
+ @name = name
+ @block = block
+ end
+ attr_reader :name
+
+ def execute_block(command_runner, argvalue)
+ @block.call(command_runner, argvalue)
+ end
+
+ def to_s
+ '[' + name + ']'
+ end
+end
+
+class Option
+ def initialize(sname, lname, help, &block)
+ @sname = '-' + sname
+ @lname = '--' + lname
+ @help = help
+ @block = block
+ end
+ attr_reader :sname
+ attr_reader :lname
+ attr_reader :help
+
+ def execute_block(command_runner, _)
+ @block.call(command_runner, true)
+ end
+
+ def to_s
+ " " + sname + ", " + lname + " " + help
+ end
+end
+
+class OptionWithParam < Option
+ def initialize(sname, lname, help, dependency, &block)
+ @sname = '-' + sname
+ @lname = '--' + lname + "=" + dependency
+ @help = help
+ @dependency = dependency
+ @block = block
+ end
+ attr_reader :dependency
+
+ def execute_block(command_runner, argvalue)
+ argvalue = parse_name(argvalue)
+ @block.call(command_runner, argvalue)
+ end
+
+ private
+ def parse_name(name)
+ if name.include?('=')
+ name.split('=')[1]
+ else
+ name[2..-1]
+ end
+ end
+end

Петър обнови решението на 09.11.2016 04:34 (преди около 8 години)

class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(argument, &block)
@arguments << Argument.new(argument, &block)
end
def option(sname, lname, help, &block)
@options << Option.new(sname, lname, help, &block)
end
def option_with_parameter(sname, lname, help, dependency, &block)
@options << OptionWithParam.new(sname, lname, help, dependency, &block)
end
def parse(command_runner, argv)
argv = parse_argv(argv)
parse_arguments(command_runner, argv[1])
argv[0].each { |el| parse_options(command_runner, el) }
end
def help
- print 'Usage: '
- @arguments.each { |arg| print arg }
- puts ""
- @options.each { |opt| puts opt }
+ str = 'Usage: '
+ str << @command_name + " "
+ str << @arguments.each { |arg| arg.to_s }.join(" ")
+ str << "\n" unless @options.empty?
+ str << @options.each { |opt| opt.to_s }.join("\n")
end
private
def parse_options(command_runner, name)
@options.each { |opt| opt.execute_block(command_runner, name) }
end
def parse_arguments(command_runner, argument_values)
i = 0
@arguments.each do |arg|
arg.execute_block(command_runner, argument_values[i])
i += 1
end
end
def parse_argv(argv)
out = []
out[0] = argv.select { |element| element.include?('-') }
out[1] = argv.reject { |element| element.include?('-') }
out
end
end
class Argument
def initialize(name, &block)
@name = name
@block = block
end
- attr_reader :name
def execute_block(command_runner, argvalue)
@block.call(command_runner, argvalue)
end
def to_s
- '[' + name + ']'
+ "[#{@name}]"
end
end
class Option
def initialize(sname, lname, help, &block)
@sname = '-' + sname
@lname = '--' + lname
@help = help
@block = block
end
- attr_reader :sname
- attr_reader :lname
- attr_reader :help
- def execute_block(command_runner, _)
- @block.call(command_runner, true)
+ def execute_block(command_runner, name)
+ @block.call(command_runner, true) if name == @lname || name == @sname
end
def to_s
- " " + sname + ", " + lname + " " + help
+ " #{@sname}, #{@lname} #{@help}"
end
end
class OptionWithParam < Option
def initialize(sname, lname, help, dependency, &block)
@sname = '-' + sname
- @lname = '--' + lname + "=" + dependency
+ @lname = '--' + lname
@help = help
@dependency = dependency
@block = block
end
- attr_reader :dependency
- def execute_block(command_runner, argvalue)
- argvalue = parse_name(argvalue)
- @block.call(command_runner, argvalue)
+ def execute_block(command_runner, name)
+ name = parse_name(name)
+ @block.call(command_runner, name)
end
+ def to_s
+ " #{@sname}, #{@lname}=#{@dependency} #{@help}"
+ end
+
private
def parse_name(name)
if name.include?('=')
name.split('=')[1]
else
name[2..-1]
end
end
-end
+end