Class: L43::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/l43/opt_parser.rb,
lib/l43/opt_parser/flag.rb,
lib/l43/opt_parser/keyword.rb,
lib/l43/opt_parser/version.rb

Defined Under Namespace

Classes: Flag, Keyword

Constant Summary collapse

FlagRgx =
/\A:(.*)/
KeywordRgx =
/(.*):\z/
VERSION =
'0.0.2'

Instance Method Summary collapse

Instance Method Details

#flag(name, multiple: false) ⇒ Object

Constraints




16
17
18
19
20
21
22
# File 'lib/l43/opt_parser.rb', line 16

def flag(name, multiple: false)
  flags.merge!(symbolize(name) => Flag.new(symbolize(name), multiple:)) { |*|
    raise DuplicateFlagDefinition, "flag :#{name} is already defined"
  }
  @constrained = true
  self
end

#keyword(name, multiple: false, type: String, &initializer) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/l43/opt_parser.rb', line 24

def keyword(name, multiple: false, type: String, &initializer)
  keywords.merge!(name => Keyword.new(name, multiple:, type:, &initializer)) { |*| 
    raise DuplicateKeywordDefinition, "keyword :#{name} is already defined"
  }
  @constrained = true
  self
end

#parse(arguments) ⇒ Object

Business Logic




34
35
36
37
38
39
40
# File 'lib/l43/opt_parser.rb', line 34

def parse(arguments)
  reset_data!
  @arguments = arguments
  parse_arguments

  OpenStruct.new(args:, errors:, kwds:, ok?: errors.empty?).freeze
end