cl 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1 -0
  3. data/Gemfile.lock +1 -1
  4. data/NOTES.md +4 -0
  5. data/README.md +131 -74
  6. data/examples/README.md +22 -0
  7. data/examples/{src → _src}/args/cast.erb.rb +0 -0
  8. data/examples/{src → _src}/args/opts.erb.rb +0 -0
  9. data/examples/{src → _src}/args/required.erb.rb +0 -0
  10. data/examples/{src → _src}/args/splat.erb.rb +0 -0
  11. data/examples/{src → _src}/gem.erb.rb +0 -0
  12. data/examples/{src → _src}/heroku.erb.rb +0 -0
  13. data/examples/{src → _src}/rakeish.erb.rb +0 -0
  14. data/examples/{src → _src}/readme/abstract.erb.rb +0 -0
  15. data/examples/{src → _src}/readme/alias.erb.rb +0 -0
  16. data/examples/{src → _src}/readme/arg.erb.rb +0 -0
  17. data/examples/{src → _src}/readme/arg_array.erb.rb +0 -0
  18. data/examples/{src → _src}/readme/arg_type.erb.rb +0 -0
  19. data/examples/{src → _src}/readme/args_splat.erb.rb +0 -0
  20. data/examples/{src → _src}/readme/array.erb.rb +0 -0
  21. data/examples/_src/readme/basic.erb.rb +70 -0
  22. data/examples/{src → _src}/readme/default.erb.rb +0 -0
  23. data/examples/{src → _src}/readme/deprecated.erb.rb +0 -0
  24. data/examples/{src → _src}/readme/deprecated_alias.erb.rb +0 -0
  25. data/examples/_src/readme/description.erb.rb +58 -0
  26. data/examples/{src → _src}/readme/downcase.erb.rb +0 -0
  27. data/examples/{src → _src}/readme/enum.erb.rb +0 -0
  28. data/examples/{src → _src}/readme/example.erb.rb +0 -0
  29. data/examples/{src → _src}/readme/format.erb.rb +0 -0
  30. data/examples/{src → _src}/readme/internal.erb.rb +0 -0
  31. data/examples/{src → _src}/readme/negate.erb.rb +0 -0
  32. data/examples/{src/readme/node.erb.rb → _src/readme/note.erb.rb} +0 -0
  33. data/examples/{src → _src}/readme/opts.erb.rb +0 -0
  34. data/examples/{src → _src}/readme/opts_block.erb.rb +0 -0
  35. data/examples/{src → _src}/readme/range.erb.rb +0 -0
  36. data/examples/_src/readme/registry.erb.rb +16 -0
  37. data/examples/{src → _src}/readme/required.erb.rb +0 -0
  38. data/examples/{src → _src}/readme/requireds.erb.rb +0 -0
  39. data/examples/{src → _src}/readme/requires.erb.rb +0 -0
  40. data/examples/_src/readme/runner.erb.rb +27 -0
  41. data/examples/_src/readme/runner_custom.erb.rb +25 -0
  42. data/examples/{src → _src}/readme/secret.erb.rb +0 -0
  43. data/examples/{src → _src}/readme/see.erb.rb +0 -0
  44. data/examples/{src → _src}/readme/type.erb.rb +0 -0
  45. data/examples/readme/basic +65 -0
  46. data/examples/readme/description +54 -0
  47. data/examples/readme/note +6 -4
  48. data/examples/readme/registry +13 -0
  49. data/examples/readme/runner +28 -0
  50. data/examples/readme/runner_custom +22 -0
  51. data/lib/cl/args.rb +10 -18
  52. data/lib/cl/cast.rb +46 -24
  53. data/lib/cl/help/cmd.rb +3 -55
  54. data/lib/cl/help/format.rb +69 -0
  55. data/lib/cl/help/table.rb +5 -2
  56. data/lib/cl/opt.rb +4 -0
  57. data/lib/cl/opts/validate.rb +117 -0
  58. data/lib/cl/opts.rb +11 -101
  59. data/lib/cl/version.rb +1 -1
  60. metadata +49 -36
  61. data/examples/readme/node +0 -19
data/lib/cl/opts.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'cl/opt'
2
+ require 'cl/opts/validate'
2
3
 
3
4
  class Cl
4
5
  class Opts
5
- include Enumerable, Regex
6
+ include Enumerable, Validate
6
7
 
7
8
  def define(const, *args, &block)
8
9
  opts = args.last.is_a?(Hash) ? args.pop : {}
@@ -19,9 +20,10 @@ class Cl
19
20
  cmd.deprecations = deprecations(cmd, opts)
20
21
  opts = with_defaults(cmd, opts)
21
22
  opts = downcase(opts)
23
+ opts = upcase(opts)
22
24
  opts = cast(opts)
23
25
  opts = taint(opts)
24
- validate(cmd, opts)
26
+ validate(cmd, self, opts)
25
27
  opts
26
28
  end
27
29
 
@@ -69,93 +71,6 @@ class Cl
69
71
  defs.map(&:deprecated).to_h
70
72
  end
71
73
 
72
- def validate(cmd, opts)
73
- validate_requireds(cmd, opts)
74
- validate_required(opts)
75
- validate_requires(opts)
76
- validate_range(opts)
77
- validate_format(opts)
78
- validate_enum(opts)
79
- end
80
-
81
- def validate_requireds(cmd, opts)
82
- opts = missing_requireds(cmd, opts)
83
- raise RequiredsOpts.new(opts) if opts.any?
84
- end
85
-
86
- def validate_required(opts)
87
- opts = missing_required(opts)
88
- # make sure we do not accept unnamed required options
89
- raise RequiredOpts.new(opts.map(&:name)) if opts.any?
90
- end
91
-
92
- def validate_requires(opts)
93
- opts = missing_requires(opts)
94
- raise RequiresOpts.new(invert(opts)) if opts.any?
95
- end
96
-
97
- def validate_range(opts)
98
- opts = out_of_range(opts)
99
- raise OutOfRange.new(opts) if opts.any?
100
- end
101
-
102
- def validate_format(opts)
103
- opts = invalid_format(opts)
104
- raise InvalidFormat.new(opts) if opts.any?
105
- end
106
-
107
- def validate_enum(opts)
108
- opts = unknown_values(opts)
109
- raise UnknownValues.new(opts) if opts.any?
110
- end
111
-
112
- def missing_requireds(cmd, opts)
113
- opts = cmd.class.required.map do |alts|
114
- alts if alts.none? { |alt| Array(alt).all? { |key| opts.key?(key) } }
115
- end.compact
116
- end
117
-
118
- def missing_required(opts)
119
- select(&:required?).select { |opt| !opts.key?(opt.name) }
120
- end
121
-
122
- def missing_requires(opts)
123
- select(&:requires?).map do |opt|
124
- next unless opts.key?(opt.name)
125
- missing = opt.requires.select { |key| !opts.key?(key) }
126
- [opt.name, missing] if missing.any?
127
- end.compact
128
- end
129
-
130
- def out_of_range(opts)
131
- self.opts.map do |opt|
132
- next unless value = opts[opt.name]
133
- range = only(opt.opts, :min, :max)
134
- [opt.name, compact(range)] if out_of_range?(range, value)
135
- end.compact
136
- end
137
-
138
- def out_of_range?(range, value)
139
- min, max = range.values_at(:min, :max)
140
- min && value < min || max && value > max
141
- end
142
-
143
- def invalid_format(opts)
144
- select(&:format?).map do |opt|
145
- value = opts[opt.name]
146
- [opt.name, opt.format] if value && !opt.formatted?(value)
147
- end.compact
148
- end
149
-
150
- def unknown_values(opts)
151
- select(&:enum?).map do |opt|
152
- value = opts[opt.name]
153
- next unless value && !opt.known?(value)
154
- known = opt.enum.map { |str| format_regex(str) }
155
- [opt.name, value, known]
156
- end.compact
157
- end
158
-
159
74
  def with_defaults(cmd, opts)
160
75
  select(&:default?).inject(opts) do |opts, opt|
