cl 0.1.25 → 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +42 -8
- data/examples/readme/format +8 -8
- data/examples/readme/negate +27 -0
- data/lib/cl/opt.rb +14 -7
- data/lib/cl/parser.rb +38 -5
- data/lib/cl/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cfda6ea85d151f58e3705bebe492eae526565db9
|
4
|
+
data.tar.gz: 491a59779fb7c13c144f89f2af7cfc1938466bad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c298f35079ea96d36e2d6e8b1e9c2da37cffed21b4a380b60102beb3b8749d2374c678f550df73b5f30f2dfc5fd7182bbaed330f490dce2daaa0f6b2f3ebc577
|
7
|
+
data.tar.gz: 6728ad70ef348061b6b555144e6dfab65b6187aff46bd5c3816c9192e8b6d0449290185dccf062db9393748a2554c7277927b10b027fb6920f1dac6f7ae1b4e7
|
data/CHANGELOG.md
CHANGED
@@ -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`
|
data/Gemfile.lock
CHANGED
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
|
-
|
665
|
-
|
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.
|
data/examples/readme/format
CHANGED
@@ -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
|
-
|
29
|
-
|
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
|
data/lib/cl/opt.rb
CHANGED
@@ -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
|
-
|
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.
|
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
|
data/lib/cl/parser.rb
CHANGED
@@ -9,22 +9,28 @@ class Cl
|
|
9
9
|
|
10
10
|
super do
|
11
11
|
opts.each do |opt|
|
12
|
-
on(*
|
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(*
|
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(
|
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(
|
44
|
-
strs.map
|
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
|
data/lib/cl/version.rb
CHANGED
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.
|
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-
|
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
|
-
|
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
|