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
|
||||
|
||||
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( :'arg', "", &lambda {|one| $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|
|
||||
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( :foo, "BAR") { $stderr.puts "FOO bar"}
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ require_relative '../dencli'
|
|||
class DenCli::CMD
|
||||
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
|
||||
@parent, @name, @description, @exe = parent, name, description, lambda( &exe)
|
||||
@parent, @name, @description, @exe, @noshortaliases = parent, name, description, lambda( &exe), noshortaliases
|
||||
@parameters = @exe.parameters
|
||||
@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] }
|
||||
|
|
|
@ -3,11 +3,12 @@ require_relative '../dencli'
|
|||
class DenCli::Sub
|
||||
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__, :parent, parent, DenCli, DenCli::Sub
|
||||
#DenCli::assert_type self, __method__, :description, description, String
|
||||
@parent, @name, @description, @subs, @aliases = parent, name, "-> #{description}", {}, {}
|
||||
@noshortaliases = ! ! noshortaliases
|
||||
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
|
||||
name = name.to_s unless name.nil?
|
||||
@subs[name] = obj
|
||||
if @noshortaliases
|
||||
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
|
||||
[*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
|
||||
obj
|
||||
end
|
||||
private :_add
|
||||
|
||||
def sub name, description, min: nil, aliases: nil, &exe
|
||||
r = _add name, min, DenCli::Sub.new( self, name, description), aliases
|
||||
def sub name, description, min: nil, aliases: nil, noshortaliases: nil, &exe
|
||||
r = _add name, min, DenCli::Sub.new( self, name, description, noshortaliases: noshortaliases), aliases
|
||||
block_given? ? yield( r) : r
|
||||
end
|
||||
|
||||
def cmd name, description, min: nil, aliases: nil, &exe
|
||||
_add name, min, DenCli::CMD.new( self, name, description, exe), aliases
|
||||
def cmd name, description, min: nil, aliases: nil, noshortaliases: nil, &exe
|
||||
_add name, min, DenCli::CMD.new( self, name, description, exe, noshortaliases: noshortaliases), aliases
|
||||
end
|
||||
|
||||
def complete *pre, str
|
||||
|
|
Loading…
Reference in a new issue