noshortaliases. warn or raise if alias already exists
This commit is contained in:
parent
6254349aa9
commit
2c654f2941
|
@ -90,7 +90,7 @@ class Capture
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.sub :tests, "Some tests" do |tcli|
|
cli.sub :tests, "Some tests", noshortaliases: true do |tcli|
|
||||||
tcli.cmd( :'-', "No arguments no options expected", &lambda {|| $capture.args = [] })
|
tcli.cmd( :'-', "No arguments no options expected", &lambda {|| $capture.args = [] })
|
||||||
tcli.cmd( :'arg', "", &lambda {|one| $capture.args = [one] })
|
tcli.cmd( :'arg', "", &lambda {|one| $capture.args = [one] })
|
||||||
tcli.cmd( :'oar', "", &lambda {|one=nil| $capture.args = [one] })
|
tcli.cmd( :'oar', "", &lambda {|one=nil| $capture.args = [one] })
|
||||||
|
@ -208,7 +208,6 @@ cli.cmd( :help, "An example for help", aliases: [nil, '-h', '--help'], &lambda {
|
||||||
|
|
||||||
cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
|
cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
|
||||||
sub.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| $stderr.puts sub.help(*args) }
|
sub.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| $stderr.puts sub.help(*args) }
|
||||||
sub.cmd( :help, "") {|*args| $stderr.puts cli.help( 'more', *args) }
|
|
||||||
sub.cmd( :example, "Here is an example, too") { $stderr.puts "This is an other example" }
|
sub.cmd( :example, "Here is an example, too") { $stderr.puts "This is an other example" }
|
||||||
sub.cmd( :foo, "BAR") { $stderr.puts "FOO bar"}
|
sub.cmd( :foo, "BAR") { $stderr.puts "FOO bar"}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ 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
|
||||||
|
|
||||||
def initialize parent, name, description, exe
|
def initialize parent, name, description, exe, noshortaliases: nil
|
||||||
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, @noshortaliases = parent, name, description, lambda( &exe), noshortaliases
|
||||||
@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] }
|
||||||
|
|
|
@ -3,11 +3,12 @@ require_relative '../dencli'
|
||||||
class DenCli::Sub
|
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, noshortaliases: 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
|
||||||
end
|
end
|
||||||
|
|
||||||
def _full_cmd( post) parent._full_cmd [@name]+post end
|
def _full_cmd( post) parent._full_cmd [@name]+post end
|
||||||
|
@ -118,21 +119,29 @@ class DenCli::Sub
|
||||||
#DenCli::assert_type self, __method__, :aliases, aliases, Array, NilClass
|
#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
|
||||||
|
if @noshortaliases
|
||||||
DenCli.gen_aliases( name, min) {|a| @aliases[a] ||= obj }
|
DenCli.gen_aliases( name, min) {|a| @aliases[a] ||= obj }
|
||||||
|
else
|
||||||
|
warn "Alias already exists: #{full_cmd.join ' '} #{name}" if @aliases.has_key? name
|
||||||
|
@aliases[name] = obj
|
||||||
|
end
|
||||||
if aliases
|
if aliases
|
||||||
[*aliases].each {|a| @aliases[a] = obj }
|
[*aliases].each do |a|
|
||||||
|
raise ArgumentError, "Alias already exists: #{full_cmd.join ' '} #{a}" if @aliases.has_key? a
|
||||||
|
@aliases[a] = obj
|
||||||
|
end
|
||||||
end
|
end
|
||||||
obj
|
obj
|
||||||
end
|
end
|
||||||
private :_add
|
private :_add
|
||||||
|
|
||||||
def sub name, description, min: nil, aliases: nil, &exe
|
def sub name, description, min: nil, aliases: nil, noshortaliases: nil, &exe
|
||||||
r = _add name, min, DenCli::Sub.new( self, name, description), aliases
|
r = _add name, min, DenCli::Sub.new( self, name, description, noshortaliases: noshortaliases), 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, noshortaliases: nil, &exe
|
||||||
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
|
_add name, min, DenCli::CMD.new( self, name, description, exe, noshortaliases: noshortaliases), aliases
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete *pre, str
|
def complete *pre, str
|
||||||
|
|
Loading…
Reference in a new issue