help_parser 8.2.230210 → 10.0.261206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.version +1 -0
- data/CREDITS.md +15 -0
- data/README.md +78 -59
- data/lib/help_parser/aliases.rb +16 -4
- data/lib/help_parser/completion.rb +209 -143
- data/lib/help_parser/constants.rb +20 -19
- data/lib/help_parser/exceptions.rb +24 -31
- data/lib/help_parser/macros.rb +16 -132
- data/lib/help_parser/options.rb +77 -47
- data/lib/help_parser/parse_argv.rb +56 -0
- data/lib/help_parser/parse_help.rb +68 -0
- data/lib/help_parser/type_map.rb +53 -0
- data/lib/help_parser/validate.rb +197 -66
- data/lib/help_parser.rb +10 -9
- metadata +18 -76
- data/lib/help_parser/k2t2r.rb +0 -41
- data/lib/help_parser/parsea.rb +0 -29
- data/lib/help_parser/parseh.rb +0 -50
- data/lib/help_parser/parseu.rb +0 -24
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Shared constants, patterns, messages, and utilities for HelpParser.
|
|
4
|
+
# :reek:TooManyConstants
|
|
1
5
|
module HelpParser
|
|
2
|
-
VSN = %w[v version]
|
|
3
|
-
HLP = %w[h help]
|
|
4
|
-
VRBS
|
|
6
|
+
VSN = %w[v version].freeze
|
|
7
|
+
HLP = %w[h help].freeze
|
|
8
|
+
VRBS = 'verbose'
|
|
9
|
+
DBG = 'debug'
|
|
5
10
|
|
|
6
11
|
# reserved name
|
|
7
12
|
USAGE = 'usage'
|
|
@@ -9,8 +14,8 @@ module HelpParser
|
|
|
9
14
|
EXCLUSIVE = 'exclusive'
|
|
10
15
|
INCLUSIVE = 'inclusive'
|
|
11
16
|
CONDITIONAL = 'conditional'
|
|
12
|
-
FLAG_CLUMPS = [EXCLUSIVE,INCLUSIVE,CONDITIONAL]
|
|
13
|
-
RESERVED = [USAGE,TYPES,EXCLUSIVE,INCLUSIVE,CONDITIONAL]
|
|
17
|
+
FLAG_CLUMPS = [EXCLUSIVE, INCLUSIVE, CONDITIONAL].freeze
|
|
18
|
+
RESERVED = [USAGE, TYPES, EXCLUSIVE, INCLUSIVE, CONDITIONAL].freeze
|
|
14
19
|
|
|
15
20
|
# sections
|
|
16
21
|
SECTION_NAME = /^(?<name>[A-Z]\w+):$/
|
|
@@ -18,20 +23,20 @@ module HelpParser
|
|
|
18
23
|
# usage
|
|
19
24
|
FLAG = /^--?(?<k>\w+)$/
|
|
20
25
|
LITERAL = /^(?<k>\w[\w.-]*:?)$/
|
|
21
|
-
VARIABLE = /^<(?<k>\w+)(
|
|
26
|
+
VARIABLE = /^<(?<k>\w+)(?:=(?<t>[A-Z]+))?>(?<p>[+])?$/
|
|
22
27
|
FLAG_GROUP = /^:(?<k>\w+)(?<p>[+])?$/
|
|
23
28
|
|
|
24
29
|
# spec --?w+
|
|
25
30
|
SHORT = /^-(?<s>\w)$/
|
|
26
|
-
LONG = /^--(?<k>\w+)(
|
|
31
|
+
LONG = /^--(?<k>\w+)(?:=(?<t>[A-Z]+))?(?:,?\s+(?<d>[^-\s]\S*))?$/
|
|
27
32
|
|
|
28
33
|
# spec -w,? --w+
|
|
29
34
|
SHORT_LONG = /^-(?<s>\w),?\s+--(?<k>\w+)$/
|
|
30
35
|
SHORT_LONG_DEFAULT =
|
|
31
|
-
/^-(?<s>\w),?\s+--(?<k>\w+)(
|
|
36
|
+
/^-(?<s>\w),?\s+--(?<k>\w+)(?:=(?<t>[A-Z]+))?,?\s+(?<d>\S*)$/
|
|
32
37
|
|
|
33
38
|
# spec W+ /~/
|
|
34
|
-
TYPE_DEF =
|
|
39
|
+
TYPE_DEF = %r{^(?<t>[A-Z]+),?\s+/(?<r>\S+)/$}
|
|
35
40
|
|
|
36
41
|
# spec w+( w+)+
|
|
37
42
|
X_DEF = /^\w+( +\w+)+$/ # for both exclusive and inclusive specs
|
|
@@ -67,12 +72,6 @@ module HelpParser
|
|
|
67
72
|
MISSING_USAGE = 'Missing usage'
|
|
68
73
|
UNCOMPLETED_TYPES = 'Uncompleted types definition'
|
|
69
74
|
BAD_DEFAULT = 'Default does not match type'
|
|
70
|
-
NOT_STRING = 'Not a String'
|
|
71
|
-
NOT_STRINGS = 'Not all Strings'
|
|
72
|
-
NOT_FLOAT = 'Not a Float'
|
|
73
|
-
NOT_FLOATS = 'Not all Floats'
|
|
74
|
-
NOT_INTEGER = 'Not an Integer'
|
|
75
|
-
NOT_INTEGERS = 'Not all Integers'
|
|
76
75
|
NOT_EXIST = 'Does not exist'
|
|
77
76
|
# error messages, full:
|
|
78
77
|
NO_MATCH = 'Software Error: NoMatch was not caught by HelpParser.'
|
|
@@ -80,9 +79,11 @@ module HelpParser
|
|
|
80
79
|
EXTRANEOUS_SPACES = 'Extraneous spaces in help.'
|
|
81
80
|
|
|
82
81
|
# lambda utilities
|
|
83
|
-
MSG = ->(msg
|
|
84
|
-
F2K =
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
MSG = ->(msg, *keys) { "#{msg}: #{keys.join(' ')}" }
|
|
83
|
+
F2K = lambda do |flag|
|
|
84
|
+
flag.start_with?('--') ? flag[2..].split('=', 2).first : flag[1]
|
|
85
|
+
end
|
|
86
|
+
REDTTY = lambda do |msg, out = $stderr|
|
|
87
|
+
out.tty? ? out.puts("\e[0;31;49m#{msg}\e[0m") : out.puts(msg)
|
|
87
88
|
end
|
|
88
89
|
end
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
class HelpParserException < Exception
|
|
3
|
-
def _init
|
|
4
|
-
@code = 1
|
|
5
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
6
2
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
end
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Base exception carrying an exit status and formatted output behavior.
|
|
5
|
+
class HelpParserException < StandardError
|
|
6
|
+
EXIT_CODE = 1
|
|
12
7
|
|
|
13
8
|
def exit
|
|
14
9
|
if @code.positive?
|
|
@@ -18,48 +13,46 @@ module HelpParser
|
|
|
18
13
|
end
|
|
19
14
|
Kernel.exit @code
|
|
20
15
|
end
|
|
16
|
+
|
|
17
|
+
# Must give message.
|
|
18
|
+
def initialize(message)
|
|
19
|
+
@code = self.class::EXIT_CODE
|
|
20
|
+
super
|
|
21
|
+
end
|
|
21
22
|
end
|
|
22
23
|
|
|
24
|
+
# Raised when version output is requested.
|
|
23
25
|
class VersionException < HelpParserException
|
|
24
|
-
|
|
25
|
-
@code = 0
|
|
26
|
-
end
|
|
26
|
+
EXIT_CODE = 0
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# Raised when help output is requested.
|
|
29
30
|
class HelpException < HelpParserException
|
|
30
|
-
|
|
31
|
-
@code = 0
|
|
32
|
-
end
|
|
31
|
+
EXIT_CODE = 0
|
|
33
32
|
end
|
|
34
33
|
|
|
34
|
+
# Raised on invalid command-line usage.
|
|
35
35
|
class UsageError < HelpParserException
|
|
36
|
-
|
|
37
|
-
@code = EX_USAGE
|
|
38
|
-
end
|
|
36
|
+
EXIT_CODE = EX_USAGE
|
|
39
37
|
end
|
|
40
38
|
|
|
39
|
+
# Raised on internal errors that should not happen.
|
|
41
40
|
class SoftwareError < HelpParserException
|
|
42
|
-
|
|
43
|
-
def _init
|
|
44
|
-
@code = EX_SOFTWARE
|
|
45
|
-
end
|
|
41
|
+
EXIT_CODE = EX_SOFTWARE
|
|
46
42
|
end
|
|
47
43
|
|
|
44
|
+
# Raised internally to short-circuit failed usage matching.
|
|
48
45
|
class NoMatch < HelpParserException
|
|
49
|
-
|
|
50
|
-
def _init
|
|
51
|
-
@code = EX_SOFTWARE
|
|
52
|
-
end
|
|
46
|
+
EXIT_CODE = EX_SOFTWARE
|
|
53
47
|
|
|
54
|
-
# Forces
|
|
48
|
+
# Forces its own message.
|
|
55
49
|
def initialize
|
|
56
50
|
super(NO_MATCH)
|
|
57
51
|
end
|
|
58
52
|
end
|
|
59
53
|
|
|
54
|
+
# Raised on invalid help specification.
|
|
60
55
|
class HelpError < HelpParserException
|
|
61
|
-
|
|
62
|
-
@code = EX_CONFIG
|
|
63
|
-
end
|
|
56
|
+
EXIT_CODE = EX_CONFIG
|
|
64
57
|
end
|
|
65
58
|
end
|
data/lib/help_parser/macros.rb
CHANGED
|
@@ -1,145 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
def self.string(*names)
|
|
3
|
-
names.each do |name|
|
|
4
|
-
Options.instance_eval do
|
|
5
|
-
define_method(name) do
|
|
6
|
-
s = @hash[name.to_s]
|
|
7
|
-
raise UsageError, MSG[NOT_STRING,name] unless s.is_a?(String)
|
|
8
|
-
s
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def self.string?(*names)
|
|
15
|
-
names.each do |name|
|
|
16
|
-
Options.instance_eval do
|
|
17
|
-
define_method("#{name}?") do
|
|
18
|
-
s = @hash[name.to_s]
|
|
19
|
-
raise UsageError,MSG[NOT_STRING,name] unless s.nil? || s.is_a?(String)
|
|
20
|
-
s
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def self.strings(*names)
|
|
27
|
-
names.each do |name|
|
|
28
|
-
Options.instance_eval do
|
|
29
|
-
define_method(name) do
|
|
30
|
-
a = @hash[name.to_s]
|
|
31
|
-
raise UsageError, MSG[NOT_STRINGS,name] unless a.is_a?(Array)
|
|
32
|
-
a
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def self.strings?(*names)
|
|
39
|
-
names.each do |name|
|
|
40
|
-
Options.instance_eval do
|
|
41
|
-
define_method("#{name}?") do
|
|
42
|
-
a = @hash[name.to_s]
|
|
43
|
-
raise UsageError,MSG[NOT_STRINGS,name] unless a.nil? || a.is_a?(Array)
|
|
44
|
-
a
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def self.float(*names)
|
|
51
|
-
names.each do |name|
|
|
52
|
-
Options.instance_eval do
|
|
53
|
-
define_method(name) do
|
|
54
|
-
@hash[name.to_s]&.to_f or raise
|
|
55
|
-
rescue
|
|
56
|
-
raise UsageError, MSG[NOT_FLOAT,name]
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
61
2
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
define_method("#{name}?") do
|
|
66
|
-
@hash[name.to_s]&.to_f
|
|
67
|
-
rescue
|
|
68
|
-
raise UsageError, MSG[NOT_FLOAT,name]
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def self.floats(*names)
|
|
75
|
-
names.each do |name|
|
|
76
|
-
Options.instance_eval do
|
|
77
|
-
define_method(name) do
|
|
78
|
-
@hash[name.to_s].map(&:to_f)
|
|
79
|
-
rescue
|
|
80
|
-
raise UsageError, MSG[NOT_FLOATS,name]
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
3
|
+
# Option value conversion macros for HelpParser::Options.
|
|
4
|
+
module HelpParser
|
|
5
|
+
def self.integer(*names) = map(*names, map: :to_i)
|
|
85
6
|
|
|
86
|
-
def self.
|
|
87
|
-
names
|
|
88
|
-
Options.instance_eval do
|
|
89
|
-
define_method("#{name}?") do
|
|
90
|
-
@hash[name.to_s]&.map(&:to_f)
|
|
91
|
-
rescue
|
|
92
|
-
raise UsageError, MSG[NOT_FLOATS,name]
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
7
|
+
def self.map(*names, map:)
|
|
8
|
+
define_converter(names, &map)
|
|
96
9
|
end
|
|
97
10
|
|
|
98
|
-
def self.
|
|
99
|
-
|
|
100
|
-
Options.instance_eval do
|
|
101
|
-
define_method(name) do
|
|
102
|
-
@hash[name.to_s]&.to_i or raise
|
|
103
|
-
rescue
|
|
104
|
-
raise UsageError, MSG[NOT_INTEGER,name]
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
11
|
+
def self.float(*names) = map(*names, map: :to_f)
|
|
12
|
+
def self.rational(*names) = map(*names, map: :to_r)
|
|
109
13
|
|
|
110
|
-
def self.
|
|
14
|
+
def self.define_converter(names, &convert)
|
|
111
15
|
names.each do |name|
|
|
112
|
-
Options.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
rescue
|
|
116
|
-
raise UsageError, MSG[NOT_INTEGER,name]
|
|
117
|
-
end
|
|
16
|
+
Options.define_method(name) do
|
|
17
|
+
value = @hash[name.to_s]
|
|
18
|
+
value.is_a?(Array) ? value.map(&convert) : value && convert.call(value)
|
|
118
19
|
end
|
|
119
20
|
end
|
|
120
21
|
end
|
|
22
|
+
private_class_method :define_converter
|
|
121
23
|
|
|
122
|
-
def self.
|
|
123
|
-
names.
|
|
124
|
-
Options.instance_eval do
|
|
125
|
-
define_method(name) do
|
|
126
|
-
@hash[name.to_s].map(&:to_i)
|
|
127
|
-
rescue
|
|
128
|
-
raise UsageError, MSG[NOT_INTEGERS,name]
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
end
|
|
24
|
+
def self.split(*names, sep:, map:)
|
|
25
|
+
define_converter(names) { it.split(sep).map(&map) }
|
|
132
26
|
end
|
|
133
27
|
|
|
134
|
-
def self.
|
|
135
|
-
names.each do |name|
|
|
136
|
-
Options.instance_eval do
|
|
137
|
-
define_method("#{name}?") do
|
|
138
|
-
@hash[name.to_s]&.map(&:to_i)
|
|
139
|
-
rescue
|
|
140
|
-
raise UsageError, MSG[NOT_INTEGERS,name]
|
|
141
|
-
end
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
end
|
|
28
|
+
def self.csv(*names) = split(*names, sep: ',', map: :strip)
|
|
145
29
|
end
|
data/lib/help_parser/options.rb
CHANGED
|
@@ -1,63 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module HelpParser
|
|
4
|
+
# Parsed command-line options with hash-like and method-style access.
|
|
2
5
|
class Options
|
|
3
|
-
def
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
def [](key) = @hash[key]
|
|
7
|
+
|
|
8
|
+
def []=(key, value)
|
|
9
|
+
@hash[key] = value
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# :reek:BooleanParameter
|
|
13
|
+
def respond_to_missing?(name, include_all = false)
|
|
14
|
+
name.end_with?('!') ? super : true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def method_missing(name, *args)
|
|
18
|
+
super unless respond_to_missing?(name)
|
|
19
|
+
if args.any? || block_given?
|
|
20
|
+
raise ArgumentError, 'expected neither args nor block'
|
|
8
21
|
end
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
end
|
|
37
|
-
end
|
|
22
|
+
|
|
23
|
+
key = name.to_s
|
|
24
|
+
key.end_with?('?') ? @hash.key?(key.chop) : @hash[key]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h = @hash
|
|
28
|
+
|
|
29
|
+
def initialize(version, help, argv)
|
|
30
|
+
@hash = ParseArgv.new(argv).to_h
|
|
31
|
+
check_version(version) if version
|
|
32
|
+
apply_help(String(help)) if help
|
|
33
|
+
$VERBOSE = true if @hash[VRBS] == true
|
|
34
|
+
$DEBUG = true if @hash[DBG] == true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def check_version(version)
|
|
40
|
+
return unless VSN.any? { @hash.key? it }
|
|
41
|
+
|
|
42
|
+
raise VersionException, String(version)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def check_help(help)
|
|
46
|
+
case HLP.count { @hash.key? it }
|
|
47
|
+
when 0 then return
|
|
48
|
+
when HLP.length then Validate.new(help)
|
|
38
49
|
end
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
|
|
51
|
+
raise HelpException, help
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def apply_help(help)
|
|
55
|
+
check_help(help)
|
|
56
|
+
specs = ParseHelp.new(help).specs
|
|
57
|
+
Completion.new(@hash, specs)
|
|
58
|
+
check_clumps(specs)
|
|
41
59
|
end
|
|
42
60
|
|
|
43
|
-
def
|
|
44
|
-
|
|
61
|
+
def check_exclusive(groups)
|
|
62
|
+
groups&.each do |keys|
|
|
63
|
+
next if keys.count { @hash.key? it } <= 1
|
|
64
|
+
|
|
65
|
+
raise UsageError, MSG[EXCLUSIVE_KEYS, *keys]
|
|
66
|
+
end
|
|
45
67
|
end
|
|
46
68
|
|
|
47
|
-
def
|
|
48
|
-
|
|
69
|
+
def check_clumps(specs)
|
|
70
|
+
check_exclusive(specs[EXCLUSIVE])
|
|
71
|
+
check_inclusive(specs[INCLUSIVE])
|
|
72
|
+
check_conditional(specs[CONDITIONAL])
|
|
49
73
|
end
|
|
50
74
|
|
|
51
|
-
def
|
|
52
|
-
|
|
75
|
+
def check_inclusive(groups)
|
|
76
|
+
groups&.each do |keys|
|
|
77
|
+
count = keys.count { @hash.key? it }
|
|
78
|
+
next if count.zero? || count == keys.length
|
|
79
|
+
|
|
80
|
+
raise UsageError, MSG[INCLUSIVE_KEYS, *keys]
|
|
81
|
+
end
|
|
53
82
|
end
|
|
54
83
|
|
|
55
|
-
def
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
84
|
+
def check_conditional(groups)
|
|
85
|
+
groups&.each do |keys|
|
|
86
|
+
next unless @hash.key?(keys.first)
|
|
87
|
+
next if keys.all? { @hash.key? it }
|
|
88
|
+
|
|
89
|
+
raise UsageError, MSG[CONDITIONAL_KEYS, *keys]
|
|
59
90
|
end
|
|
60
|
-
m[-1]=='?' ? @hash.key?(m[0..-2].to_s) : @hash[m.to_s]
|
|
61
91
|
end
|
|
62
92
|
end
|
|
63
93
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Parses command-line arguments into an ArgvHash.
|
|
5
|
+
class ParseArgv
|
|
6
|
+
STOP_FLAGS = %w[- --].freeze
|
|
7
|
+
|
|
8
|
+
def to_h = @hash.to_h
|
|
9
|
+
|
|
10
|
+
def initialize(argv)
|
|
11
|
+
@hash = ArgvHash.new
|
|
12
|
+
@index = 0
|
|
13
|
+
parse(argv)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def parse(argv)
|
|
19
|
+
argv.each do |arg|
|
|
20
|
+
if arg.start_with?('-')
|
|
21
|
+
break if STOP_FLAGS.include?(arg)
|
|
22
|
+
|
|
23
|
+
parse_flag(arg[1..])
|
|
24
|
+
else
|
|
25
|
+
add_positional(arg)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_positional(arg)
|
|
31
|
+
@hash[@index] = arg
|
|
32
|
+
@index += 1
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def parse_flag(flag)
|
|
36
|
+
if flag.start_with?('-')
|
|
37
|
+
parse_long(flag[1..])
|
|
38
|
+
else
|
|
39
|
+
parse_short(flag)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def parse_short(chars)
|
|
44
|
+
chars.each_char { |char| @hash[char] = true }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def parse_long(spec)
|
|
48
|
+
if spec.include?('=')
|
|
49
|
+
key, value = spec.split('=', 2)
|
|
50
|
+
@hash[key] = value
|
|
51
|
+
else
|
|
52
|
+
@hash[spec] = true
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Parses a help text string into a section-keyed NoDupHash of specs.
|
|
5
|
+
class ParseHelp
|
|
6
|
+
USAGE_SCAN = /\[|\]|[^ \[\]]+/
|
|
7
|
+
|
|
8
|
+
attr_reader :specs
|
|
9
|
+
|
|
10
|
+
def initialize(help)
|
|
11
|
+
@specs = NoDupHash.new
|
|
12
|
+
@section = ''
|
|
13
|
+
parse(help)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
attr_reader :section
|
|
19
|
+
|
|
20
|
+
def parse(help)
|
|
21
|
+
help.each_line do |line|
|
|
22
|
+
stripped = line.chomp
|
|
23
|
+
next if stripped.empty?
|
|
24
|
+
break if process_line(stripped) == :stop
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def process_line(line)
|
|
29
|
+
if (match = SECTION_NAME.match(line))
|
|
30
|
+
@section = match[:name].downcase
|
|
31
|
+
@specs[@section] = []
|
|
32
|
+
elsif !@section.empty?
|
|
33
|
+
return :stop if line.start_with?('#')
|
|
34
|
+
|
|
35
|
+
parse_indented(line) if line.start_with?(' ')
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def check_empty_spec(_comment) = nil
|
|
40
|
+
|
|
41
|
+
def parse_indented(line)
|
|
42
|
+
spec, comment = line.split("\t", 2).map(&:strip)
|
|
43
|
+
if spec.empty?
|
|
44
|
+
check_empty_spec(comment)
|
|
45
|
+
else
|
|
46
|
+
add_spec(spec)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def add_spec(spec)
|
|
51
|
+
entry =
|
|
52
|
+
@section == USAGE ? parse_usage(spec.scan(USAGE_SCAN)) : spec.split(CSV)
|
|
53
|
+
@specs[@section].push(entry)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def parse_usage(tokens)
|
|
57
|
+
result = []
|
|
58
|
+
while (token = tokens.shift)
|
|
59
|
+
result.push(case token
|
|
60
|
+
when ']' then return result
|
|
61
|
+
when '[' then parse_usage(tokens)
|
|
62
|
+
else token
|
|
63
|
+
end)
|
|
64
|
+
end
|
|
65
|
+
result
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Maps option/variable keys to type names and type names to compiled regexes.
|
|
5
|
+
class TypeMap
|
|
6
|
+
attr_reader :key_types, :regexes
|
|
7
|
+
|
|
8
|
+
def self.record_key_type(map, token)
|
|
9
|
+
match = VARIABLE.match(token) || LONG.match(token)
|
|
10
|
+
raise SoftwareError, MSG[UNEXPECTED, token] unless match
|
|
11
|
+
|
|
12
|
+
name, type = match.values_at(:k, :t)
|
|
13
|
+
return map[name] = type unless (existing = map[name])
|
|
14
|
+
|
|
15
|
+
raise HelpError, MSG[INCONSISTENT, name, type, existing] unless
|
|
16
|
+
type == existing
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.compile_regex(type, pattern)
|
|
20
|
+
Regexp.new(pattern[1..-2])
|
|
21
|
+
rescue StandardError
|
|
22
|
+
raise HelpError, MSG[BAD_REGEX, type, pattern]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(specs)
|
|
26
|
+
@specs = specs
|
|
27
|
+
@regexes = build_regexes
|
|
28
|
+
@key_types = build_key_types
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def build_key_types
|
|
34
|
+
typed_tokens.each_with_object(NoDupHash.new) do |token, map|
|
|
35
|
+
self.class.record_key_type(map, token)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def build_regexes
|
|
40
|
+
types = @specs[TYPES]
|
|
41
|
+
return unless types
|
|
42
|
+
|
|
43
|
+
types.each_with_object(NoDupHash.new) do |(type, pattern), map|
|
|
44
|
+
map[type] = self.class.compile_regex(type, pattern)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def typed_tokens
|
|
49
|
+
@specs.select { |key, _| key == USAGE || !RESERVED.include?(key) }
|
|
50
|
+
.values.flatten.select { it.include?('=') }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|