From d4aec2c73cbf130f8c7ef174340eb959ca52fd7f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 20 Feb 2025 12:25:43 -0800 Subject: cdalias: take zero, one or two args. * cdlog.sh (cdlog.alias): Behave like cdlog.aliases in the zero argument case, and in the one argument case list the specified alias, if it exists. * README.md: Updated. The cdaliases command becomes deprecated. --- README.md | 14 +++++++++++--- cdlog.sh | 23 ++++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4857d22..b590d61 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,7 @@ current again that new new session that `cdlog` had allocated on startup. That new session does not appear in the numbered list of recoverable sessions unless it has been already persisted by the execution of a directory-changing command. -* The `cdalias` command is used for defining "cd aliases"; see below. - -* The `cdaliases` command lists cd aliases. +* The `cdalias` command is used for defining or listing "cd aliases"; see below. * The `cdunalias` command removes a cd alias. @@ -107,6 +105,16 @@ If the replacement begins with `@` then it is a search. If the search succeeds, then the result will be used as the alias value. +When `cdalias` is invoked with no arguments, it lists all the +currently defined aliases. + +When `cdalias` is invoked with one argument, and that argument +names an alias, it lists that alias. Otherwise it fails +with an error message. + +The deprecated `cdaliases` command also lists aliases; it +will disappear in a future version of `cdlog`. + ## Searches A search is an arguments which begins with `@`. The rest diff --git a/cdlog.sh b/cdlog.sh index fc8568f..feebfda 100644 --- a/cdlog.sh +++ b/cdlog.sh @@ -437,10 +437,27 @@ cdlog.alias() { local def= - if [ $# -ne 2 ] ; then - printf "cdlog.alias: two arguments required\n" + case $# in + ( 0 ) + cdlog.aliases + return $? + ;; + ( 1 ) + if [ ${cdlog_alias[$1]+y} ] ; then + printf "%s -> %s\n" "$1" "${cdlog_alias[$1]}" + return 0 + else + printf "cdlog.alias; %s: not defined\n" "$1" + return 1 + fi + ;; + ( 2 ) + ;; + ( * ) + printf "cdlog.alias: one or two arguments required\n" return 1 - fi + ;; + esac if [ -z "$1" ] ; then printf "cdlog.alias: alias may not be empty\n" -- cgit v1.2.3