cl 0.1.25 → 0.1.26

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
- SHA256:
3
- metadata.gz: fdd4074de1e4d5cd388b007470f2670288ceea192d9759e8a2f023970ae814eb
4
- data.tar.gz: 9e65045265af54581182190eb281f594e6154d2db4bd54481af2e894405b6d02
2
+ SHA1:
3
+ metadata.gz: cfda6ea85d151f58e3705bebe492eae526565db9
4
+ data.tar.gz: 491a59779fb7c13c144f89f2af7cfc1938466bad
5
5
  SHA512:
6
- metadata.gz: 71b0cb12a6a341e6dc92c11f0035550cea52978419bd4f9038f62e2590aa8782486f752742d22060a23a2d70aed0b08e12e8fa564d7d1898f7a8e6c707249f7a
7
- data.tar.gz: e7c17550880c5fd107a3e1afb463e6057fbfb834481c87e58cef744b044f98feef6e501137835b6fd2f9893669bad0999b3fa5ced89ce794e11b6079b1d970f9
6
+ metadata.gz: c298f35079ea96d36e2d6e8b1e9c2da37cffed21b4a380b60102beb3b8749d2374c678f550df73b5f30f2dfc5fd7182bbaed330f490dce2daaa0f6b2f3ebc577
7
+ data.tar.gz: 6728ad70ef348061b6b555144e6dfab65b6187aff46bd5c3816c9192e8b6d0449290185dccf062db9393748a2554c7277927b10b027fb6920f1dac6f7ae1b4e7
@@ -21,6 +21,7 @@
21
21
  * Add `opt '--one STR', downcase: true`
22
22
  * Add `opt '--one STR', internal: true`, hide internal options from help output
23
23
  * Add `opt '--one STR', example: 'foo'`
24
+ * Add `opt '--one STR', negate: %w(skip)`
24
25
  * Add `opt '--one STR', note: 'note'`
25
26
  * Add `opt '--one STR', see: 'https://provider.com/docs'
26
27
  * Add `opt '--one STR', secret: true`
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cl (0.1.23)
4
+ cl (0.1.25)
5
5
  regstry (~> 1.0.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -37,6 +37,7 @@ Further documentation is available on [rubydoc.info](https://www.rubydoc.info/gi
37
37
  * [Format](#format)
38
38
  * [Internal](#internal)
39
39
  * [Min and Max](#min-and-max)
40
+ * [Negations](#negations)
40
41
  * [Note](#note)
41
42
  * [Secret](#secret)
42
43
  * [See Also](#see-also)
@@ -655,14 +656,14 @@ Cl.new('owners').run(%w(add --to one))
655
656
 
656
657
  Cl.new('owners').run(['add', '--to', 'does not match!'])
657
658
 
658
- Invalid format: to (format: /^\w+$/)
659
-
660
- Usage: format add [options]
661
-
662
- Options:
663
-
664
- --to GROUP type: string, format: /^\w+$/
665
- --help Get help on this command
659
+ # Invalid format: to (format: /^\w+$/)
660
+ #
661
+ # Usage: format add [options]
662
+ #
663
+ # Options:
664
+ #
665
+ # --to GROUP type: string, format: /^\w+$/
666
+ # --help Get help on this command
666
667
 
667
668
  ```
668
669
 
@@ -727,6 +728,39 @@ Cl.new('owners').run(%w(add --retries 10))
727
728
 
