Class: L43::OptParser
- Inherits:
-
Object
- Object
- L43::OptParser
- 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.2.1'
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#constrained ⇒ Object
readonly
Returns the value of attribute constrained.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#kwds_name ⇒ Object
readonly
Returns the value of attribute kwds_name.
Instance Method Summary collapse
-
#add_br ⇒ Object
Documentation ————-.
- #add_desc(*chunks) ⇒ Object
-
#descriptions ⇒ Object
Services ——–.
-
#flag(name, short = nil, **kwds) ⇒ Object
Constraints ———–.
- #help(device: $stderr) ⇒ Object
- #keyword(name, short = nil, as: nil, default: Core::None, **ks, &init) ⇒ Object
-
#parse(arguments) ⇒ Object
Business Logic ————–.
- #post_check(name = nil, &blk) ⇒ Object
- #post_hook(&blk) ⇒ Object
- #section(line, indent: 1, color: [:bold, :green]) ⇒ Object
- #usage(name, options: nil, args: nil, nls: 0) ⇒ Object
Methods included from Meta
Methods included from Memos
#aliases, #args, #checks, #defaults, #errors, #flags, #hooks, #keywords, #keywords_values
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
29 30 31 |
# File 'lib/l43/opt_parser.rb', line 29 def arguments @arguments end |
#constrained ⇒ Object (readonly)
Returns the value of attribute constrained.
29 30 31 |
# File 'lib/l43/opt_parser.rb', line 29 def constrained @constrained end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
29 30 31 |
# File 'lib/l43/opt_parser.rb', line 29 def description @description end |
#kwds_name ⇒ Object (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_br ⇒ Object
Documentation
71 72 73 74 |
# File 'lib/l43/opt_parser.rb', line 71 def add_br description.add_line self end |
#add_desc(*chunks) ⇒ Object
76 77 78 79 |
# File 'lib/l43/opt_parser.rb', line 76 def add_desc(*chunks) description.add_desc(*chunks) self end |
#descriptions ⇒ Object
Services
89 |
# File 'lib/l43/opt_parser.rb', line 89 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
90 |
# File 'lib/l43/opt_parser.rb', line 90 def help(device: $stderr) = cs_put(descriptions, device:) |
#keyword(name, short = nil, as: nil, default: Core::None, **ks, &init) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/l43/opt_parser.rb', line 49 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 DuplicateKeywordDefinition, "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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/l43/opt_parser.rb', line 95 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
117 118 119 120 |
# File 'lib/l43/opt_parser.rb', line 117 def post_check(name = nil, &blk) checks << [name, blk] self end |
#post_hook(&blk) ⇒ Object
112 113 114 115 |
# File 'lib/l43/opt_parser.rb', line 112 def post_hook(&blk) hooks << blk self end |
#section(line, indent: 1, color: [:bold, :green]) ⇒ Object
81 82 83 84 |
# File 'lib/l43/opt_parser.rb', line 81 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
122 123 124 125 |
# File 'lib/l43/opt_parser.rb', line 122 def usage(name, options: nil, args: nil, nls: 0) description.add_usage(name, options:, args:) nls.times { add_br } end |