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

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

Към профила на Михаил Здравков

Резултати

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

Код

module CommandLineArgumentMatchers
def argument?(arg)
arg.match /\A[^-].+\z/
end
def option?(arg)
arg.match(/\A-\w\S+??\z/) || arg.match(/\A--\w+(=\S+)?\z/)
end
def split_option_from_parameter(arg)
match_data = arg.match(/\A(?<name>-\w)(?<value>\S+??)\z/)
match_data ||= arg.match(/\A(?<name>--\w+)(=(?<value>\S+))?\z/)
[match_data[:name], match_data[:value]]
end
def help_argument_list(arguments)
arguments.map { |a| "[#{a}]" }.join(' ')
end
end
class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(name, &block)
@arguments.push name: name, block: block
end
def option(short_name, long_name, description, &block)
@options.push short_name: short_name, long_name: long_name,
description: description, block: block
end
def option_with_parameter(short_name, long_name, description, param, &block)
@options.push short_name: short_name, long_name: long_name,
parameter: param, description: description,
block: block
end
def parse(command_runner, argv)
parse_arguments command_runner, argv
parse_options command_runner, argv
end
def help
arguments = @arguments.map { |a| a[:name] }
message = "Usage: #{@command_name} " + help_argument_list(arguments)
@options.each do |o|
message += "\n -#{o[:short_name]}, --#{o[:long_name]}"
message += '=' + o[:parameter] if o[:parameter]
message += ' ' + o[:description]
end
message
end
private
def parse_arguments(command_runner, argv)
arguments = argv.select { |arg| argument? arg }
arguments.each_with_index do |value, i|
@arguments[i][:block].call(command_runner, value)
end
end
def parse_options(command_runner, argv)
options = argv.select { |arg| option? arg }
options.each do |option|
name, value = split_option_from_parameter option
value = nil if value == ""
match = @options.find { |o| option_matches? o, name }
match[:block].call(command_runner, value || true) if match
end
end
def option_matches?(option, argument)
matches_short_name = '-' + option[:short_name] == argument
matches_long_name = '--' + option[:long_name] == argument
matches_short_name || matches_long_name
end
include CommandLineArgumentMatchers
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-199hdhq/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.00881 seconds
15 examples, 1 failure

Failed examples:

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

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

Михаил обнови решението на 02.11.2016 02:18 (преди около 8 години)

+module CommandLineArgumentMatchers
+ def argument?(arg)
+ arg.match /\A[^-]\S+\z/
+ end
+
+ def option?(arg)
+ arg.match(/\A-\w\S+?\z/) || arg.match(/\A--\w+(=\S+)?\z/)
+ end
+
+ def split_option_from_parameter(arg)
+ match_data = arg.match(/\A(?<name>-\w)(?<value>\S+?)\z/)
+ match_data ||= arg.match(/\A(?<name>--\w+)(=(?<value>\S+))?\z/)
+ [match_data[:name], match_data[:value]]
+ end
+end
+
+class CommandParser
+ def initialize(command_name)
+ @command_name = command_name
+ @arguments = []
+ @options = []
+ end
+
+ def argument(name, &block)
+ @arguments.push name: name, block: block
+ end
+
+ def option(short_name, long_name, description, &block)
+ @options.push short_name: short_name, long_name: long_name,
+ description: description, block: block
+ end
+
+ def option_with_parameter(short_name, long_name, description, param, &block)
+ @options.push short_name: short_name, long_name: long_name,
+ parameter: param, description: description,
+ block: block
+ end
+
+ def parse(command_runner, argv)
+ parse_arguments command_runner, argv
+ parse_options command_runner, argv
+ end
+
+ def help
+ arguments = @arguments.map { |arg| arg[:name] }
+ message = "Usage: #{@command_name} [#{arguments.join(' ')}]\n"
+ @options.each do |o|
+ message += " -#{o[:short_name]}, --#{o[:long_name]}"
+ message += '=' + o[:parameter] if o[:parameter]
+ message += ' ' + o[:description] + "\n"
+ end
+ message
+ end
+
+ private
+
+ def parse_arguments(command_runner, argv)
+ arguments = argv.select { |arg| argument? arg }
+ arguments.each_with_index do |value, i|
+ @arguments[i][:block].call(command_runner, value)
+ end
+ end
+
+ def parse_options(command_runner, argv)
+ options = argv.select { |arg| option? arg }
+ options.each do |option|
+ name, value = split_option_from_parameter option
+ match = @options.find { |o| option_matches? o, name }
+ match[:block].call(command_runner, value || true) if match
+ end
+ end
+
+ def option_matches?(option, argument)
+ matches_short_name = '-' + option[:short_name] == argument
+ matches_long_name = '--' + option[:long_name] == argument
+ matches_short_name || matches_long_name
+ end
+
+ include CommandLineArgumentMatchers
+end

Михаил обнови решението на 09.11.2016 08:55 (преди около 8 години)

