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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b139df1946e2bbf2534d8bcd9a5d22927d8b18f2353f12687789405c783077cf
4
- data.tar.gz: 7cd494d8569c7fe6c56364d25f8b79da3d21511160cb308854096dc4f157e5da
3
+ metadata.gz: 9e0750d87cf5ab625735c828e2933f322833f7be3bd87da4fbf1d2ec0772095d
4
+ data.tar.gz: dbd621dbf01b044e561a188c3c62bf06b6c0c8f65be6293b3d3543c5417315b8
5
5
  SHA512:
6
- metadata.gz: ecbab8d3d760c9c88a023b0bb13e6ca185e7b91b4c2519452db7bae7e3254b625fde572325423cc5476866978dd92c0d2e931819308e34b35ff50ce474578915
7
- data.tar.gz: d7f8d1dc2bba5c493864091c3b15c577892465a305a4fd826ecb6013c51e729f0245d1a1ad0ada935dd8f7a1928aac85f23d014a52a0ba468fd9c945b34cdc95
6
+ metadata.gz: 44fc0fc7fcad04526caa0d73aed2a6cdb77cdc1a9773e29b39565fb37ae3dbe82f5b9c58141d6cd207a7e8404b96b45ba90f22b61393991c3daa7e6b938ec5f6
7
+ data.tar.gz: bdaeea3685e4629eb98720bd8934b20b7da4c85f57a368ce6733284ca0641cb5206e5277c4db48ae6490fb9aaeb06359fd1e4cb8eb01b153fd77366a0bbbb8a7
@@ -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, desc
58
- @subs = Sub.new self, progname, desc
59
+ def initialize progname, description
60
+ @subs = Sub.new self, progname, description
59
61
  end
60
62
 
61
63
  def full_cmd
@@ -1,18 +1,33 @@
1
1
  require_relative '../dencli'
2
2
 
3
3
  class DenCli::CMD
4
- attr_reader :parent, :name, :desc, :exe, :completion
4
+ attr_reader :parent, :name, :description, :exe, :completion, :arguments
5
5
 
6
- def initialize parent, name, desc, exe
6
+ def initialize parent, name, description, exe
7
7
  raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
8
- @parent, @name, @desc, @exe = parent, name, desc, exe
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( *a) @exe.call *a end
15
- def help() "#{parent.full_cmd.join ' '} #{name}\n#{ desc}" end
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 @desc=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
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, @desc, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
47
+ @name, @description, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
28
48
  @exe.arity
29
49
  ]
30
50
  end
@@ -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.desc[3..-1]}", min: 2 do
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
@@ -1,10 +1,10 @@
1
1
  require_relative '../dencli'
2
2
 
3
3
  class DenCli::Sub
4
- attr_reader :parent, :name, :desc, :subs, :aliases
4
+ attr_reader :parent, :name, :description, :subs, :aliases
5
5
 
6
- def initialize parent, name, desc
7
- @parent, @name, @desc, @subs, @aliases = parent, name, "-> #{desc}", {}, {}
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 ' '}: #{desc}\n\n"
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.desc] unless k.nil?
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, desc, min: nil, aliases: nil, &exe
50
- r = _add name, min, DenCli::Sub.new( self, name, desc), aliases
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, desc, min: nil, aliases: nil, &exe
55
- _add name, min, DenCli::CMD.new( self, name, desc, exe), aliases
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 @desc=%p @subs={%s} @aliases={%s} @parent=<%s:0x%x %s>>" % [
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, @desc, @subs.keys.join(', '), @aliases.keys.join(', '), @parent.class.name, @parent.class.object_id, @parent.full_cmd
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
@@ -1,3 +1,3 @@
1
1
  class DenCli
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.1
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: 2020-12-27 00:00:00.000000000 Z
11
+ date: 2021-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec