Class: Login::ArgumentsParser
- Defined in:
- lib/kanseishitsu/login/argument_parser.rb
Overview
Define the ArgumentsParser class
Constant Summary collapse
- UNDERSCORE_PATTERN =
%r{_}- HYPHEN_STRING =
'-'.freeze
- FLAGS =
i[ show_all list remove verbose].freeze
- POSITIONAL =
i[watch_path executable_path_with_args].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
-
.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) ⇒ Object
rubocop: disable Metrics/MethodLength.
Instance Method Summary collapse
- #banner ⇒ Object
- #demand(arg, positional: false) ⇒ Object
- #executable_path_with_args(args) ⇒ Object
-
#initialize(parser = OptionParser.new) ⇒ ArgumentsParser
constructor
A new instance of ArgumentsParser.
- #list ⇒ Object
- #options? ⇒ Boolean
- #positional!(args) ⇒ Object
- #remove ⇒ Object
- #show_all ⇒ Object
- #usage! ⇒ Object
- #verbose ⇒ Object
- #version ⇒ Object
- #watch_path(args) ⇒ Object
Constructor Details
#initialize(parser = OptionParser.new) ⇒ ArgumentsParser
Returns a new instance of ArgumentsParser.
24 25 26 27 28 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 24 def initialize(parser = OptionParser.new) @parser = parser = {} FLAGS.each { |method_name| self.method(method_name).call } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
22 23 24 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 22 def end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
22 23 24 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 22 def parser @parser end |
Class Method Details
.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) ⇒ Object
rubocop: disable Metrics/MethodLength
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 119 def self.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) arguments_parser.parser.parse!(args) if !arguments_parser. && ARGV.length != 2 = 'A directory and executable handler is required' raise OptionParser::MissingArgument, elsif !args.empty? arguments_parser.positional!(args) end arguments_parser. rescue OptionParser::AmbiguousOption => e abort e. rescue OptionParser::ParseError => e puts e. arguments_parser.usage! end |
Instance Method Details
#banner ⇒ Object
30 31 32 33 34 35 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 30 def @parser. = "Usage: #{File.basename($PROGRAM_NAME)} " \ '<watch_path> <executable_path_with_args>' @parser.separator '' @parser.separator 'Options:' end |
#demand(arg, positional: false) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 93 def demand(arg, positional: false) return [arg] unless [arg].nil? required_arg = if positional then "<#{arg}>" else "--#{arg.to_s.gsub(UNDERSCORE_PATTERN, HYPHEN_STRING)}" end raise OptionParser::MissingArgument, "Required argument: #{required_arg}" end |
#executable_path_with_args(args) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 81 def executable_path_with_args(args) executable, *args = args return if executable.nil? executable_path = Object.new.extend(Which).which(executable) if executable_path.nil? = "Executable not found: #{executable_path}" raise OptionParser::InvalidArgument, end [:executable_path_with_args] = [executable_path] + args end |
#list ⇒ Object
43 44 45 46 47 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 43 def list @parser.on('-l', '--list', 'List login job labels') do [:list] = true end end |
#options? ⇒ Boolean
109 110 111 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 109 def ArgumentsParser::FLAGS.any? { |flag| .include?(flag) } end |
#positional!(args) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 102 def positional!(args) POSITIONAL.each do |opt| self.method(opt).call(args) self.demand(opt, positional: true) end end |
#remove ⇒ Object
49 50 51 52 53 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 49 def remove @parser.on('-r', '--remove=<label>', 'Remove a login job by label') do |label| [:remove] = label end end |
#show_all ⇒ Object
37 38 39 40 41 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 37 def show_all @parser.on('--show-all', 'Show login jobs') do [:show_all] = true end end |
#usage! ⇒ Object
113 114 115 116 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 113 def usage! puts @parser exit end |
#verbose ⇒ Object
55 56 57 58 59 60 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 55 def verbose [:log_level] ||= Logger::INFO @parser.on_tail('-v', '--verbose', 'Increase verbosity') do [:log_level] -= 1 end end |
#version ⇒ Object
62 63 64 65 66 67 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 62 def version @parser.on_tail('--version', 'Show version') do puts "#{File.basename($PROGRAM_NAME)} version #{Kanseishitsu::VERSION}" exit end end |
#watch_path(args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kanseishitsu/login/argument_parser.rb', line 69 def watch_path(args) return if (v = args.shift).nil? watch_path = File.(v) unless File.exist?(watch_path) && File.directory?(watch_path) = "Directory not found: #{watch_path}" raise OptionParser::InvalidArgument, end [:watch_path] = [watch_path] end |