desc renamed to description. arguments implemented (first dirty try)

This commit is contained in:
Denis Knauf 2021-01-03 13:42:00 +01:00
parent cae3692008
commit 27ca6faf31
5 changed files with 44 additions and 22 deletions

View file

@ -1,3 +1,5 @@
require 'optparse'
class DenCli
class UsageError < ::RuntimeError
end
@ -54,8 +56,8 @@ class DenCli
attr_reader :subs
def initialize progname, desc
@subs = Sub.new self, progname, desc
def initialize progname, description
@subs = Sub.new self, progname, description
end
def full_cmd

View file

@ -1,18 +1,33 @@
require_relative '../dencli'
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
@parent, @name, @desc, @exe = parent, name, desc, exe
@parent, @name, @description, @exe = parent, name, description, exe
@arguments = []
completion {|*a| [] }
end
def _full_cmd( post) parent._full_cmd [@name]+post end
def full_cmd() _full_cmd [] end
def call( *a) @exe.call *a end
def help() "#{parent.full_cmd.join ' '} #{name}\n#{ desc}" end
def call( *as)
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
@ -21,10 +36,15 @@ class DenCli::CMD
self
end
def arg name, *as, **os, &exe
@arguments.push [name.to_s.to_sym, as, os, exe || lambda{|*a|a} ]
self
end
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,
@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
]
end

View file

@ -90,7 +90,7 @@ class DenCli::Interactive
c.subs.values.each do |n|
case n
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
end
n.cmd '', "", min: 2, aliases: [nil] do

View file

@ -1,10 +1,10 @@
require_relative '../dencli'
class DenCli::Sub
attr_reader :parent, :name, :desc, :subs, :aliases
attr_reader :parent, :name, :description, :subs, :aliases
def initialize parent, name, desc
@parent, @name, @desc, @subs, @aliases = parent, name, "-> #{desc}", {}, {}
def initialize parent, name, description
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
end
def _full_cmd( post) parent._full_cmd [@name]+post end
@ -13,10 +13,10 @@ class DenCli::Sub
def help n = nil, *a
if n.nil?
r = "#{full_cmd.join ' '}: #{desc}\n\n"
r = "#{full_cmd.join ' '}: #{description}\n\n"
m = @subs.map {|k,_| k.length }.max
@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
r
elsif @aliases.has_key? n
@ -46,13 +46,13 @@ class DenCli::Sub
end
private :_add
def sub name, desc, min: nil, aliases: nil, &exe
r = _add name, min, DenCli::Sub.new( self, name, desc), aliases
def sub name, description, min: nil, aliases: nil, &exe
r = _add name, min, DenCli::Sub.new( self, name, description), aliases
block_given? ? yield( r) : r
end
def cmd name, desc, min: nil, aliases: nil, &exe
_add name, min, DenCli::CMD.new( self, name, desc, exe), aliases
def cmd name, description, min: nil, aliases: nil, &exe
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
end
def complete *pre, str
@ -66,9 +66,9 @@ class DenCli::Sub
end
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,
@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

View file

@ -1,3 +1,3 @@
class DenCli
VERSION = '0.2.1'
VERSION = '0.3.0'
end