Class: Client::ArgumentsParser
Overview
The ArgumentsParser class
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
- #banner ⇒ Object
- #help ⇒ Object
- #host ⇒ Object
-
#initialize(parser = OptionParser.new, options = ::Client.client_config.dup) ⇒ ArgumentsParser
constructor
A new instance of ArgumentsParser.
- #log_level ⇒ Object
- #parse_positional_arguments! ⇒ Object
- #port ⇒ Object
- #ssl ⇒ Object
- #validated_port(val, integer_pattern = /^\d+$/) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(parser = OptionParser.new, options = ::Client.client_config.dup) ⇒ ArgumentsParser
Returns a new instance of ArgumentsParser.
462 463 464 465 466 467 |
# File 'lib/client.rb', line 462 def initialize(parser = OptionParser.new, = ::Client.client_config.dup) @parser = parser @options = @flags = %i[banner port ssl log_level help version] @flags.each { |method_name| method(method_name)&.call if respond_to?(method_name) } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
460 461 462 |
# File 'lib/client.rb', line 460 def @options end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
460 461 462 |
# File 'lib/client.rb', line 460 def parser @parser end |
Instance Method Details
#banner ⇒ Object
469 470 471 472 473 |
# File 'lib/client.rb', line 469 def @parser. = "Usage: #{File.basename($PROGRAM_NAME)} [options] <hostname> [port]" @parser.separator '' @parser.separator 'Options:' end |
#help ⇒ Object
509 510 511 512 513 514 |
# File 'lib/client.rb', line 509 def help @parser.on_tail('-?', '--help', 'Show this message') do puts @parser exit end end |
#host ⇒ Object
475 476 477 478 479 480 |
# File 'lib/client.rb', line 475 def host description = "Connect to server at this host; default: #{@options[:host]}" @parser.on('-h', '--hostname=<hostname>', description) do |v| @options[:host] = v end end |
#log_level ⇒ Object
502 503 504 505 506 507 |
# File 'lib/client.rb', line 502 def log_level @parser.on_tail('-v', '--verbose', 'Increase verbosity') do @options[:log_level] ||= 0 @options[:log_level] -= 1 end end |
#parse_positional_arguments! ⇒ Object
523 524 525 526 527 528 |
# File 'lib/client.rb', line 523 def parse_positional_arguments! @options[:host] = ARGV.shift or raise OptionParser::MissingArgument, 'hostname' return if (given_port = ARGV.shift&.to_i).nil? @options[:port] = validated_port(given_port).to_i end |
#port ⇒ Object
489 490 491 492 493 494 |
# File 'lib/client.rb', line 489 def port description = "Connect to server at this port; default: #{@options[:port]}" @parser.on('-p', '--port=<port>', Integer, description) do |v| @options[:port] = validated_port(v).to_i end end |
#ssl ⇒ Object
496 497 498 499 500 |
# File 'lib/client.rb', line 496 def ssl @parser.on('--ssl', "Secure connection with TLSv1.3; default: #{@options[:ssl]}") do @options[:ssl] = true end end |
#validated_port(val, integer_pattern = /^\d+$/) ⇒ Object
482 483 484 485 486 487 |
# File 'lib/client.rb', line 482 def validated_port(val, integer_pattern = /^\d+$/) raise OptionParser::InvalidArgument, "Invalid port: #{val}" unless \ integer_pattern.match?(val.to_s) && val.positive? && val < 65_536 val end |