728
729
  ```
729
730
 
731
+ #### Negations
732
+
733
+ Flags (boolean options) automatically allow negation using `--no-*` and
734
+ `--no_*` using OptionParser's support for these. However, sometimes it can be
735
+ convenient to allow other terms for negating an option. Flags therefore accept
736
+ an option `negate` like so:
737
+
738
+ ```ruby
739
+ class Add < Cl::Cmd
740
+ opt '--notifications', 'Send out notifications to the team', negate: %w(skip)
741
+
742
+ def run
743
+ p notifications?
744
+ end
745
+ end
746
+
747
+ Cl.new('owners').run(%w(add --notifications))
748
+ # => true
749
+
750
+ Cl.new('owners').run(%w(add --no_notifications))
751
+ # => false
752
+
753
+ Cl.new('owners').run(%w(add --no-notifications))
754
+ # => false
755
+
756
+ Cl.new('owners').run(%w(add --skip_notifications))
757
+ # => false
758
+
759
+ Cl.new('owners').run(%w(add --skip-notifications))
760
+ # => false
761
+
762
+ ```
763
+
730
764
  #### Note
731
765
 
732
766
  Options can have a note that will be printed in the help output.
@@ -19,11 +19,11 @@ Cl.new('owners').run(%w(add --to one))
19
19
 
20
20
  Cl.new('owners').run(['add', '--to', 'does not match!'])
21
21
 
22
- Invalid format: to (format: /^\w+$/)
23
-
24
- Usage: format add [options]
25
-
26
- Options:
27
-
28
- --to GROUP type: string, format: /^\w+$/
29
- --help Get help on this command
22
+ # Invalid format: to (format: /^\w+$/)
23
+ #
24
+ # Usage: format add [options]
25
+ #
26
+ # Options:
27
+ #
28
+ # --to GROUP type: string, format: /^\w+$/
29
+ # --help Get help on this command
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path('lib')
3
+
4
+ require 'cl'
5
+
6
+ class Add < Cl::Cmd
7
+ opt '--notifications', 'Send out notifications to the team', negate: %w(skip)
8
+
9
+ def run
10
+ p notifications?
11
+ end
12
+ end
13
+
14
+ Cl.new('owners').run(%w(add --notifications))
15
+ # => true
16
+
17
+ Cl.new('owners').run(%w(add --no_notifications))
18
+ # => false
19
+
20
+ Cl.new('owners').run(%w(add --no-notifications))
21
+ # => false
22
+
23
+ Cl.new('owners').run(%w(add --skip_notifications))
24
+ # => false
25
+
26
+ Cl.new('owners').run(%w(add --skip-notifications))
27
+ # => false
@@ -41,6 +41,10 @@ class Cl
41
41
  strs.any? { |str| str.split(' ').size > 1 } ? :string : :flag
42
42
  end
43
43
 
44
+ def flag?
45
+ type == :flag
46
+ end
47
+
44
48
  def int?
45
49
  type == :int || type == :integer
46
50
  end
@@ -71,7 +75,7 @@ class Cl
71
75
  end
72
76
 
73
77
  def default?
74
- !!default
78
+ opts.key?(:default)
75
79
  end
76
80
 
77
81
  def default
@@ -87,12 +91,7 @@ class Cl
87
91
  end
88
92
 
89
93
  def known?(value)
90
- enum.include?(value)
91
- enum.any? { |obj| matches?(obj, value) }
92
- end
93
-
94
- def matches?(obj, value)
95
- obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value
94
+ enum.any? { |obj| obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value }
96
95
  end
97
96
 
98
97
  def example?
@@ -136,6 +135,14 @@ class Cl
136
135
  opts[:max]
137
136
  end
138
137
 
138
+ def negate?
139
+ !!opts[:negate]
140
+ end
141
+
142
+ def negate
143
+ Array(opts[:negate])
144
+ end
145
+
139
146
  def note?
140
147
  !!opts[:note]
141
148
  end
@@ -9,22 +9,28 @@ class Cl
9
9
 
10
10
  super do
11
11
  opts.each do |opt|
12
- on(*dasherize(*opt.strs)) do |value|
12
+ on(*args_for(opt, opt.strs)) do |value|
13
13
  set(opt, value)
14
14
  end
15
15
 
16
16
  opt.aliases.each do |name|
17
- on(*dasherize(aliased(opt, name))) do |value|
17
+ on(*args_for(opt, [aliased(opt, name)])) do |value|
18
18
  @opts[name] = set(opt, value)
19
19
  end
20
20
  end
21
21
  end
22
22
  end
23
23
 
24
- args.replace(dasherize(*args))
24
+ args.replace(normalize(opts, args))
25
25
  parse!(args)
26
26
  end
27
27
 
28
+ def args_for(opt, strs)
29
+ args = dasherize(strs)
30
+ args = flagerize(args) if opt.flag?
31
+ args
32
+ end
33
+
28
34
  def aliased(opt, name)
29
35
  str = opt.strs.detect { |str| str.start_with?('--') } || raise
30
36
  str = str.sub(opt.name.to_s, name.to_s)
@@ -33,15 +39,42 @@ class Cl
33
39
 
34
40
  # should consider negative arities (e.g. |one, *two|)
35
41
  def set(opt, value)
42
+ value = true if value.nil? && opt.flag?
36
43
  args = [opts, opt.type, opt.name, value]
37
44
  args = args[-opt.block.arity, opt.block.arity]
38
45
  instance_exec(*args, &opt.block)
39
46
  end
40
47
 
48
+ def normalize(opts, args)
49
+ args = noize(opts, args)
50
+ dasherize(args)
51
+ end
52
+
53
+ def noize(opts, args)
54
+ args.map do |arg|
55
+ str = negation(opts, arg)
56
+ str ? arg.sub(/^--#{str}[-_]+/, '--no-') : arg
57
+ end
58
+ end
59
+
60
+ def negation(opts, arg)
61
+ opts.detect do |opt|
62
+ str = opt.negate.detect { |str| arg =~ /^--#{str}[-_]+/ }
63
+ break str if str
64
+ end
65
+ end
66
+
41
67
  DASHERIZE = /^--([^= ])*/
42
68
 
43
- def dasherize(*strs)
44
- strs.map { |str| str.gsub(DASHERIZE) { |opt| opt.gsub('_', '-') } }
69
+ def dasherize(strs)
70
+ strs.map do |str|
71
+ str.is_a?(String) ? str.gsub(DASHERIZE) { |opt| opt.gsub('_', '-') } : str
72
+ end
73
+ end
74
+
75
+ def flagerize(strs)
76
+ strs = strs.map { |str| str.include?(' ') ? str : "#{str} [true|false|yes|no]" }
77
+ strs << TrueClass
45
78
  end
46
79
  end
47
80
  end
@@ -1,3 +1,3 @@
1
1
  class Cl
2
- VERSION = '0.1.25'
2
+ VERSION = '0.1.26'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.25
4
+ version: 0.1.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-27 00:00:00.000000000 Z
11
+ date: 2019-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: regstry
@@ -70,6 +70,7 @@ files:
70
70
  - examples/readme/example
71
71
  - examples/readme/format
72
72
  - examples/readme/internal
73
+ - examples/readme/negate
73
74
  - examples/readme/note
74
75
  - examples/readme/opts
75
76
  - examples/readme/opts_block
@@ -124,7 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  - !ruby/object:Gem::Version
125
126
  version: '0'
126
127
  requirements: []
127
- rubygems_version: 3.0.3
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.13
128
130
  signing_key:
129
131
  specification_version: 4
130
132
  summary: Object-oriented OptionParser based CLI support