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

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

Към профила на Иван Станков

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 13 успешни тест(а)
  • 2 неуспешни тест(а)

Код

module CustomParser
def add_argument(runner, arguments)
arguments.each_index { |i| check_argument(runner, arguments[i], i) }
end
private
def parse_argument(runner, arguments, hash, index)
hash[hash.keys[index]] = arguments
runner.merge! hash
end
def parse_option(runner, hash)
runner.merge! hash
end
def parse_option_with_param(runner, argument, hash, index)
hash[hash.keys[index]] = argument.slice(
argument.index('=') + 1,
argument.length
).to_s
runner.merge! hash
end
def check_argument(runner, argument, index)
if argument.include? '='
parse_option_with_param(runner, argument, @arguments, index)
elsif argument.include? '-'
parse_option(runner, @options)
else
parse_argument(runner, argument, @arguments, index)
end
end
end
module CommandParserHelper
def generate_help
output = ["Usage: #{@name}", ""]
@argv.each { |arguments| output_argument(arguments, output) }
output[0] + output[1]
end
private
def output_argument(arguments, output)
if arguments.is_a? Array
print_array_help(arguments, output)
else
output[0] += " [#{arguments}]"
end
end
def print_array_help(array, output)
if array.count == 4
output[1] += "\n -#{array[0]}, --#{array[1]}=#{array[3]} #{array[2]}"
else
output[1] += "\n -#{array[0]}, --#{array[1]} #{array[2]}"
end
end
end
class CommandParser
include CustomParser
include CommandParserHelper
def initialize(command_name)
@name = command_name
@options = {}
@arguments = {}
@argv = []
end
def parse(command_runner, arguments = [])
add_argument(command_runner, arguments)
end
def argument(name)
@argv.push name
yield(@arguments, name)
end
def option(short, full, description)
@argv.push [short, full, description]
# Вторият аргумент на блока - value - винаги трябва да има стойност true
yield(@options, true)

Това ок ли е? Правилно ли интерпретирам коментара от условието?

p.s. отговорих си на поредния глупав въпрос... Реално това важи за helper-a и ако на command_runner подадем опция, то тогава тя е true... Просто съм се надъхал, а някои неща са ми малко объркващи. Извинявайте за спам-а xD

end
def option_with_parameter(short, full, description, parameter)
@argv.push [short, full, description, parameter]
yield(@arguments, parameter)
end
def help
generate_help
end
def to_s
@arguments
end
end

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

FILE 2
.....F.......F.

Failures:

  1) CommandParser#option_with_parameter parses a option with parameter in short format
     Failure/Error: expect(command_runner[:sort]).to eq 'time'
       
       expected: "time"
            got: :default
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"time"
       +:default
     # /tmp/d20161113-27983-1t6srbc/spec.rb:89: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)>'

  2) CommandParser when having options with and without values and parameters parses all the options and arguments correctly
     Failure/Error: expect(command_runner[:first_file]).to eq 'first.rb'
       
       expected: "first.rb"
            got: "FIRST FILE"
       
       (compared using ==)
     # /tmp/d20161113-27983-1t6srbc/spec.rb:171: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.00899 seconds
15 examples, 2 failures

Failed examples:

rspec /tmp/d20161113-27983-1t6srbc/spec.rb:83 # CommandParser#option_with_parameter parses a option with parameter in short format
rspec /tmp/d20161113-27983-1t6srbc/spec.rb:168 # CommandParser when having options with and without values and parameters parses all the options and arguments correctly

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

Иван обнови решението на 04.11.2016 07:43 (преди над 7 години)

+module CustomParser
+ def parse_argument(runner, arguments, hash, index)
+ hash[hash.keys[index]] = arguments
+ runner.merge! hash
+ end
+
+ def parse_option(runner, hash)
+ runner.merge! hash
+ end
+
+ def parse_option_with_param(runner, argument, hash, index)
+ hash[hash.keys[index]] = argument.slice(
+ argument.index('=') + 1,
+ argument.length
+ ).to_s
+ runner.merge! hash
+ end
+
+ def an_option?(arguments)
+ arguments.include? '-'
+ end
+
+ def an_option_with_parameter?(arguments)
+ arguments.include? '='
+ end
+
+ def add_argument(runner, arguments)
+ # arg_keys = runner.keys
+ arguments.each_index do |i|
+ check_argument(runner, arguments[i], i)
+ end
+ end
+
+ def check_argument(runner, argument, index)
+ if an_option_with_parameter?(argument)
+ parse_option_with_param(runner, argument, @arguments, index)
+ elsif an_option?(argument)
+ parse_option(runner, @options)
+ else
+ parse_argument(runner, argument, @arguments, index)
+ end
+ end
+end
+
+module CommandParserHelper
+end
+
+class CommandParser
+ include CustomParser
+ include CommandParserHelper
+
+ def initialize(command_name)
+ @name = command_name
+ @options = {}
+ @arguments = {}
+ @argv = []
+ @runner
+ end
+
+ def parse(command_runner, arguments = [])
+
+ add_argument(command_runner, arguments)
+
+ end
+
+ def argument(name)
+ @argv.push name
+ yield(@arguments, name)
+ end
+
+ def option(short, full, description)
+ @argv.push [short, full, description]
+ # Вторият аргумент на блока - value - винаги трябва да има стойност true
+ yield(@options, true)

