dencli 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dencli/cmd.rb +37 -8
- data/lib/dencli/sub.rb +6 -2
- data/lib/dencli/version.rb +1 -1
- 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: 291671c467c0e0cd0b75a21d36e1f5b8e2ce03e50b8f6ffe42ffd29a8b6b2bf4
|
4
|
+
data.tar.gz: 78020b860dea0a56909a90b8e421201e52c7ddc8b768b21c073cebdbd273f15f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a9eb16a990f3f7570e3c714e61c613b7fd76b3b3f6d635f6681e7fe165318d8e46b8e527439ea6c5203d215b529d66b48f6674f4eb795352fd2acc623658bb7
|
7
|
+
data.tar.gz: e3802537b891dbb74221a723867b5123b74d25c4da3f258536d15cb2723b3f56f10fa9b8dcc6fe992f311dd86dae3ba1f59bcf57bb63ea14a3ddee84340db3db
|
data/lib/dencli/cmd.rb
CHANGED
@@ -1,33 +1,62 @@
|
|
1
1
|
require_relative '../dencli'
|
2
2
|
|
3
3
|
class DenCli::CMD
|
4
|
-
attr_reader :parent, :name, :description, :exe, :completion, :
|
4
|
+
attr_reader :parent, :name, :description, :exe, :completion, :options
|
5
5
|
|
6
6
|
def initialize parent, name, description, exe
|
7
7
|
raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
|
8
|
-
@parent, @name, @description, @exe = parent, name, description, exe
|
9
|
-
@
|
8
|
+
@parent, @name, @description, @exe = parent, name, description, lambda( &exe)
|
9
|
+
@options = []
|
10
10
|
completion {|*a| [] }
|
11
11
|
end
|
12
12
|
|
13
13
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
14
14
|
def full_cmd() _full_cmd [] end
|
15
|
+
|
16
|
+
def parameters() @exe.parameters end
|
17
|
+
def required() @exe.parameters.select{|e|:req == e[0]}.map{|e|e[1]} end
|
18
|
+
def additional() @exe.parameters.select{|e|:opt == e[0]}.map{|e|e[1]} end
|
19
|
+
|
15
20
|
def call( *as)
|
16
|
-
if @
|
21
|
+
if @options.empty?
|
17
22
|
@exe.call *as
|
18
23
|
else
|
19
24
|
os = {}
|
20
25
|
options = OptionParser.new
|
21
26
|
options.banner = "#{full_cmd.join ' '}"
|
22
|
-
@
|
27
|
+
@options.each do |(aname, aas, aos, aexe)|
|
23
28
|
os[aname] = aos[aname] if aos.has_key? :default
|
24
29
|
options.on( *aas) {|val| os[aname] = aexe[val] }
|
25
30
|
end
|
26
31
|
as = options.parse! as
|
32
|
+
if @exe.lambda?
|
33
|
+
pars = required
|
34
|
+
if as.length < pars.length
|
35
|
+
raise DenCli::UsageError, "Missing parameter(s): #{pars[as.length..-1].join " "}"
|
36
|
+
end
|
37
|
+
if parameters.select{|e|:rest == e[0]}.empty?
|
38
|
+
pars = pars + additional
|
39
|
+
if as.length > pars.length
|
40
|
+
raise DenCli::UsageError, "Unused parameter(s): #{as[-pars.length..-1].shelljoin}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
27
44
|
@exe.call *as, **os
|
28
45
|
end
|
29
46
|
end
|
30
|
-
|
47
|
+
|
48
|
+
def usage
|
49
|
+
"#{parent.full_cmd.join ' '} #{name} "+
|
50
|
+
@options.map{|(_,(o,_,_,_),_)|"[#{o}] "}.join( '')+
|
51
|
+
(@exe.lambda? ? (
|
52
|
+
required.join( " ")+
|
53
|
+
(additional.empty? ? "" : " [#{additional.join " "}]")
|
54
|
+
) : '...')
|
55
|
+
end
|
56
|
+
|
57
|
+
def help
|
58
|
+
"#{usage}\n#{description}"
|
59
|
+
end
|
31
60
|
|
32
61
|
def complete( *pre, str) @completion.call *pre, str end
|
33
62
|
|
@@ -36,8 +65,8 @@ class DenCli::CMD
|
|
36
65
|
self
|
37
66
|
end
|
38
67
|
|
39
|
-
def
|
40
|
-
@
|
68
|
+
def opt name, *as, **os, &exe
|
69
|
+
@options.push [name.to_s.to_sym, as, os, exe || lambda{|val|val} ]
|
41
70
|
self
|
42
71
|
end
|
43
72
|
|
data/lib/dencli/sub.rb
CHANGED
@@ -11,12 +11,16 @@ class DenCli::Sub
|
|
11
11
|
def full_cmd() _full_cmd [] end
|
12
12
|
def []( k) @aliases[k] end
|
13
13
|
|
14
|
+
def usage
|
15
|
+
"#{full_cmd.join ' '} ..."
|
16
|
+
end
|
17
|
+
|
14
18
|
def help n = nil, *a
|
15
19
|
if n.nil?
|
16
20
|
r = "#{full_cmd.join ' '}: #{description}\n\n"
|
17
|
-
m = @subs.map {|k,
|
21
|
+
m = @subs.map {|k,c| k.nil? ? 0 : c.usage.length }.max
|
18
22
|
@subs.each do |k, c|
|
19
|
-
r += " % -#{m}s %s\n" % [
|
23
|
+
r += " % -#{m}s %s\n" % [c.usage, c.description] unless k.nil?
|
20
24
|
end
|
21
25
|
r
|
22
26
|
elsif @aliases.has_key? n
|
data/lib/dencli/version.rb
CHANGED