flex_args 0.0.1 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e93a15e8addb2cefcdcda7b58faf9cceb148e5775a57f0e21cceb3dd90734260
4
- data.tar.gz: 68a45e49acfd7d0f99033140fba26352db551c7e4f9b161c64e9fe231de0b695
3
+ metadata.gz: ef1ef86d73320f89eba86a8e982f3b7d24724ccdfe0d085ac2a9ac674485b79e
4
+ data.tar.gz: 0f554da437841db0d31aee39352d15db3e6480b95bcbcf8755ac5dcf77d84cbb
5
5
  SHA512:
6
- metadata.gz: a46f39dd0565e37fb17c52efc6b7e15acd1e10f197574aa779111f48da19142984faf53867b22354193670514a366cd278135021769ac7985dad325ef910f8ca
7
- data.tar.gz: 59e4cf51aa46e4119fe7de1cb01639e5cdd12cf911f16a9f473a2a465798543e6ea80178aeaea9cb61fb925bdbc639a2da4732c9bec794e90de26039ce1d3487
6
+ metadata.gz: f745f4e480196273b41b4db03742f3ba1a8d1a1d4b645d81b2aa976a30b7709c7971871ab6edea421d0575878c24de931055c60a39b20f54adbb1dc8bba201b9
7
+ data.tar.gz: 74e53271bf7ca6699ea7dbf47b2bf31602e3ae2a8b8fbb1595d58d0e94ddb1970f1ff1087b83946f0169af025c0d4e346cb81f66a8b5f4c878c9f226587c8b2c
@@ -2,8 +2,7 @@
2
2
 
3
3
  class FlexArgs
4
4
  module Version
5
- VERSION="0.0.1"
6
-
5
+ VERSION="0.1.0"
7
6
  end
8
7
  end
9
8
  # SPDX-License-Identifier: AGPL-3.0-or-later
data/lib/flex_args.rb CHANGED
@@ -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 :args, :flags, :positionals, :result, :values
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(args)
88
- @args = args
89
- @positionals = []
90
- @flags = Set.new
91
- @values = Hash.new
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
149
+ end
92
150
  end
93
151
 
94
152
  def add_value(matches)
95
153
  matches => [key, value]
96
- values.update(key.to_sym => cast_all(value))
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
195
  flags << Regexp.last_match[1]
124
196
  when MANY_FLAGS
125
- @flags += Regexp.last_match[1].grapheme_clusters
197
+ add_flags(Regexp.last_match[1].grapheme_clusters)
126
198
  when VALUE
127
199
  add_value(Regexp.last_match)
128
200
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flex_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober