dencli 0.3.0 → 0.3.1

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
2
  SHA256:
3
- metadata.gz: 9e0750d87cf5ab625735c828e2933f322833f7be3bd87da4fbf1d2ec0772095d
4
- data.tar.gz: dbd621dbf01b044e561a188c3c62bf06b6c0c8f65be6293b3d3543c5417315b8
3
+ metadata.gz: 291671c467c0e0cd0b75a21d36e1f5b8e2ce03e50b8f6ffe42ffd29a8b6b2bf4
4
+ data.tar.gz: 78020b860dea0a56909a90b8e421201e52c7ddc8b768b21c073cebdbd273f15f
5
5
  SHA512:
6
- metadata.gz: 44fc0fc7fcad04526caa0d73aed2a6cdb77cdc1a9773e29b39565fb37ae3dbe82f5b9c58141d6cd207a7e8404b96b45ba90f22b61393991c3daa7e6b938ec5f6
7
- data.tar.gz: bdaeea3685e4629eb98720bd8934b20b7da4c85f57a368ce6733284ca0641cb5206e5277c4db48ae6490fb9aaeb06359fd1e4cb8eb01b153fd77366a0bbbb8a7
6
+ metadata.gz: 2a9eb16a990f3f7570e3c714e61c613b7fd76b3b3f6d635f6681e7fe165318d8e46b8e527439ea6c5203d215b529d66b48f6674f4eb795352fd2acc623658bb7
7
+ data.tar.gz: e3802537b891dbb74221a723867b5123b74d25c4da3f258536d15cb2723b3f56f10fa9b8dcc6fe992f311dd86dae3ba1f59bcf57bb63ea14a3ddee84340db3db
@@ -1,33 +1,62 @@
1
1
  require_relative '../dencli'
2
2
 
3
3
  class DenCli::CMD
4
- attr_reader :parent, :name, :description, :exe, :completion, :arguments
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
- @arguments = []
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 @arguments.empty?
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
- @arguments.each do |(aname, aas, aos, aexe)|
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
- def help() "#{parent.full_cmd.join ' '} #{name}\n#{description}" end
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 arg name, *as, **os, &exe
40
- @arguments.push [name.to_s.to_sym, as, os, exe || lambda{|*a|a} ]
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
 
@@ -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,_| k.length }.max
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" % [k, c.description] unless k.nil?
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
@@ -1,3 +1,3 @@
1
1
  class DenCli
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dencli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Knauf