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/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

BadFormat =
Class.new(RuntimeError)
DuplicateFlagDefinition =
Class.new(RuntimeError)
DuplicateKwdDefinition =
Class.new(RuntimeError)
FailedCheck =
Class.new(RuntimeError)
MissingValue =
Class.new(RuntimeError)
UndefinedFlag =
Class.new(RuntimeError)
UndefinedKwd =
Class.new(RuntimeError)
FlagRgx =
/\A:(.*)/
KeywordRgx =
/(.*):\z/
ShortFlagRgx =
/\A\-(.*)/
VERSION =
'0.2.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.



36
37
38
# File 'lib/l43/opt_parser.rb', line 36

def arguments
  @arguments
end

#constrainedObject (readonly)

Returns the value of attribute constrained.



36
37
38
# File 'lib/l43/opt_parser.rb', line 36

def constrained
  @constrained
end

#descriptionObject (readonly)

Returns the value of attribute description.



36
37
38
# File 'lib/l43/opt_parser.rb', line 36

def description
  @description
end

#kwds_nameObject (readonly)

Returns the value of attribute kwds_name.



36
37
38
# File 'lib/l43/opt_parser.rb', line 36

def kwds_name
  @kwds_name
end

Instance Method Details

#add_brObject

Documentation




78
79
80
81
# File 'lib/l43/opt_parser.rb', line 78

def add_br
  description.add_line
  self
end

#add_desc(*chunks) ⇒ Object



83
84
85
86
# File 'lib/l43/opt_parser.rb', line 83

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

#descriptionsObject

Services




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

def descriptions = description.lines

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

Constraints




40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/l43/opt_parser.rb', line 40

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



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

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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/l43/opt_parser.rb', line 56

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

#parse(arguments) ⇒ Object

Business Logic




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

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:).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



124
125
126
127
# File 'lib/l43/opt_parser.rb', line 124

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

#post_hook(&blk) ⇒ Object



119
120
121
122
# File 'lib/l43/opt_parser.rb', line 119

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

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



88
89
90
91
# File 'lib/l43/opt_parser.rb', line 88

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



129
130
131
132
# File 'lib/l43/opt_parser.rb', line 129

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