options instead of arguments. arguments checking against arity if lambda. help improved.
This commit is contained in:
parent
27ca6faf31
commit
fa5fbf8f6f
|
@ -1,33 +1,62 @@
|
||||||
require_relative '../dencli'
|
require_relative '../dencli'
|
||||||
|
|
||||||
class DenCli::CMD
|
class DenCli::CMD
|
||||||
attr_reader :parent, :name, :description, :exe, :completion, :arguments
|
attr_reader :parent, :name, :description, :exe, :completion, :options
|
||||||
|
|
||||||
def initialize parent, name, description, 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, @description, @exe = parent, name, description, exe
|
@parent, @name, @description, @exe = parent, name, description, lambda( &exe)
|
||||||
@arguments = []
|
@options = []
|
||||||
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 parameters() @exe.parameters end
|
||||||
|
def required() @exe.parameters.select{|e|:req == e[0]}.map{|e|e[1]} end
|
||||||
|
def additional() @exe.parameters.select{|e|:opt == e[0]}.map{|e|e[1]} end
|
||||||
|
|
||||||
def call( *as)
|
def call( *as)
|
||||||
if @arguments.empty?
|
if @options.empty?
|
||||||
@exe.call *as
|
@exe.call *as
|
||||||
else
|
else
|
||||||
os = {}
|
os = {}
|
||||||
options = OptionParser.new
|
options = OptionParser.new
|
||||||
options.banner = "#{full_cmd.join ' '}"
|
options.banner = "#{full_cmd.join ' '}"
|
||||||
@arguments.each do |(aname, aas, aos, aexe)|
|
@options.each do |(aname, aas, aos, aexe)|
|
||||||
os[aname] = aos[aname] if aos.has_key? :default
|
os[aname] = aos[aname] if aos.has_key? :default
|
||||||
options.on( *aas) {|val| os[aname] = aexe[val] }
|
options.on( *aas) {|val| os[aname] = aexe[val] }
|
||||||
end
|
end
|
||||||
as = options.parse! as
|
as = options.parse! as
|
||||||
|
if @exe.lambda?
|
||||||
|
pars = required
|
||||||
|
if as.length < pars.length
|
||||||
|
raise DenCli::UsageError, "Missing parameter(s): #{pars[as.length..-1].join " "}"
|
||||||
|
end
|
||||||
|
if parameters.select{|e|:rest == e[0]}.empty?
|
||||||
|
pars = pars + additional
|
||||||
|
if as.length > pars.length
|
||||||
|
raise DenCli::UsageError, "Unused parameter(s): #{as[-pars.length..-1].shelljoin}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@exe.call *as, **os
|
@exe.call *as, **os
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def help() "#{parent.full_cmd.join ' '} #{name}\n#{description}" end
|
|
||||||
|
def usage
|
||||||
|
"#{parent.full_cmd.join ' '} #{name} "+
|
||||||
|
@options.map{|(_,(o,_,_,_),_)|"[#{o}] "}.join( '')+
|
||||||
|
(@exe.lambda? ? (
|
||||||
|
required.join( " ")+
|
||||||
|
(additional.empty? ? "" : " [#{additional.join " "}]")
|
||||||
|
) : '...')
|
||||||
|
end
|
||||||
|
|
||||||
|
def help
|
||||||
|
"#{usage}\n#{description}"
|
||||||
|
end
|
||||||
|
|
||||||
def complete( *pre, str) @completion.call *pre, str end
|
def complete( *pre, str) @completion.call *pre, str end
|
||||||
|
|
||||||
|
@ -36,8 +65,8 @@ class DenCli::CMD
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def arg name, *as, **os, &exe
|
def opt name, *as, **os, &exe
|
||||||
@arguments.push [name.to_s.to_sym, as, os, exe || lambda{|*a|a} ]
|
@options.push [name.to_s.to_sym, as, os, exe || lambda{|val|val} ]
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,16 @@ class DenCli::Sub
|
||||||
def full_cmd() _full_cmd [] end
|
def full_cmd() _full_cmd [] end
|
||||||
def []( k) @aliases[k] end
|
def []( k) @aliases[k] end
|
||||||
|
|
||||||
|
def usage
|
||||||
|
"#{full_cmd.join ' '} ..."
|
||||||
|
end
|
||||||
|
|
||||||
def help n = nil, *a
|
def help n = nil, *a
|
||||||
if n.nil?
|
if n.nil?
|
||||||
r = "#{full_cmd.join ' '}: #{description}\n\n"
|
r = "#{full_cmd.join ' '}: #{description}\n\n"
|
||||||
m = @subs.map {|k,_| k.length }.max
|
m = @subs.map {|k,c| k.nil? ? 0 : c.usage.length }.max
|
||||||
@subs.each do |k, c|
|
@subs.each do |k, c|
|
||||||
r += " % -#{m}s %s\n" % [k, c.description] unless k.nil?
|
r += " % -#{m}s %s\n" % [c.usage, c.description] unless k.nil?
|
||||||
end
|
end
|
||||||
r
|
r
|
||||||
elsif @aliases.has_key? n
|
elsif @aliases.has_key? n
|
||||||
|
|
Loading…
Reference in a new issue