defined_in for determine already defined commands/aliases.
This commit is contained in:
parent
8fe78bb406
commit
09b467f7bd
|
@ -2,11 +2,11 @@ require_relative '../dencli'
|
||||||
|
|
||||||
|
|
||||||
class DenCli::CMD
|
class DenCli::CMD
|
||||||
attr_reader :parent, :name, :description, :exe, :completion, :options
|
attr_reader :parent, :name, :description, :exe, :completion, :options, :defined_in
|
||||||
|
|
||||||
def initialize parent, name, description, exe
|
def initialize parent, name, description, exe, defined_in
|
||||||
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, lambda( &exe)
|
@parent, @name, @description, @exe, @defined_in = parent, name, description, lambda( &exe), defined_in
|
||||||
@parameters = @exe.parameters
|
@parameters = @exe.parameters
|
||||||
@arguments_required = @exe.parameters.select {|e| :req == e[0] }.map {|e| e[1] }
|
@arguments_required = @exe.parameters.select {|e| :req == e[0] }.map {|e| e[1] }
|
||||||
@arguments_additional = @exe.parameters.select {|e| :opt == e[0] }.map {|e| e[1] }
|
@arguments_additional = @exe.parameters.select {|e| :opt == e[0] }.map {|e| e[1] }
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
require_relative '../dencli'
|
require_relative '../dencli'
|
||||||
|
|
||||||
class DenCli::Sub
|
class DenCli::Sub
|
||||||
attr_reader :parent, :name, :description, :subs, :aliases
|
attr_reader :parent, :name, :description, :subs, :aliases, :defined_in
|
||||||
|
|
||||||
def initialize parent, name, description, noshortaliases: nil
|
def initialize parent, name, description, noshortaliases: nil, defined_in: nil
|
||||||
#DenCli::assert_type self, __method__, :name, name, Symbol
|
#DenCli::assert_type self, __method__, :name, name, Symbol
|
||||||
#DenCli::assert_type self, __method__, :parent, parent, DenCli, DenCli::Sub
|
#DenCli::assert_type self, __method__, :parent, parent, DenCli, DenCli::Sub
|
||||||
#DenCli::assert_type self, __method__, :description, description, String
|
#DenCli::assert_type self, __method__, :description, description, String
|
||||||
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
||||||
@noshortaliases = ! ! noshortaliases
|
@noshortaliases, @defined_in = ! ! noshortaliases, defined_in || Kernel.caller
|
||||||
end
|
end
|
||||||
|
|
||||||
def _full_cmd( post) parent._full_cmd [@name]+post end
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
||||||
|
@ -120,17 +120,17 @@ class DenCli::Sub
|
||||||
name = name.to_s unless name.nil?
|
name = name.to_s unless name.nil?
|
||||||
@subs[name] = obj
|
@subs[name] = obj
|
||||||
if @noshortaliases
|
if @noshortaliases
|
||||||
warn "Command/Alias for [#{obj.full_cmd}] already exists: #{full_cmd.join ' '} #{name}. Used by: #{@aliases[name].full_cmd}" if @aliases.has_key? name
|
warn "Command/Alias for #{obj.full_cmd} defined in #{obj.defined_in} already exists: #{full_cmd.join ' '} #{name}. Used by #{@aliases[name].full_cmd} defined in #{@aliases[name].defined_in}" if @aliases.has_key? name
|
||||||
@aliases[name] = obj
|
@aliases[name] = obj
|
||||||
else
|
else
|
||||||
DenCli.gen_aliases name, min do |a|
|
DenCli.gen_aliases name, min do |a|
|
||||||
warn "Command/Alias for [#{obj.full_cmd}] already exists: #{full_cmd.join ' '} #{a}. Used by: #{@aliases[a].full_cmd}" if @aliases.has_key? a
|
warn "Command/Alias for #{obj.full_cmd} defined in #{obj.defined_in} already exists: #{full_cmd.join ' '} #{a}. Used by #{@aliases[a].full_cmd} defined in #{@aliases[a].defined_in}" if @aliases.has_key? a
|
||||||
@aliases[a] ||= obj
|
@aliases[a] ||= obj
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if aliases
|
if aliases
|
||||||
[*aliases].each do |a|
|
[*aliases].each do |a|
|
||||||
raise ArgumentError, "Alias for [#{obj.full_cmd}] already exists: #{full_cmd.join ' '} #{a}. Used by: #{@aliases[a].full_cmd}" if @aliases.has_key? a
|
raise ArgumentError, "Alias for #{obj.full_cmd} defined in #{obj.defined_in} already exists: #{full_cmd.join ' '} #{a}. Used by #{@aliases[a].full_cmd} defined in #{@aliases[a].defined_in}" if @aliases.has_key? a
|
||||||
@aliases[a] = obj
|
@aliases[a] = obj
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -138,13 +138,13 @@ class DenCli::Sub
|
||||||
end
|
end
|
||||||
private :_add
|
private :_add
|
||||||
|
|
||||||
def sub name, description, min: nil, aliases: nil, noshortaliases: nil, &exe
|
def sub name, description, min: nil, aliases: nil, noshortaliases: nil, defined_in: nil, &exe
|
||||||
r = _add name, min, DenCli::Sub.new( self, name, description, noshortaliases: noshortaliases), aliases
|
r = _add name, min, DenCli::Sub.new( self, name, description, noshortaliases: noshortaliases, defined_in: defined_in || Kernel.caller.first), aliases
|
||||||
block_given? ? yield( r) : r
|
block_given? ? yield( r) : r
|
||||||
end
|
end
|
||||||
|
|
||||||
def cmd name, description, min: nil, aliases: nil, &exe
|
def cmd name, description, min: nil, aliases: nil, defined_in: nil, &exe
|
||||||
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
|
_add name, min, DenCli::CMD.new( self, name, description, exe, defined_in || Kernel.caller.first), aliases
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete *pre, str
|
def complete *pre, str
|
||||||
|
|
Loading…
Reference in a new issue