Sub#commands|CMD#commands should yield(name,self).
assert_type for debugging. Commented out.
This commit is contained in:
parent
09c4c46d25
commit
df15912e7a
|
@ -4,8 +4,9 @@ require 'pathname'
|
||||||
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
|
$:.unshift Pathname.new(__FILE__).dirname.dirname.join('lib').to_s
|
||||||
require 'dencli'
|
require 'dencli'
|
||||||
|
|
||||||
cli = DenCli.new 'example', "This is an example for generate a DenCli-API"
|
cli = DenCli.new :example, "This is an example for generate a DenCli-API"
|
||||||
cli.cmd( :args, "Expects and prints given arguments", &lambda {|a, b, c:, d:, e:|
|
cli.cmd( :args, "Expects and prints given arguments",
|
||||||
|
&lambda {|a, b, c:, d:, e:|
|
||||||
p a: a, b: b, c: c, d: d, e: e
|
p a: a, b: b, c: c, d: d, e: e
|
||||||
}).
|
}).
|
||||||
opt( :c, '-c=ForC', "Option c").
|
opt( :c, '-c=ForC', "Option c").
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
|
||||||
|
|
||||||
class DenCli
|
class DenCli
|
||||||
|
def self.assert_type klass, method, argument_name, object, *types
|
||||||
|
unless types.any? {|type| object.is_a? type }
|
||||||
|
raise ArgumentError, "#{klass.name}.#{method} expects #{types.map( &:to_s).join '|' } for #{argument_name}, not: #{object.inspect}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class UsageError < ::RuntimeError
|
class UsageError < ::RuntimeError
|
||||||
end
|
end
|
||||||
class UnknownCommand < UsageError
|
class UnknownCommand < UsageError
|
||||||
|
|
|
@ -80,8 +80,8 @@ class DenCli::CMD
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def commands *args, &exe
|
def commands *args
|
||||||
yield self
|
yield name, self
|
||||||
end
|
end
|
||||||
|
|
||||||
def goto *args
|
def goto *args
|
||||||
|
|
|
@ -4,6 +4,9 @@ class DenCli::Sub
|
||||||
attr_reader :parent, :name, :description, :subs, :aliases
|
attr_reader :parent, :name, :description, :subs, :aliases
|
||||||
|
|
||||||
def initialize parent, name, description
|
def initialize parent, name, description
|
||||||
|
#DenCli::assert_type self, __method__, :name, name, Symbol
|
||||||
|
#DenCli::assert_type self, __method__, :parent, parent, DenCli, DenCli::Sub
|
||||||
|
#DenCli::assert_type self, __method__, :description, description, String
|
||||||
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,7 +36,7 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
|
|
||||||
def commands &exe
|
def commands &exe
|
||||||
yield self
|
yield name, self
|
||||||
@subs.each {|k, c| c.commands &exe }
|
@subs.each {|k, c| c.commands &exe }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -44,8 +47,8 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._help_commands output, subs
|
def self._help_commands output, subs
|
||||||
m = subs.map {|c| x = c.usage.length; 25 < x ? 0 : x }.max
|
m = subs.map {|_name,c| x = c.usage.length; 25 < x ? 0 : x }.max
|
||||||
subs.each do |c|
|
subs.each do |_name, c|
|
||||||
if 25 < c.usage.length
|
if 25 < c.usage.length
|
||||||
output << "% -#{m}s\n#{' ' * m} " % [c.usage]
|
output << "% -#{m}s\n#{' ' * m} " % [c.usage]
|
||||||
else
|
else
|
||||||
|
@ -96,6 +99,10 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
|
|
||||||
def _add name, min, obj, aliases
|
def _add name, min, obj, aliases
|
||||||
|
#DenCli::assert_type self, __method__, :name, name, Symbol, NilClass
|
||||||
|
#DenCli::assert_type self, __method__, :min, min, Integer, NilClass
|
||||||
|
#DenCli::assert_type self, __method__, :obj, obj, DenCli::Sub, DenCli::CMD
|
||||||
|
#DenCli::assert_type self, __method__, :aliases, aliases, Array, NilClass
|
||||||
name = name.to_s unless name.nil?
|
name = name.to_s unless name.nil?
|
||||||
@subs[name] = obj
|
@subs[name] = obj
|
||||||
DenCli.gen_aliases( name, min) {|a| @aliases[a] = obj }
|
DenCli.gen_aliases( name, min) {|a| @aliases[a] = obj }
|
||||||
|
|
Loading…
Reference in a new issue