min: false for no shorts. noshortaliases only for Sub-commands

This commit is contained in:
Denis Knauf 2021-12-09 13:28:46 +01:00
parent 2c654f2941
commit 7be062c1db
3 changed files with 11 additions and 6 deletions

View file

@ -51,11 +51,16 @@ class DenCli
# `g(:abc)` => `["a", "ab", "abc"]` # `g(:abc)` => `["a", "ab", "abc"]`
# `g(:abcdef, 4)` => `["abcd", "abcde", "abcdef"]` # `g(:abcdef, 4)` => `["abcd", "abcde", "abcdef"]`
def gen_aliases cmd, min = nil def gen_aliases cmd, min = nil
r = ((min||1)-1).upto cmd.length-1 case min
when false then min = cmd.length
when nil then min = 1
end
r = ([min, 1].max - 1).upto cmd.length-2
if block_given? if block_given?
r.each {|i| yield cmd[0..i] } r.each {|i| yield cmd[0..i] }
yield cmd
else else
r.map {|i| cmd[0..i] } r.map {|i| cmd[0..i] } + [cmd]
end end
end end
alias g gen_aliases alias g gen_aliases

View file

@ -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, noshortaliases: nil 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, @noshortaliases = parent, name, description, lambda( &exe), noshortaliases @parent, @name, @description, @exe = parent, name, description, lambda( &exe)
@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] }

View file

@ -140,8 +140,8 @@ class DenCli::Sub
block_given? ? yield( r) : r block_given? ? yield( r) : r
end end
def cmd name, description, min: nil, aliases: nil, noshortaliases: nil, &exe def cmd name, description, min: nil, aliases: nil, &exe
_add name, min, DenCli::CMD.new( self, name, description, exe, noshortaliases: noshortaliases), aliases _add name, min, DenCli::CMD.new( self, name, description, exe), aliases
end end
def complete *pre, str def complete *pre, str