Class: L43::OptParser

Inherits:
Object
  • Object
show all
Includes:
Color::Output, Core, Memos, Meta
Defined in:
lib/l43/opt_parser.rb,
lib/l43/opt_parser/flag.rb,
lib/l43/opt_parser/meta.rb,
lib/l43/opt_parser/memos.rb,
lib/l43/opt_parser/errors.rb,
lib/l43/opt_parser/version.rb,
lib/l43/opt_parser/description.rb,
lib/l43/opt_parser/keyword.rb

Defined Under Namespace

Modules: Memos, Meta Classes: Description, Flag, Keyword

Constant Summary collapse

FlagRgx =
/\A:(.*)/
KeywordRgx =
/(.*):\z/
ShortFlagRgx =
/\A\-(.*)/
BadFormat =
Class.new(RuntimeError)
DuplicateFlagDefinition =
Class.new(RuntimeError)
DuplicateKeywordDefinition =
Class.new(RuntimeError)
FailedCheck =
Class.new(RuntimeError)
MissingValue =
Class.new(RuntimeError)
UndefinedFlag =
Class.new(RuntimeError)
UndefinedKwd =
Class.new(RuntimeError)
VERSION =
'0.3.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Meta

#make_open_object

Methods included from Memos

#aliases, #args, #checks, #defaults, #errors, #flags, #hooks, #keywords, #keywords_values

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



29
30
31
# File 'lib/l43/opt_parser.rb', line 29

def arguments
  @arguments
end

#constrainedObject (readonly)

Returns the value of attribute constrained.



29
30
31
# File 'lib/l43/opt_parser.rb', line 29

def constrained
  @constrained
end

#descriptionObject (readonly)

Returns the value of attribute description.



29
30
31
# File 'lib/l43/opt_parser.rb', line 29

def description
  @description
end

#kwds_nameObject (readonly)

Returns the value of attribute kwds_name.



29
30
31
# File 'lib/l43/opt_parser.rb', line 29

def kwds_name
  @kwds_name
end

Instance Method Details

#add_brObject

Documentation




75
76
77
78
# File 'lib/l43/opt_parser.rb', line 75

def add_br
  description.add_line
  self
end

#add_desc(*chunks) ⇒ Object



80
81
82
83
# File 'lib/l43/opt_parser.rb', line 80

def add_desc(*chunks) 
  description.add_desc(*chunks)
  self
end

#descriptionsObject

Services




93
# File 'lib/l43/opt_parser.rb', line 93

def descriptions = description.lines

#flag(name, short = nil, **kwds) ⇒ Object

Constraints




33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/l43/opt_parser.rb', line 33

def flag(name, short=nil, **kwds)
  f = Flag.new(name:, short:, **kwds)
  flags.merge!(name => f) { |*|
    raise DuplicateFlagDefinition, "flag :#{name} is already defined"
  }
  if short
    aliases.merge!(short => f)  {|*|
      raise DuplicateFlagDefinition, "flag :#{short} is already defined"
    }
  end
  @constrained = true
  description.add_flag(f)
  self
end

#help(device: $stderr) ⇒ Object



94
# File 'lib/l43/opt_parser.rb', line 94

def help(device: $stderr) = cs_put(descriptions, device:)

#keyword(name, short = nil, as: nil, default: Core::None, multiple: false, **ks, &init) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/l43/opt_parser.rb', line 49

def keyword(name, short=nil, as: nil, default: Core::None, multiple: false, **ks, &init)
  raise ArgumentError, "must not define a default for multiple keyword :#{name}" if
    multiple && default != Core::None

  k = Keyword.new(name:, short:, init:, default:, as:, multiple:, **ks)
  keywords.merge!(name => k) { |*|
    raise DuplicateKeywordDefinition, "keyword :#{name} is already defined"
  }
  if short
    aliases.merge!(short => k)  {|*|
      raise DuplicateKeywordDefinition, "keyword :#{short} is already defined"
    }
  end
  if default != Core::None
    defaults.merge!(k.real_name => default)
  end
  if multiple
    keywords_values[k.real_name] = []
  end
  @constrained = true
  description.add_kwd(k)
  self
end

#parse(arguments) ⇒ Object

Business Logic




99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/l43/opt_parser.rb', line 99

def parse(arguments)
  @arguments = arguments
  parse_arguments

  return Result.error(errors) unless errors.empty?
  if constrained
    mo = make_open_object
    kwds = mo.new(**defaults.merge(keywords_values))
    post_checks!(kwds)
    kwds = run_hooks(kwds)
    result = OpenStruct.new(kwds_name => kwds, args:, errors: errors.uniq).freeze
    Result.ok(result)
  else
    Result.ok(OpenStruct.new(args:, errors:, kwds: OpenStruct.new(**keywords_values)).freeze)
  end
end

#post_check(name = nil, &blk) ⇒ Object



121
122
123
124
# File 'lib/l43/opt_parser.rb', line 121

def post_check(name = nil, &blk)
  checks << [name, blk]
  self
end

#post_hook(&blk) ⇒ Object



116
117
118
119
# File 'lib/l43/opt_parser.rb', line 116

def post_hook(&blk)
  hooks << blk
  self
end

#section(line, indent: 1, color: [:bold, :green]) ⇒ Object



85
86
87
88
# File 'lib/l43/opt_parser.rb', line 85

def section(line, indent: 1, color: [:bold, :green])
  description.add_section(line, indent:, color:)
  self
end

#usage(name, options: nil, args: nil, nls: 0) ⇒ Object



126
127
128
129
# File 'lib/l43/opt_parser.rb', line 126

def usage(name, options: nil, args: nil, nls: 0)
  description.add_usage(name, options:, args:)
  nls.times { add_br }
end