module CommandLineArgumentMatchers
def argument?(arg)
arg.match /\A[^-]\S+\z/
end
def option?(arg)
- arg.match(/\A-\w\S+?\z/) || arg.match(/\A--\w+(=\S+)?\z/)
+ arg.match(/\A-\w\S+??\z/) || arg.match(/\A--\w+(=\S+)?\z/)
end
def split_option_from_parameter(arg)
- match_data = arg.match(/\A(?<name>-\w)(?<value>\S+?)\z/)
+ match_data = arg.match(/\A(?<name>-\w)(?<value>\S+??)\z/)
match_data ||= arg.match(/\A(?<name>--\w+)(=(?<value>\S+))?\z/)
[match_data[:name], match_data[:value]]
end
+
+ def help_argument_list(arguments)
+ arguments.map { |a| "[#{a}]" }.join(' ')
+ end
end
class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(name, &block)
@arguments.push name: name, block: block
end
def option(short_name, long_name, description, &block)
@options.push short_name: short_name, long_name: long_name,
description: description, block: block
end
def option_with_parameter(short_name, long_name, description, param, &block)
@options.push short_name: short_name, long_name: long_name,
parameter: param, description: description,
block: block
end
def parse(command_runner, argv)
parse_arguments command_runner, argv
parse_options command_runner, argv
end
def help
- arguments = @arguments.map { |arg| arg[:name] }
- message = "Usage: #{@command_name} [#{arguments.join(' ')}]\n"
+ arguments = @arguments.map { |a| a[:name] }
+ message = "Usage: #{@command_name} " + help_argument_list(arguments)
@options.each do |o|
- message += " -#{o[:short_name]}, --#{o[:long_name]}"
+ message += "\n -#{o[:short_name]}, --#{o[:long_name]}"
message += '=' + o[:parameter] if o[:parameter]
- message += ' ' + o[:description] + "\n"
+ message += ' ' + o[:description]
end
message
end
private
def parse_arguments(command_runner, argv)
arguments = argv.select { |arg| argument? arg }
arguments.each_with_index do |value, i|
@arguments[i][:block].call(command_runner, value)
end
end
def parse_options(command_runner, argv)
options = argv.select { |arg| option? arg }
options.each do |option|
name, value = split_option_from_parameter option
+ value = nil if value == ""
match = @options.find { |o| option_matches? o, name }
match[:block].call(command_runner, value || true) if match
end
end
def option_matches?(option, argument)
matches_short_name = '-' + option[:short_name] == argument
matches_long_name = '--' + option[:long_name] == argument
matches_short_name || matches_long_name
end
include CommandLineArgumentMatchers
-end
+end

Михаил обнови решението на 09.11.2016 12:14 (преди около 8 години)

module CommandLineArgumentMatchers
def argument?(arg)
- arg.match /\A[^-]\S+\z/
+ arg.match /\A[^-].+\z/
end
def option?(arg)
arg.match(/\A-\w\S+??\z/) || arg.match(/\A--\w+(=\S+)?\z/)
end
def split_option_from_parameter(arg)
match_data = arg.match(/\A(?<name>-\w)(?<value>\S+??)\z/)
match_data ||= arg.match(/\A(?<name>--\w+)(=(?<value>\S+))?\z/)
[match_data[:name], match_data[:value]]
end
def help_argument_list(arguments)
arguments.map { |a| "[#{a}]" }.join(' ')
end
end
class CommandParser
def initialize(command_name)
@command_name = command_name
@arguments = []
@options = []
end
def argument(name, &block)
@arguments.push name: name, block: block
end
def option(short_name, long_name, description, &block)
@options.push short_name: short_name, long_name: long_name,
description: description, block: block
end
def option_with_parameter(short_name, long_name, description, param, &block)
@options.push short_name: short_name, long_name: long_name,
parameter: param, description: description,
block: block
end
def parse(command_runner, argv)
parse_arguments command_runner, argv
parse_options command_runner, argv
end
def help
arguments = @arguments.map { |a| a[:name] }
message = "Usage: #{@command_name} " + help_argument_list(arguments)
@options.each do |o|
message += "\n -#{o[:short_name]}, --#{o[:long_name]}"
message += '=' + o[:parameter] if o[:parameter]
message += ' ' + o[:description]
end
message
end
private
def parse_arguments(command_runner, argv)
arguments = argv.select { |arg| argument? arg }
arguments.each_with_index do |value, i|
@arguments[i][:block].call(command_runner, value)
end
end
def parse_options(command_runner, argv)
options = argv.select { |arg| option? arg }
options.each do |option|
name, value = split_option_from_parameter option
value = nil if value == ""
match = @options.find { |o| option_matches? o, name }
match[:block].call(command_runner, value || true) if match
end
end
def option_matches?(option, argument)
matches_short_name = '-' + option[:short_name] == argument
matches_long_name = '--' + option[:long_name] == argument
matches_short_name || matches_long_name
end
include CommandLineArgumentMatchers
end