From 7be062c1dbf37b024721560a7eb3cd675618f43c Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 9 Dec 2021 13:28:46 +0100 Subject: [PATCH] min: false for no shorts. noshortaliases only for Sub-commands --- lib/dencli.rb | 9 +++++++-- lib/dencli/cmd.rb | 4 ++-- lib/dencli/sub.rb | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/dencli.rb b/lib/dencli.rb index a4970d6..45a233e 100755 --- a/lib/dencli.rb +++ b/lib/dencli.rb @@ -51,11 +51,16 @@ class DenCli # `g(:abc)` => `["a", "ab", "abc"]` # `g(:abcdef, 4)` => `["abcd", "abcde", "abcdef"]` 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? r.each {|i| yield cmd[0..i] } + yield cmd else - r.map {|i| cmd[0..i] } + r.map {|i| cmd[0..i] } + [cmd] end end alias g gen_aliases diff --git a/lib/dencli/cmd.rb b/lib/dencli/cmd.rb index cb24e9a..f057b2d 100644 --- a/lib/dencli/cmd.rb +++ b/lib/dencli/cmd.rb @@ -4,9 +4,9 @@ require_relative '../dencli' class DenCli::CMD 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 - @parent, @name, @description, @exe, @noshortaliases = parent, name, description, lambda( &exe), noshortaliases + @parent, @name, @description, @exe = parent, name, description, lambda( &exe) @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] } diff --git a/lib/dencli/sub.rb b/lib/dencli/sub.rb index 03dc0de..7b71093 100644 --- a/lib/dencli/sub.rb +++ b/lib/dencli/sub.rb @@ -140,8 +140,8 @@ class DenCli::Sub block_given? ? yield( r) : r end - def cmd name, description, min: nil, aliases: nil, noshortaliases: nil, &exe - _add name, min, DenCli::CMD.new( self, name, description, exe, noshortaliases: noshortaliases), aliases + def cmd name, description, min: nil, aliases: nil, &exe + _add name, min, DenCli::CMD.new( self, name, description, exe), aliases end def complete *pre, str