dencli 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dencli.rb +4 -2
- data/lib/dencli/cmd.rb +27 -7
- data/lib/dencli/interactive.rb +1 -1
- data/lib/dencli/sub.rb +11 -11
- data/lib/dencli/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e0750d87cf5ab625735c828e2933f322833f7be3bd87da4fbf1d2ec0772095d
|
4
|
+
data.tar.gz: dbd621dbf01b044e561a188c3c62bf06b6c0c8f65be6293b3d3543c5417315b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44fc0fc7fcad04526caa0d73aed2a6cdb77cdc1a9773e29b39565fb37ae3dbe82f5b9c58141d6cd207a7e8404b96b45ba90f22b61393991c3daa7e6b938ec5f6
|
7
|
+
data.tar.gz: bdaeea3685e4629eb98720bd8934b20b7da4c85f57a368ce6733284ca0641cb5206e5277c4db48ae6490fb9aaeb06359fd1e4cb8eb01b153fd77366a0bbbb8a7
|
data/lib/dencli.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
1
3
|
class DenCli
|
2
4
|
class UsageError < ::RuntimeError
|
3
5
|
end
|
@@ -54,8 +56,8 @@ class DenCli
|
|
54
56
|
|
55
57
|
attr_reader :subs
|
56
58
|
|
57
|
-
def initialize progname,
|
58
|
-
@subs = Sub.new self, progname,
|
59
|
+
def initialize progname, description
|
60
|
+
@subs = Sub.new self, progname, description
|
59
61
|
end
|
60
62
|
|
61
63
|
def full_cmd
|
data/lib/dencli/cmd.rb
CHANGED
@@ -1,18 +1,33 @@
|
|
1
1
|
require_relative '../dencli'
|
2
2
|
|
3
3
|
class DenCli::CMD
|
4
|
-
attr_reader :parent, :name, :
|
4
|
+
attr_reader :parent, :name, :description, :exe, :completion, :arguments
|
5
5
|
|
6
|
-
def initialize parent, name,
|
6
|
+
def initialize parent, name, description, exe
|
7
7
|
raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
|
8
|
-
@parent, @name, @
|
8
|
+
@parent, @name, @description, @exe = parent, name, description, exe
|
9
|
+
@arguments = []
|
9
10
|
completion {|*a| [] }
|
10
11
|
end
|
11
12
|
|
12
13
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
13
14
|
def full_cmd() _full_cmd [] end
|
14
|
-
def call( *
|
15
|
-
|
15
|
+
def call( *as)
|
16
|
+
if @arguments.empty?
|
17
|
+
@exe.call *as
|
18
|
+
else
|
19
|
+
os = {}
|
20
|
+
options = OptionParser.new
|
21
|
+
options.banner = "#{full_cmd.join ' '}"
|
22
|
+
@arguments.each do |(aname, aas, aos, aexe)|
|
23
|
+
os[aname] = aos[aname] if aos.has_key? :default
|
24
|
+
options.on( *aas) {|val| os[aname] = aexe[val] }
|
25
|
+
end
|
26
|
+
as = options.parse! as
|
27
|
+
@exe.call *as, **os
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def help() "#{parent.full_cmd.join ' '} #{name}\n#{description}" end
|
16
31
|
|
17
32
|
def complete( *pre, str) @completion.call *pre, str end
|
18
33
|
|
@@ -21,10 +36,15 @@ class DenCli::CMD
|
|
21
36
|
self
|
22
37
|
end
|
23
38
|
|
39
|
+
def arg name, *as, **os, &exe
|
40
|
+
@arguments.push [name.to_s.to_sym, as, os, exe || lambda{|*a|a} ]
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
24
44
|
def inspect
|
25
|
-
"#<%s:0x%x %s @name=%p @
|
45
|
+
"#<%s:0x%x %s @name=%p @description=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
|
26
46
|
self.class.name, self.object_id, self.full_cmd,
|
27
|
-
@name, @
|
47
|
+
@name, @description, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
|
28
48
|
@exe.arity
|
29
49
|
]
|
30
50
|
end
|
data/lib/dencli/interactive.rb
CHANGED
@@ -90,7 +90,7 @@ class DenCli::Interactive
|
|
90
90
|
c.subs.values.each do |n|
|
91
91
|
case n
|
92
92
|
when DenCli::Sub
|
93
|
-
n.cmd :exit, "<- #{n.parent.full_cmd.join ' '} - #{n.parent.
|
93
|
+
n.cmd :exit, "<- #{n.parent.full_cmd.join ' '} - #{n.parent.description[3..-1]}", min: 2 do
|
94
94
|
@cur = n.parent
|
95
95
|
end
|
96
96
|
n.cmd '', "", min: 2, aliases: [nil] do
|
data/lib/dencli/sub.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require_relative '../dencli'
|
2
2
|
|
3
3
|
class DenCli::Sub
|
4
|
-
attr_reader :parent, :name, :
|
4
|
+
attr_reader :parent, :name, :description, :subs, :aliases
|
5
5
|
|
6
|
-
def initialize parent, name,
|
7
|
-
@parent, @name, @
|
6
|
+
def initialize parent, name, description
|
7
|
+
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
8
8
|
end
|
9
9
|
|
10
10
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
@@ -13,10 +13,10 @@ class DenCli::Sub
|
|
13
13
|
|
14
14
|
def help n = nil, *a
|
15
15
|
if n.nil?
|
16
|
-
r = "#{full_cmd.join ' '}: #{
|
16
|
+
r = "#{full_cmd.join ' '}: #{description}\n\n"
|
17
17
|
m = @subs.map {|k,_| k.length }.max
|
18
18
|
@subs.each do |k, c|
|
19
|
-
r += " % -#{m}s %s\n" % [k, c.
|
19
|
+
r += " % -#{m}s %s\n" % [k, c.description] unless k.nil?
|
20
20
|
end
|
21
21
|
r
|
22
22
|
elsif @aliases.has_key? n
|
@@ -46,13 +46,13 @@ class DenCli::Sub
|
|
46
46
|
end
|
47
47
|
private :_add
|
48
48
|
|
49
|
-
def sub name,
|
50
|
-
r = _add name, min, DenCli::Sub.new( self, name,
|
49
|
+
def sub name, description, min: nil, aliases: nil, &exe
|
50
|
+
r = _add name, min, DenCli::Sub.new( self, name, description), aliases
|
51
51
|
block_given? ? yield( r) : r
|
52
52
|
end
|
53
53
|
|
54
|
-
def cmd name,
|
55
|
-
_add name, min, DenCli::CMD.new( self, name,
|
54
|
+
def cmd name, description, min: nil, aliases: nil, &exe
|
55
|
+
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
|
56
56
|
end
|
57
57
|
|
58
58
|
def complete *pre, str
|
@@ -66,9 +66,9 @@ class DenCli::Sub
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def inspect
|
69
|
-
"#<%s:0x%x %s @name=%p @
|
69
|
+
"#<%s:0x%x %s @name=%p @description=%p @subs={%s} @aliases={%s} @parent=<%s:0x%x %s>>" % [
|
70
70
|
self.class.name, self.object_id, self.full_cmd,
|
71
|
-
@name, @
|
71
|
+
@name, @description, @subs.keys.join(', '), @aliases.keys.join(', '), @parent.class.name, @parent.class.object_id, @parent.full_cmd
|
72
72
|
]
|
73
73
|
end
|
74
74
|
end
|
data/lib/dencli/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dencli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Knauf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|