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

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

Към профила на Мила Бянкова

Резултати

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

Код

module CommandParserHelpMessageGenerator
def generate_help_message(arguments, options)
arguments_list = ""
arguments.each { |e| arguments_list.concat(" [#{e[:name]}]") }
result = "Usage: #{@command_name}#{arguments_list}"
options.each do |option|
result.concat("\n -#{option[:name]}, --#{option[:long_name]}")
result.concat("=#{option[:placeholder]}") if option[:placeholder] != nil
result.concat(" #{option[:description]}")
end
result
end
end
class CommandParser
include CommandParserHelpMessageGenerator
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(argument_name, &block)
@arguments.push({name: argument_name, block: block})
end
def option(name, long_name, description, placeholder = nil, &block)
@options.push(
{
name: name,
long_name: long_name,
description: description,
block: block,
placeholder: placeholder
}
)
end
alias_method :option_with_parameter, :option
def parse(command_runner, argv)
@arguments_copy = @arguments.dup
@command_runner = command_runner
argv.each { |e| parse_argument(e) }
end
def parse_argument(argument)
if argument.start_with?("--")
parse_long_option(argument.slice(2..-1))
elsif argument.start_with?('-')
parse_option(argument.slice(1..-1))
else
parse_parameter(argument)
end
end
def call_block(block, parameter)
block.call(@command_runner, parameter)
end
def parse_long_option(option)
option_split = option.split('=')
name = @options.find { |e| e[:long_name] == option_split[0] }
if name != nil
call_block name[:block], option_split.count == 2 ? option_split[1] : true
end
end
def parse_option(option)
name = @options.find { |e| e[:name] == option[0] }
if name != nil
call_block(name[:block], option.length > 1 ? option.slice(1..-1) : true)
end
end
def parse_parameter(argument)
call_block(@arguments_copy.shift[:block], argument)
end
def help
generate_help_message(@arguments, @options)
end
end

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

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

Finished in 0.00975 seconds
15 examples, 0 failures

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

Мила обнови решението на 07.11.2016 22:45 (преди над 7 години)

+module CommandParserHelpMessageGenerator
+ def generate_help_message(arguments, options)
+ arguments_list = ""
+ arguments.each { |e| arguments_list.concat("[#{e[:name]}]") }
+ result = "Usage: #{@command_name} #{arguments_list}"
+ options.each do |option|
+ result.concat("\n -#{option[:name]}, --#{option[:long_name]}")
+ result.concat("=#{option[:placeholder]}") if option[:placeholder] != nil
+ result.concat(" #{option[:description]}")
+ end
+ result
+ end
+end
+
+class CommandParser
+ include CommandParserHelpMessageGenerator
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = []
+ @options = []
+ end
+
+ def argument(argument_name, &block)
+ @arguments.push({name: argument_name, block: block})
+ end
+
+ def option(name, long_name, description, placeholder = nil, &block)
+ @options.push(
+ {
+ name: name,
+ long_name: long_name,
+ description: description,
+ block: block,
+ placeholder: placeholder
+ }
+ )
+ end
+
+ alias_method :option_with_parameter, :option
+
+ def parse(command_runner, argv)
+ @arguments_copy = @arguments.dup
+ @command_runner = command_runner
+ argv.each { |e| parse_argument(e) }
+ end
+
+ def parse_argument(argument)
+ if argument.start_with?("--")
+ parse_long_option(argument.slice(2..-1))
+ elsif argument.start_with?('-')
+ parse_option(argument.slice(1..-1))
+ else
+ parse_parameter(argument)
+ end
+ end
+
+ def call_block(block, parameter)
+ block.call(@command_runner, parameter)
+ end
+
+ def parse_long_option(option)
+ option_split = option.split('=')
+ call_block(
+ @options.find { |e| e[:long_name] == option_split[0] }[:block],
+ option_split.count == 2 ? option_split[1] : true
+ )
+ end
+
+ def parse_option(option)
+ call_block(
+ @options.find { |e| e[:name] == option[0] }[:block],
+ option.length > 1 ? option.slice(1..-1) : true
+ )
+ end
+
+ def parse_parameter(argument)
+ call_block(@arguments_copy.shift[:block], argument)
+ end
+
+ def help
+ generate_help_message(@arguments, @options)
+ end
+end

Мила обнови решението на 08.11.2016 20:29 (преди над 7 години)

module CommandParserHelpMessageGenerator
def generate_help_message(arguments, options)
arguments_list = ""
- arguments.each { |e| arguments_list.concat("[#{e[:name]}]") }
- result = "Usage: #{@command_name} #{arguments_list}"
+ arguments.each { |e| arguments_list.concat(" [#{e[:name]}]") }
+ result = "Usage: #{@command_name}#{arguments_list}"
options.each do |option|
result.concat("\n -#{option[:name]}, --#{option[:long_name]}")
result.concat("=#{option[:placeholder]}") if option[:placeholder] != nil
result.concat(" #{option[:description]}")
end
result
end
end
class CommandParser
include CommandParserHelpMessageGenerator
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(argument_name, &block)
@arguments.push({name: argument_name, block: block})
end
def option(name, long_name, description, placeholder = nil, &block)
@options.push(
{
name: name,
long_name: long_name,
description: description,
block: block,
placeholder: placeholder
}
)
end
alias_method :option_with_parameter, :option
def parse(command_runner, argv)
@arguments_copy = @arguments.dup
@command_runner = command_runner
argv.each { |e| parse_argument(e) }
end
def parse_argument(argument)
if argument.start_with?("--")
parse_long_option(argument.slice(2..-1))
elsif argument.start_with?('-')
parse_option(argument.slice(1..-1))
else
parse_parameter(argument)
end
end
def call_block(block, parameter)
block.call(@command_runner, parameter)
end
def parse_long_option(option)
option_split = option.split('=')
- call_block(
- @options.find { |e| e[:long_name] == option_split[0] }[:block],
- option_split.count == 2 ? option_split[1] : true
- )
+ name = @options.find { |e| e[:long_name] == option_split[0] }
+ if name != nil
+ call_block name[:block], option_split.count == 2 ? option_split[1] : true
+ end
end
def parse_option(option)
- call_block(
- @options.find { |e| e[:name] == option[0] }[:block],
- option.length > 1 ? option.slice(1..-1) : true
- )
+ name = @options.find { |e| e[:name] == option[0] }
+ if name != nil
+ call_block(name[:block], option.length > 1 ? option.slice(1..-1) : true)
+ end
end
def parse_parameter(argument)
call_block(@arguments_copy.shift[:block], argument)
end
def help
generate_help_message(@arguments, @options)
end
end