161
76
  next opts if opts.key?(opt.name)
@@ -176,6 +91,13 @@ class Cl
176
91
  end
177
92
  end
178
93
 
94
+ def upcase(opts)
95
+ select(&:upcase?).inject(opts) do |opts, opt|
96
+ next opts unless value = opts[opt.name]
97
+ opts.merge(opt.name => value.to_s.upcase)
98
+ end
99
+ end
100
+
179
101
  def cast(opts)
180
102
  opts.map do |key, value|
181
103
  [key, self[key] ? self[key].cast(value) : value]
@@ -187,17 +109,5 @@ class Cl
187
109
  [key, self[key] && self[key].secret? ? value.taint : value]
188
110
  end.to_h
189
111
  end
190
-
191
- def compact(hash, *keys)
192
- hash.reject { |_, value| value.nil? }.to_h
193
- end
194
-
195
- def only(hash, *keys)
196
- hash.select { |key, _| keys.include?(key) }.to_h
197
- end
198
-
199
- def invert(hash)
200
- hash.map { |key, obj| Array(obj).map { |obj| [obj, key] } }.flatten(1).to_h
201
- end
202
112
  end
203
113
  end
data/lib/cl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Cl
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -45,6 +45,45 @@ files:
45
45
  - MIT_LICENSE.md
46
46
  - NOTES.md
47
47
  - README.md
48
+ - examples/README.md
49
+ - examples/_src/args/cast.erb.rb
50
+ - examples/_src/args/opts.erb.rb
51
+ - examples/_src/args/required.erb.rb
52
+ - examples/_src/args/splat.erb.rb
53
+ - examples/_src/gem.erb.rb
54
+ - examples/_src/heroku.erb.rb
55
+ - examples/_src/rakeish.erb.rb
56
+ - examples/_src/readme/abstract.erb.rb
57
+ - examples/_src/readme/alias.erb.rb
58
+ - examples/_src/readme/arg.erb.rb
59
+ - examples/_src/readme/arg_array.erb.rb
60
+ - examples/_src/readme/arg_type.erb.rb
61
+ - examples/_src/readme/args_splat.erb.rb
62
+ - examples/_src/readme/array.erb.rb
63
+ - examples/_src/readme/basic.erb.rb
64
+ - examples/_src/readme/default.erb.rb
65
+ - examples/_src/readme/deprecated.erb.rb
66
+ - examples/_src/readme/deprecated_alias.erb.rb
67
+ - examples/_src/readme/description.erb.rb
68
+ - examples/_src/readme/downcase.erb.rb
69
+ - examples/_src/readme/enum.erb.rb
70
+ - examples/_src/readme/example.erb.rb
71
+ - examples/_src/readme/format.erb.rb
72
+ - examples/_src/readme/internal.erb.rb
73
+ - examples/_src/readme/negate.erb.rb
74
+ - examples/_src/readme/note.erb.rb
75
+ - examples/_src/readme/opts.erb.rb
76
+ - examples/_src/readme/opts_block.erb.rb
77
+ - examples/_src/readme/range.erb.rb
78
+ - examples/_src/readme/registry.erb.rb
79
+ - examples/_src/readme/required.erb.rb
80
+ - examples/_src/readme/requireds.erb.rb
81
+ - examples/_src/readme/requires.erb.rb
82
+ - examples/_src/readme/runner.erb.rb
83
+ - examples/_src/readme/runner_custom.erb.rb
84
+ - examples/_src/readme/secret.erb.rb
85
+ - examples/_src/readme/see.erb.rb
86
+ - examples/_src/readme/type.erb.rb
48
87
  - examples/args/cast
49
88
  - examples/args/opts
50
89
  - examples/args/required
@@ -59,59 +98,30 @@ files:
59
98
  - examples/readme/arg_type
60
99
  - examples/readme/args_splat
61
100
  - examples/readme/array
101
+ - examples/readme/basic
62
102
  - examples/readme/default
63
103
  - examples/readme/deprecated
64
104
  - examples/readme/deprecated_alias
105
+ - examples/readme/description
65
106
  - examples/readme/downcase