Това ок ли е? Правилно ли интерпретирам коментара от условието?

p.s. отговорих си на поредния глупав въпрос... Реално това важи за helper-a и ако на command_runner подадем опция, то тогава тя е true... Просто съм се надъхал, а някои неща са ми малко объркващи. Извинявайте за спам-а xD

+ end
+
+ def option_with_parameter(short, full, description, parameter)
+ @argv.push [short, full, description, parameter]
+ yield(@arguments, parameter)
+ end
+
+ def help
+ puts "Usage: HELP "
+ output = "Usage: " + @name
+ output
+ end
+
+ def to_s
+ @arguments
+ end
+end

Иван обнови решението на 05.11.2016 13:18 (преди над 7 години)

module CustomParser
def parse_argument(runner, arguments, hash, index)
hash[hash.keys[index]] = arguments
runner.merge! hash
end
def parse_option(runner, hash)
runner.merge! hash
end
def parse_option_with_param(runner, argument, hash, index)
hash[hash.keys[index]] = argument.slice(
argument.index('=') + 1,
argument.length
).to_s
runner.merge! hash
end
def an_option?(arguments)
arguments.include? '-'
end
def an_option_with_parameter?(arguments)
arguments.include? '='
end
def add_argument(runner, arguments)
- # arg_keys = runner.keys
arguments.each_index do |i|
check_argument(runner, arguments[i], i)
end
end
def check_argument(runner, argument, index)
if an_option_with_parameter?(argument)
parse_option_with_param(runner, argument, @arguments, index)
elsif an_option?(argument)
parse_option(runner, @options)
else
parse_argument(runner, argument, @arguments, index)
end
end
end
module CommandParserHelper
+ def generate_help(command_name, argv = [])
+ output = ["Usage: #{command_name}", ""]
+ work_array = argv
+ work_array.each { |arguments| output_argument(arguments, output) }
+ output[0] + output[1]
+ end
+
+ private
+
+ def output_argument(arguments, output)
+ if arguments.is_a? Array
+ print_array_help(arguments, output)
+ else
+ output[0] += " [#{arguments}]"
+ end
+ # p "Argument is: #{arguments} and is array #{arguments.is_a? Array}"
+ end
+
+ def print_array_help(array, output)
+ if array.count == 4
+ output[1] += "\n -#{array[0]}, --#{array[1]}=#{array[3]} #{array[2]}"
+ else
+ output[1] += "\n -#{array[0]}, --#{array[1]} #{array[2]}"
+ end
+ end
end
class CommandParser
include CustomParser
include CommandParserHelper
def initialize(command_name)
@name = command_name
@options = {}
@arguments = {}
@argv = []
@runner
end
def parse(command_runner, arguments = [])
add_argument(command_runner, arguments)
end
def argument(name)
@argv.push name
yield(@arguments, name)
end
def option(short, full, description)
@argv.push [short, full, description]
# Вторият аргумент на блока - value - винаги трябва да има стойност true
yield(@options, true)
end
def option_with_parameter(short, full, description, parameter)
@argv.push [short, full, description, parameter]
yield(@arguments, parameter)
end
def help
- puts "Usage: HELP "
- output = "Usage: " + @name
- output
+ generate_help(@name, @argv)
end
def to_s
@arguments
end
end

Иван обнови решението на 05.11.2016 13:38 (преди над 7 години)

module CustomParser
+ def add_argument(runner, arguments)
+ arguments.each_index { |i| check_argument(runner, arguments[i], i) }
+ end
+
+ private
+
def parse_argument(runner, arguments, hash, index)
hash[hash.keys[index]] = arguments
runner.merge! hash
end
def parse_option(runner, hash)
runner.merge! hash
end
def parse_option_with_param(runner, argument, hash, index)
hash[hash.keys[index]] = argument.slice(
argument.index('=') + 1,
argument.length
).to_s
runner.merge! hash
end
- def an_option?(arguments)
- arguments.include? '-'
- end
-
- def an_option_with_parameter?(arguments)
- arguments.include? '='
- end
-
- def add_argument(runner, arguments)
- arguments.each_index do |i|
- check_argument(runner, arguments[i], i)
- end
- end
-
def check_argument(runner, argument, index)
- if an_option_with_parameter?(argument)
+ if argument.include? '='
parse_option_with_param(runner, argument, @arguments, index)
- elsif an_option?(argument)
+ elsif argument.include? '-'
parse_option(runner, @options)
else
parse_argument(runner, argument, @arguments, index)
end
end
end
module CommandParserHelper
- def generate_help(command_name, argv = [])
- output = ["Usage: #{command_name}", ""]
- work_array = argv
- work_array.each { |arguments| output_argument(arguments, output) }
+ def generate_help
+ output = ["Usage: #{@name}", ""]
+ @argv.each { |arguments| output_argument(arguments, output) }
output[0] + output[1]
end
private
def output_argument(arguments, output)
if arguments.is_a? Array
print_array_help(arguments, output)
else
output[0] += " [#{arguments}]"
end
- # p "Argument is: #{arguments} and is array #{arguments.is_a? Array}"
end
def print_array_help(array, output)
if array.count == 4
output[1] += "\n -#{array[0]}, --#{array[1]}=#{array[3]} #{array[2]}"
else
output[1] += "\n -#{array[0]}, --#{array[1]} #{array[2]}"
end
end
end
class CommandParser
include CustomParser
include CommandParserHelper
def initialize(command_name)
@name = command_name
@options = {}
@arguments = {}
@argv = []
@runner
end
def parse(command_runner, arguments = [])
-
add_argument(command_runner, arguments)
-
end
def argument(name)
@argv.push name
yield(@arguments, name)
end
def option(short, full, description)
@argv.push [short, full, description]
# Вторият аргумент на блока - value - винаги трябва да има стойност true
yield(@options, true)
end
def option_with_parameter(short, full, description, parameter)
@argv.push [short, full, description, parameter]
yield(@arguments, parameter)
end
def help
- generate_help(@name, @argv)
+ generate_help
end
def to_s
@arguments
end
end

Иван обнови решението на 05.11.2016 13:39 (преди над 7 години)

module CustomParser
def add_argument(runner, arguments)
arguments.each_index { |i| check_argument(runner, arguments[i], i) }
end
private
def parse_argument(runner, arguments, hash, index)
hash[hash.keys[index]] = arguments
runner.merge! hash
end
def parse_option(runner, hash)
runner.merge! hash
end
def parse_option_with_param(runner, argument, hash, index)
hash[hash.keys[index]] = argument.slice(
argument.index('=') + 1,
argument.length
).to_s
runner.merge! hash
end
def check_argument(runner, argument, index)
if argument.include? '='
parse_option_with_param(runner, argument, @arguments, index)
elsif argument.include? '-'
parse_option(runner, @options)
else
parse_argument(runner, argument, @arguments, index)
end
end
end
module CommandParserHelper
def generate_help
output = ["Usage: #{@name}", ""]
@argv.each { |arguments| output_argument(arguments, output) }
output[0] + output[1]
end
private
def output_argument(arguments, output)
if arguments.is_a? Array
print_array_help(arguments, output)
else
output[0] += " [#{arguments}]"
end
end
def print_array_help(array, output)
if array.count == 4
output[1] += "\n -#{array[0]}, --#{array[1]}=#{array[3]} #{array[2]}"
else
output[1] += "\n -#{array[0]}, --#{array[1]} #{array[2]}"
end
end
end
class CommandParser
include CustomParser
include CommandParserHelper
def initialize(command_name)
@name = command_name
@options = {}
@arguments = {}
@argv = []
- @runner
end
def parse(command_runner, arguments = [])
add_argument(command_runner, arguments)
end
def argument(name)
@argv.push name
yield(@arguments, name)
end
def option(short, full, description)
@argv.push [short, full, description]
# Вторият аргумент на блока - value - винаги трябва да има стойност true
yield(@options, true)
end
def option_with_parameter(short, full, description, parameter)
@argv.push [short, full, description, parameter]
yield(@arguments, parameter)
end
def help
generate_help
end
def to_s
@arguments
end
end