flex_args 0.0.1 → 0.1.1
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/lib/flex_args/version.rb +1 -2
- data/lib/flex_args.rb +84 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8116ea5ced17c3ede88f189de179d0c79cad45852e04df7ad315e58a36c9604
|
4
|
+
data.tar.gz: 939170153910604499bed507c48bfe3f6d44c0b7909bf716b79471da6e18eb51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be410b8d094de0e4070a7a0fe4b6a49117fb60fb6904f17a488878c0374a4e1556280d5ab1ed3d7c917732e0b85c2d077359a94b6842a29bed5ca49c7a1be3e1
|
7
|
+
data.tar.gz: 93dec0a974b675939584a223eb9e4a551208f942df45bf71bd577f17aa0f6496af27b6a7c381b5cf1cc94958508722cbbddb1570622c9d7fbec6bec1d3842643
|
data/lib/flex_args/version.rb
CHANGED
data/lib/flex_args.rb
CHANGED
@@ -31,7 +31,7 @@ require 'ostruct'
|
|
31
31
|
# ```spec # Flags and positionals
|
32
32
|
# args = parse(%w[hello --verbose -world 42])
|
33
33
|
# expect(args.positionals).to eq(%w[hello 42])
|
34
|
-
# expect(args.flags).to eq(Set.new([
|
34
|
+
# expect(args.flags).to eq(Set.new(%i[verbose w o r l d]))
|
35
35
|
# ```
|
36
36
|
# ### Value arguments
|
37
37
|
#
|
@@ -52,7 +52,7 @@ require 'ostruct'
|
|
52
52
|
# parsed = parse(input)
|
53
53
|
# expect(parsed.positionals).to eq(["a/b.rb"])
|
54
54
|
# expect(parsed.values).to eq(range: -2..3, offset: 3)
|
55
|
-
# expect(parsed.flags).to eq(Set.new([
|
55
|
+
# expect(parsed.flags).to eq(Set.new([:V]))
|
56
56
|
# ```
|
57
57
|
#
|
58
58
|
# #### Lists
|
@@ -67,6 +67,33 @@ require 'ostruct'
|
|
67
67
|
# parse(%w[escaped:1,a,,b,c]).values => {escaped: [1, "a,b", "c"]}
|
68
68
|
# ```
|
69
69
|
#
|
70
|
+
# #### Multiple Values
|
71
|
+
#
|
72
|
+
# This is an _alternative_ way to get a list of values
|
73
|
+
#
|
74
|
+
# ```spec # Multiple Values => Lists
|
75
|
+
# parse(%w[list:1 list:2 hello list:3]).values => {list: [1, 2, 3]}
|
76
|
+
# ```
|
77
|
+
#
|
78
|
+
# ### Advanced Features
|
79
|
+
#
|
80
|
+
# More advanced features are set with methods on the the instance
|
81
|
+
# before invoking `parse`.
|
82
|
+
#
|
83
|
+
# These features are
|
84
|
+
#
|
85
|
+
# - Aliased flags
|
86
|
+
# - Defaults
|
87
|
+
# - Constraints
|
88
|
+
# - allowed
|
89
|
+
# - required
|
90
|
+
# - domain checks
|
91
|
+
# - format checks
|
92
|
+
# - Custom Transformations
|
93
|
+
# - Semantic Checks (v0.2)
|
94
|
+
#
|
95
|
+
# and they are documented in the rdocs of the corresponding methods
|
96
|
+
#
|
70
97
|
class FlexArgs
|
71
98
|
|
72
99
|
ESCAPED_COMMA = "a4ey8NGnXIVhsV"
|
@@ -76,24 +103,58 @@ class FlexArgs
|
|
76
103
|
SINGLE_FLAG = %r{\A -- (.*) \z}x
|
77
104
|
VALUE = %r{\A ([[:alpha:]][[:alnum:]_]+) : (.*) \z}x
|
78
105
|
|
79
|
-
attr_reader :
|
106
|
+
attr_reader :alias_definitions, :flags, :positionals, :result, :values
|
80
107
|
|
81
|
-
def parse
|
108
|
+
def parse(args)
|
109
|
+
init_values
|
82
110
|
args.each { parse_arg it }
|
83
111
|
result
|
84
112
|
end
|
85
113
|
|
114
|
+
##
|
115
|
+
#
|
116
|
+
# ## Aliases are defined by passing a hash
|
117
|
+
#
|
118
|
+
# The key of the hash is the alias and it will be defined as an alias
|
119
|
+
# to the value of the hash for **each** grapheme of the key, here is
|
120
|
+
# how that works
|
121
|
+
#
|
122
|
+
# ```spec # Aliases
|
123
|
+
# parser = FlexArgs
|
124
|
+
# .new
|
125
|
+
# .aliases(hu: :help, v: :version)
|
126
|
+
#
|
127
|
+
# expect(parser.parse(%w[-uv]).flags).to eq(Set.new(%i[help version]))
|
128
|
+
# expect(parser.parse(%w[-h]).flags).to eq(Set.new(%i[help]))
|
129
|
+
# ```
|
130
|
+
#
|
131
|
+
def aliases(alias_definitions)
|
132
|
+
alias_definitions
|
133
|
+
.each do |key, value|
|
134
|
+
key.to_s.grapheme_clusters.each { define_alias it, value }
|
135
|
+
end
|
136
|
+
self
|
137
|
+
end
|
138
|
+
|
86
139
|
private
|
87
|
-
def initialize
|
88
|
-
@
|
89
|
-
|
90
|
-
|
91
|
-
|
140
|
+
def initialize
|
141
|
+
@alias_definitions = Hash.new
|
142
|
+
init_values
|
143
|
+
end
|
144
|
+
|
145
|
+
def add_flags(shorts)
|
146
|
+
shorts.each do
|
147
|
+
flag = alias_definitions.fetch(it, it)
|
148
|
+
flags << flag.to_sym
|
149
|
+
end
|
92
150
|
end
|
93
151
|
|
94
152
|
def add_value(matches)
|
95
153
|
matches => [key, value]
|
96
|
-
values
|
154
|
+
values
|
155
|
+
.update(key.to_sym => cast_all(value)) do |k, o, n|
|
156
|
+
Array(o) << n
|
157
|
+
end
|
97
158
|
end
|
98
159
|
|
99
160
|
def cast(value)
|
@@ -116,13 +177,24 @@ class FlexArgs
|
|
116
177
|
values.one? ? values.first : values
|
117
178
|
end
|
118
179
|
|
180
|
+
def define_alias(short, long)
|
181
|
+
alias_definitions.update(short => long)
|
182
|
+
end
|
183
|
+
|
184
|
+
def init_values
|
185
|
+
@positionals = []
|
186
|
+
@flags = Set.new
|
187
|
+
@values = Hash.new
|
188
|
+
end
|
189
|
+
|
190
|
+
|
119
191
|
def parse_arg(arg)
|
120
192
|
# require "debug"; binding.break
|
121
193
|
case arg
|
122
194
|
when SINGLE_FLAG
|
123
|
-
flags << Regexp.last_match[1]
|
195
|
+
flags << Regexp.last_match[1].to_sym
|
124
196
|
when MANY_FLAGS
|
125
|
-
|
197
|
+
add_flags(Regexp.last_match[1].grapheme_clusters)
|
126
198
|
when VALUE
|
127
199
|
add_value(Regexp.last_match)
|
128
200
|
else
|