66
107
  - examples/readme/enum
67
108
  - examples/readme/example
68
109
  - examples/readme/format
69
110
  - examples/readme/internal
70
111
  - examples/readme/negate
71
- - examples/readme/node
72
112
  - examples/readme/note
73
113
  - examples/readme/opts
74
114
  - examples/readme/opts_block
75
115
  - examples/readme/range
116
+ - examples/readme/registry
76
117
  - examples/readme/required
77
118
  - examples/readme/requireds
78
119
  - examples/readme/requires
120
+ - examples/readme/runner
121
+ - examples/readme/runner_custom
79
122
  - examples/readme/secret
80
123
  - examples/readme/see
81
124
  - examples/readme/type
82
- - examples/src/args/cast.erb.rb
83
- - examples/src/args/opts.erb.rb
84
- - examples/src/args/required.erb.rb
85
- - examples/src/args/splat.erb.rb
86
- - examples/src/gem.erb.rb
87
- - examples/src/heroku.erb.rb
88
- - examples/src/rakeish.erb.rb
89
- - examples/src/readme/abstract.erb.rb
90
- - examples/src/readme/alias.erb.rb
91
- - examples/src/readme/arg.erb.rb
92
- - examples/src/readme/arg_array.erb.rb
93
- - examples/src/readme/arg_type.erb.rb
94
- - examples/src/readme/args_splat.erb.rb
95
- - examples/src/readme/array.erb.rb
96
- - examples/src/readme/default.erb.rb
97
- - examples/src/readme/deprecated.erb.rb
98
- - examples/src/readme/deprecated_alias.erb.rb
99
- - examples/src/readme/downcase.erb.rb
100
- - examples/src/readme/enum.erb.rb
101
- - examples/src/readme/example.erb.rb
102
- - examples/src/readme/format.erb.rb
103
- - examples/src/readme/internal.erb.rb
104
- - examples/src/readme/negate.erb.rb
105
- - examples/src/readme/node.erb.rb
106
- - examples/src/readme/opts.erb.rb
107
- - examples/src/readme/opts_block.erb.rb
108
- - examples/src/readme/range.erb.rb
109
- - examples/src/readme/required.erb.rb
110
- - examples/src/readme/requireds.erb.rb
111
- - examples/src/readme/requires.erb.rb
112
- - examples/src/readme/secret.erb.rb
113
- - examples/src/readme/see.erb.rb
114
- - examples/src/readme/type.erb.rb
115
125
  - lib/cl.rb
116
126
  - lib/cl/arg.rb
117
127
  - lib/cl/args.rb
@@ -126,11 +136,13 @@ files:
126
136
  - lib/cl/help.rb
127
137
  - lib/cl/help/cmd.rb
128
138
  - lib/cl/help/cmds.rb
139
+ - lib/cl/help/format.rb
129
140
  - lib/cl/help/table.rb
130
141
  - lib/cl/help/usage.rb
131
142
  - lib/cl/helper.rb
132
143
  - lib/cl/opt.rb
133
144
  - lib/cl/opts.rb
145
+ - lib/cl/opts/validate.rb
134
146
  - lib/cl/parser.rb
135
147
  - lib/cl/runner.rb
136
148
  - lib/cl/runner/default.rb
@@ -156,7 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
168
  - !ruby/object:Gem::Version
157
169
  version: '0'
158
170
  requirements: []
159
- rubygems_version: 3.0.3
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.13
160
173
  signing_key:
161
174
  specification_version: 4
162
175
  summary: Object-oriented OptionParser based CLI support
data/examples/readme/node DELETED
@@ -1,19 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $: << File.expand_path('lib')
3
-
4
- require 'cl'
5
-
6
- class Add < Cl::Cmd
7
- opt '--to GROUP', note: 'needs to be a group'
8
- end
9
-
10
- Cl.new('owners').run(%w(add --help))
11
-
12
- # Output:
13
- #
14
- # Usage: owners add [options]
15
- #
16
- # Options:
17
- #
18
- # --to GROUP type: string, note: needs to be a group
19
- # --help Get help on this command