mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-04-03 04:20:01 +02:00
lib: add safe_alias function
and ALLOW_OVERRIDE_ALIASES option
This commit is contained in:
parent
e44aa50301
commit
59fa1eacc1
2 changed files with 31 additions and 0 deletions
|
|
@ -62,6 +62,32 @@ function try_alias_value() {
|
|||
alias_value "$1" || echo "$1"
|
||||
}
|
||||
|
||||
#
|
||||
# Alias only if not already a command/function/alias
|
||||
# Use option ALLOW_OVERRIDE_ALIASES to force overriding
|
||||
#
|
||||
# Arguments:
|
||||
# 1. my_alias='my alias value'
|
||||
# Same argument you would pass to the "alias" built-in
|
||||
# Return value:
|
||||
# 0 if the alias was safely created, 73 otherwise
|
||||
#
|
||||
function safe_alias() {
|
||||
emulate -L zsh
|
||||
local cmd lhs rhs
|
||||
cmd=(${(s:=:)1})
|
||||
lhs=$cmd[1]
|
||||
rhs=$cmd[2,-1]
|
||||
if (( $+commands[$lhs] || $+functions[$lhs] || $+aliases[$lhs] )); then
|
||||
if [[ $ALLOW_OVERRIDE_ALIASES = "true" ]]; then
|
||||
>&2 echo "WARN: override \"$lhs\" with alias \"$rhs\""
|
||||
else
|
||||
return 73 # os.EX_CANTCREAT
|
||||
fi
|
||||
fi
|
||||
alias $lhs="$rhs"
|
||||
}
|
||||
|
||||
#
|
||||
# Set variable "$1" to default value "$2" if "$1" is not yet defined.
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue