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

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

Към профила на Теодор Филипов

Резултати

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

Код

module ParserUtils
def process_operations_with_args
@argv.select { |el| (el.start_with?'-') && (option_has_arg? el) }
.map do |el|
@options_with_args.fetch(extract_option_name(el), @empty_block)
.call @runner, extract_option_value(el)
end
end
def process_operations
@argv.select { |el| (el.start_with? '-') && !(option_has_arg? el) }
.map do |el|
@options.fetch(extract_option_name(el), @empty_block)
.call(@runner, true)
end
end
def process_arguments
@argv.select { |el| !(el.start_with? '-') }.first(@argument_blocks.length)
.each_with_index
.map { |el, idx| @argument_blocks[idx].call(@runner, el) }
end
def option_has_arg?(a_option)
((!a_option.start_with? '--') && (a_option.length > 2)) ||
((a_option.start_with? '--') && ((a_option.index '=') != nil))
end
def extract_option_name(a_string)
return a_string[1] unless a_string.start_with? '--'
end_index = a_string.index '='
return a_string[2..-1] if end_index == nil
a_string[2..(end_index - 1)]
end
def extract_option_value(a_string)
return a_string[2..-1] unless a_string.start_with? '--'
start_index = (a_string.index '=') + 1
a_string[start_index..-1]
end
end
class CommandParser
include ParserUtils
def initialize(a_command_name)
@command_name = a_command_name
@argument_blocks = []
@options = {}
@options_with_args = {}
@arguments_doc = []
@options_doc = []
@options_with_args_doc = []
@empty_block = -> (*) {}
end
def argument(a_arg_name, &a_block)
@arguments_doc.push a_arg_name
@argument_blocks.push a_block
end
def option(a_shorthand, a_name, a_desc, &a_block)
@options[a_shorthand] = a_block
@options[a_name] = a_block
@options_doc.push "-#{a_shorthand}, --#{a_name} #{a_desc}"
end
def option_with_parameter(a_shorthand, a_name, a_desc, a_arg_name, &a_block)
@options_with_args[a_shorthand] = a_block
@options_with_args[a_name] = a_block
desc = "-#{a_shorthand}, --#{a_name}=#{a_arg_name} #{a_desc}"
@options_with_args_doc.push desc
end
def help
"Usage: #{@command_name}\

Качих кода в private gist, къде да ти пратя линк? А ако ги изнеса в променливи става грозно и нечетимо. Ако ги Събирам с + без да има променливи според мен е най-прегледно, но не и според rubocop.

#{@arguments_doc.reduce('') { |a, b| a + " [#{b}]" }}\
#{@options_doc.reduce('') { |a, b| a + "\n " + b }}\
#{@options_with_args_doc.reduce('') { |a, b| a + "\n " + b }}"
end
def parse(a_runner, argv)
@argv = argv
@runner = a_runner
process_arguments
process_operations
process_operations_with_args
end
end

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

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

Finished in 0.00863 seconds
15 examples, 0 failures

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

Теодор обнови решението на 05.11.2016 23:00 (преди около 8 години)

+module ParserUtils
+ def process_operations_with_args
+ @argv.select { |el| (el.start_with?'-') && (option_has_arg? el) }
+ .map do |el|
+ @options_with_args.fetch(extract_option_name(el), @empty_block)
+ .call @runner, extract_option_value(el)
+ end
+ end
+
+ def process_operations
+ @argv.select { |el| (el.start_with? '-') && !(option_has_arg? el) }
+ .map do |el|
+ @options.fetch(extract_option_name(el), @empty_block)
+ .call(@runner, true)
+ end
+ end
+
+ def process_arguments
+ @argv.select { |el| !(el.start_with? '-') }.first(@argument_blocks.length)
+ .each_with_index
+ .map { |el, idx| @argument_blocks[idx].call(@runner, el) }
+ end
+
+ def option_has_arg?(a_option)
+ ((!a_option.start_with? '--') && (a_option.length > 2)) ||
+ ((a_option.start_with? '--') && ((a_option.index '=') != nil))
+ end
+
+ def extract_option_name(a_string)
+ return a_string[1] unless a_string.start_with? '--'
+ end_index = a_string.index '='
+ return a_string[2..-1] if end_index == nil
+ a_string[2..(end_index - 1)]
+ end
+
+ def extract_option_value(a_string)
+ return a_string[2..-1] unless a_string.start_with? '--'
+ start_index = (a_string.index '=') + 1
+ a_string[start_index..-1]
+ end
+end
+
+class CommandParser
+ include ParserUtils
+
+ def initialize(a_command_name)
+ @command_name = a_command_name
+ @argument_blocks = []
+ @options = {}
+ @options_with_args = {}
+ @arguments_doc = []
+ @options_doc = []
+ @options_with_args_doc = []
+ @empty_block = -> (*) {}
+ end
+
+ def argument(a_arg_name, &a_block)
+ @arguments_doc.push a_arg_name
+ @argument_blocks.push a_block
+ end
+
+ def option(a_shorthand, a_name, a_desc, &a_block)
+ @options[a_shorthand] = a_block
+ @options[a_name] = a_block
+ @options_doc.push "-#{a_shorthand}, --#{a_name} #{a_desc}"
+ end
+
+ def option_with_parameter(a_shorthand, a_name, a_desc, a_arg_name, &a_block)
+ @options_with_args[a_shorthand] = a_block
+ @options_with_args[a_name] = a_block
+ desc = "-#{a_shorthand}, --#{a_name}=#{a_arg_name} #{a_desc}"
+ @options_with_args_doc.push desc
+ end
+
+ def help
+ "Usage: #{@command_name}\
+ #{@arguments_doc.reduce('') { |a, b| a + " [#{b}]" }}\
+ #{@options_doc.reduce('') { |a, b| a + "\n " + b }}\
+ #{@options_with_args_doc.reduce('') { |a, b| a + "\n " + b }}"
+ end
+
+ def parse(a_runner, argv)
+ @argv = argv
+ @runner = a_runner
+ process_arguments
+ process_operations
+ process_operations_with_args
+ end
+end

Теодор обнови решението на 05.11.2016 23:01 (преди около 8 години)

module ParserUtils
def process_operations_with_args
@argv.select { |el| (el.start_with?'-') && (option_has_arg? el) }
.map do |el|
@options_with_args.fetch(extract_option_name(el), @empty_block)
.call @runner, extract_option_value(el)
end
end
def process_operations
@argv.select { |el| (el.start_with? '-') && !(option_has_arg? el) }
.map do |el|
@options.fetch(extract_option_name(el), @empty_block)
.call(@runner, true)
end
end
def process_arguments
@argv.select { |el| !(el.start_with? '-') }.first(@argument_blocks.length)
.each_with_index
.map { |el, idx| @argument_blocks[idx].call(@runner, el) }
end
def option_has_arg?(a_option)
((!a_option.start_with? '--') && (a_option.length > 2)) ||
((a_option.start_with? '--') && ((a_option.index '=') != nil))
end
def extract_option_name(a_string)
return a_string[1] unless a_string.start_with? '--'
end_index = a_string.index '='
return a_string[2..-1] if end_index == nil
a_string[2..(end_index - 1)]
end
def extract_option_value(a_string)
return a_string[2..-1] unless a_string.start_with? '--'
start_index = (a_string.index '=') + 1
a_string[start_index..-1]
end
end
class CommandParser
include ParserUtils
def initialize(a_command_name)
@command_name = a_command_name
@argument_blocks = []
@options = {}
@options_with_args = {}
@arguments_doc = []
@options_doc = []
@options_with_args_doc = []
@empty_block = -> (*) {}
end
def argument(a_arg_name, &a_block)
@arguments_doc.push a_arg_name
@argument_blocks.push a_block
end
def option(a_shorthand, a_name, a_desc, &a_block)
@options[a_shorthand] = a_block
@options[a_name] = a_block
@options_doc.push "-#{a_shorthand}, --#{a_name} #{a_desc}"
end
def option_with_parameter(a_shorthand, a_name, a_desc, a_arg_name, &a_block)
@options_with_args[a_shorthand] = a_block
@options_with_args[a_name] = a_block
desc = "-#{a_shorthand}, --#{a_name}=#{a_arg_name} #{a_desc}"
@options_with_args_doc.push desc
end
def help
"Usage: #{@command_name}\

Качих кода в private gist, къде да ти пратя линк? А ако ги изнеса в променливи става грозно и нечетимо. Ако ги Събирам с + без да има променливи според мен е най-прегледно, но не и според rubocop.

- #{@arguments_doc.reduce('') { |a, b| a + " [#{b}]" }}\
- #{@options_doc.reduce('') { |a, b| a + "\n " + b }}\
- #{@options_with_args_doc.reduce('') { |a, b| a + "\n " + b }}"
+#{@arguments_doc.reduce('') { |a, b| a + " [#{b}]" }}\
+#{@options_doc.reduce('') { |a, b| a + "\n " + b }}\
+#{@options_with_args_doc.reduce('') { |a, b| a + "\n " + b }}"
end
def parse(a_runner, argv)
@argv = argv
@runner = a_runner
process_arguments
process_operations
process_operations_with_args
end
end