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

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

Към профила на Методи Димитров

Резултати

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

Код

class String
def option?
self[0] == '-' || self[0..1] == '--'
end
def format_option
if include? '--'
split '='
elsif include? '-'
[self[0..1], self[2..-1]]
end
end
end
class Option
attr_reader :short
attr_reader :long
attr_reader :description
attr_reader :block
def initialize(short, long, description, value, block)
@short = short
@long = long
@description = description
@value = value
@block = block
end
def help
help_text = "\n -#{@short}, --#{@long}"
help_text += "=#{@value}" if @value
help_text + " #{@description}"
end
end
class CommandParser
def initialize(command_name)
@command = command_name
@arguments = {}
@options = {}
end
def parse(command_runner, argv)
options = argv.select(&:option?)
argument_values = argv - options
@arguments.each_value.with_index do |block, index|
block[command_runner, argument_values[index] ]
end
options.map(&:format_option).each do |option|
if @options[option.first]
@options[option.first].block[command_runner, option.last]
end
end
end
def argument(arg, &block)
@arguments[arg] = block
end
def option(short_name, long_name, help_text, &block)
default_value_block = ->(caller, _) { block[caller, true] }
define_option(short_name, long_name, help_text, nil, default_value_block)
end
def define_option(short_name, long_name, help_text, value, block)
option = Option.new(short_name, long_name, help_text, value, block)
@options[ "-" + short_name ] = option
@options[ "--" + long_name ] = option
end
def option_with_parameter(short_name, long_name, help_text, value, &block)
define_option(short_name, long_name, help_text, value, block)
end
def help
help_text = "Usage: #{@command}"
unless @arguments.empty?
help_text += " [#{@arguments.keys.map(&:to_s).join("] [")}]"
end
help_text + @options.values.uniq.map(&:help).join
end
end

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

...............

Finished in 0.01034 seconds
15 examples, 0 failures

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

Методи обнови решението на 09.11.2016 11:52 (преди над 7 години)

+class String
+ def option?
+ self[0] == '-' || self[0..1] == '--'
+ end
+
+ def format_option
+ if include? '--'
+ split '='
+ elsif include? '-'
+ [self[0..1], self[2..-1]]
+ end
+ end
+end
+
+class Option
+ attr_reader :short
+ attr_reader :long
+ attr_reader :description
+ attr_reader :block
+
+ def initialize(short, long, description, value, block)
+ @short = short
+ @long = long
+ @description = description
+ @value = value
+ @block = block
+ end
+
+ def help
+ help_text = "\n -#{@short}, --#{@long}"
+ help_text += "=#{@value}" if @value
+ help_text + " #{@description}"
+ end
+end
+
+class CommandParser
+ def initialize(command_name)
+ @command = command_name
+ @arguments = {}
+ @options = {}
+ end
+
+ def parse(command_runner, argv)
+ options = argv.select(&:option?)
+ argument_values = argv - options
+ @arguments.each_value.with_index do |block, index|
+ block[command_runner, argument_values[index] ]
+ end
+ options.map(&:format_option).each do |option|
+ if @options[option.first]
+ @options[option.first].block[command_runner, option.last]
+ end
+ end
+ end
+
+ def argument(arg, &block)
+ @arguments[arg] = block
+ end
+
+ def option(short_name, long_name, help_text, &block)
+ default_value_block = ->(caller, _) { block[caller, true] }
+ define_option(short_name, long_name, help_text, nil, default_value_block)
+ end
+
+ def define_option(short_name, long_name, help_text, value, block)
+ option = Option.new(short_name, long_name, help_text, value, block)
+ @options[ "-" + short_name ] = option
+ @options[ "--" + long_name ] = option
+ end
+
+ def option_with_parameter(short_name, long_name, help_text, value, &block)
+ define_option(short_name, long_name, help_text, value, block)
+ end
+
+ def help
+ help_text = "Usage: #{@command}"
+ unless @arguments.empty?
+ help_text += " [#{@arguments.keys.map(&:to_s).join("] [")}]"
+ end
+ help_text + @options.values.uniq.map(&:help).join
+ end
+end