desc renamed to description. arguments implemented (first dirty try)
This commit is contained in:
parent
cae3692008
commit
27ca6faf31
|
@ -1,3 +1,5 @@
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
class DenCli
|
class DenCli
|
||||||
class UsageError < ::RuntimeError
|
class UsageError < ::RuntimeError
|
||||||
end
|
end
|
||||||
|
@ -54,8 +56,8 @@ class DenCli
|
||||||
|
|
||||||
attr_reader :subs
|
attr_reader :subs
|
||||||
|
|
||||||
def initialize progname, desc
|
def initialize progname, description
|
||||||
@subs = Sub.new self, progname, desc
|
@subs = Sub.new self, progname, description
|
||||||
end
|
end
|
||||||
|
|
||||||
def full_cmd
|
def full_cmd
|
||||||
|
|
|
@ -1,18 +1,33 @@
|
||||||
require_relative '../dencli'
|
require_relative '../dencli'
|
||||||
|
|
||||||
class DenCli::CMD
|
class DenCli::CMD
|
||||||
attr_reader :parent, :name, :desc, :exe, :completion
|
attr_reader :parent, :name, :description, :exe, :completion, :arguments
|
||||||
|
|
||||||
def initialize parent, name, desc, exe
|
def initialize parent, name, description, exe
|
||||||
raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
|
raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
|
||||||
@parent, @name, @desc, @exe = parent, name, desc, exe
|
@parent, @name, @description, @exe = parent, name, description, exe
|
||||||
|
@arguments = []
|
||||||
completion {|*a| [] }
|
completion {|*a| [] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def _full_cmd( post) parent._full_cmd [@name]+post end
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
||||||
def full_cmd() _full_cmd [] end
|
def full_cmd() _full_cmd [] end
|
||||||
def call( *a) @exe.call *a end
|
def call( *as)
|
||||||
def help() "#{parent.full_cmd.join ' '} #{name}\n#{ desc}" end
|
if @arguments.empty?
|
||||||
|
@exe.call *as
|
||||||
|
else
|
||||||
|
os = {}
|
||||||
|
options = OptionParser.new
|
||||||
|
options.banner = "#{full_cmd.join ' '}"
|
||||||
|
@arguments.each do |(aname, aas, aos, aexe)|
|
||||||
|
os[aname] = aos[aname] if aos.has_key? :default
|
||||||
|
options.on( *aas) {|val| os[aname] = aexe[val] }
|
||||||
|
end
|
||||||
|
as = options.parse! as
|
||||||
|
@exe.call *as, **os
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def help() "#{parent.full_cmd.join ' '} #{name}\n#{description}" end
|
||||||
|
|
||||||
def complete( *pre, str) @completion.call *pre, str end
|
def complete( *pre, str) @completion.call *pre, str end
|
||||||
|
|
||||||
|
@ -21,10 +36,15 @@ class DenCli::CMD
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def arg name, *as, **os, &exe
|
||||||
|
@arguments.push [name.to_s.to_sym, as, os, exe || lambda{|*a|a} ]
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<%s:0x%x %s @name=%p @desc=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
|
"#<%s:0x%x %s @name=%p @description=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
|
||||||
self.class.name, self.object_id, self.full_cmd,
|
self.class.name, self.object_id, self.full_cmd,
|
||||||
@name, @desc, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
|
@name, @description, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
|
||||||
@exe.arity
|
@exe.arity
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
|
@ -90,7 +90,7 @@ class DenCli::Interactive
|
||||||
c.subs.values.each do |n|
|
c.subs.values.each do |n|
|
||||||
case n
|
case n
|
||||||
when DenCli::Sub
|
when DenCli::Sub
|
||||||
n.cmd :exit, "<- #{n.parent.full_cmd.join ' '} - #{n.parent.desc[3..-1]}", min: 2 do
|
n.cmd :exit, "<- #{n.parent.full_cmd.join ' '} - #{n.parent.description[3..-1]}", min: 2 do
|
||||||
@cur = n.parent
|
@cur = n.parent
|
||||||
end
|
end
|
||||||
n.cmd '', "", min: 2, aliases: [nil] do
|
n.cmd '', "", min: 2, aliases: [nil] do
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
require_relative '../dencli'
|
require_relative '../dencli'
|
||||||
|
|
||||||
class DenCli::Sub
|
class DenCli::Sub
|
||||||
attr_reader :parent, :name, :desc, :subs, :aliases
|
attr_reader :parent, :name, :description, :subs, :aliases
|
||||||
|
|
||||||
def initialize parent, name, desc
|
def initialize parent, name, description
|
||||||
@parent, @name, @desc, @subs, @aliases = parent, name, "-> #{desc}", {}, {}
|
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def _full_cmd( post) parent._full_cmd [@name]+post end
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
||||||
|
@ -13,10 +13,10 @@ class DenCli::Sub
|
||||||
|
|
||||||
def help n = nil, *a
|
def help n = nil, *a
|
||||||
if n.nil?
|
if n.nil?
|
||||||
r = "#{full_cmd.join ' '}: #{desc}\n\n"
|
r = "#{full_cmd.join ' '}: #{description}\n\n"
|
||||||
m = @subs.map {|k,_| k.length }.max
|
m = @subs.map {|k,_| k.length }.max
|
||||||
@subs.each do |k, c|
|
@subs.each do |k, c|
|
||||||
r += " % -#{m}s %s\n" % [k, c.desc] unless k.nil?
|
r += " % -#{m}s %s\n" % [k, c.description] unless k.nil?
|
||||||
end
|
end
|
||||||
r
|
r
|
||||||
elsif @aliases.has_key? n
|
elsif @aliases.has_key? n
|
||||||
|
@ -46,13 +46,13 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
private :_add
|
private :_add
|
||||||
|
|
||||||
def sub name, desc, min: nil, aliases: nil, &exe
|
def sub name, description, min: nil, aliases: nil, &exe
|
||||||
r = _add name, min, DenCli::Sub.new( self, name, desc), aliases
|
r = _add name, min, DenCli::Sub.new( self, name, description), aliases
|
||||||
block_given? ? yield( r) : r
|
block_given? ? yield( r) : r
|
||||||
end
|
end
|
||||||
|
|
||||||
def cmd name, desc, min: nil, aliases: nil, &exe
|
def cmd name, description, min: nil, aliases: nil, &exe
|
||||||
_add name, min, DenCli::CMD.new( self, name, desc, exe), aliases
|
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete *pre, str
|
def complete *pre, str
|
||||||
|
@ -66,9 +66,9 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<%s:0x%x %s @name=%p @desc=%p @subs={%s} @aliases={%s} @parent=<%s:0x%x %s>>" % [
|
"#<%s:0x%x %s @name=%p @description=%p @subs={%s} @aliases={%s} @parent=<%s:0x%x %s>>" % [
|
||||||
self.class.name, self.object_id, self.full_cmd,
|
self.class.name, self.object_id, self.full_cmd,
|
||||||
@name, @desc, @subs.keys.join(', '), @aliases.keys.join(', '), @parent.class.name, @parent.class.object_id, @parent.full_cmd
|
@name, @description, @subs.keys.join(', '), @aliases.keys.join(', '), @parent.class.name, @parent.class.object_id, @parent.full_cmd
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
class DenCli
|
class DenCli
|
||||||
VERSION = '0.2.1'
|
VERSION = '0.3.0'
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue