mirror of
https://github.com/notwa/rc
synced 2024-06-26 01:07:11 -07:00
support hyphens in command names passed to has
This commit is contained in:
parent
fdcae6182f
commit
e522d065bf
|
@ -5,12 +5,16 @@
|
||||||
# {{{1 utilities
|
# {{{1 utilities
|
||||||
|
|
||||||
has() ( # hardcoded here for convenience. refer to ~/sh/has for documentation.
|
has() ( # hardcoded here for convenience. refer to ~/sh/has for documentation.
|
||||||
if [ "${1:--}" = "${1#*[!A-Za-z_]}" ]; then
|
if [ "${1:-/}" = "${1#*[!A-Za-z_-]}" ]; then
|
||||||
|
if [ "$FANCY" = 0 ] || [ "${1:-/}" = "${1#-}" ]; then
|
||||||
alias "$1"=
|
alias "$1"=
|
||||||
unalias "$1"
|
unalias "$1"
|
||||||
|
fi
|
||||||
|
if [ "$FANCY" = 1 ] || [ "${1:-/}" = "${1#*-}" ]; then
|
||||||
eval "$1() (:)"
|
eval "$1() (:)"
|
||||||
unset -f "$1"
|
unset -f "$1"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
which -- "$1" >/dev/null 2>&1 && which -- "$1"
|
which -- "$1" >/dev/null 2>&1 && which -- "$1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
14
sh/has
14
sh/has
|
@ -8,14 +8,22 @@ has() ( ### @- print the result of `which` if the program is found, else simply
|
||||||
### ```
|
### ```
|
||||||
### export SOLVER="$(has kissat || has picosat || has cadical || has minisat)"
|
### export SOLVER="$(has kissat || has picosat || has cadical || has minisat)"
|
||||||
### ```
|
### ```
|
||||||
if [ "${1:--}" = "${1#*[!A-Za-z_]}" ]; then # valid alias name?
|
fancy=0
|
||||||
# forcefully unalias it (regardless of if it even was an alias)
|
[ -z "$ZSH_VERSION" ] && [ -z "$BASH_VERSION" ] || fancy=1
|
||||||
|
if [ "${1:-/}" = "${1#*[!A-Za-z_-]}" ]; then # has a simple name?
|
||||||
|
if [ $fancy = 0 ] || [ "${1:-/}" = "${1#-}" ]; then
|
||||||
|
# only ash and dash support aliases beginning with hyphens.
|
||||||
|
# forcefully unalias it (regardless of if it even was an alias):
|
||||||
alias "$1"=
|
alias "$1"=
|
||||||
unalias "$1"
|
unalias "$1"
|
||||||
# forcefully unset it (regardless of if it even was a function)
|
fi
|
||||||
|
if [ $fancy = 1 ] || [ "${1:-/}" = "${1#*-}" ]; then
|
||||||
|
# only bash and zsh support function names containing hyphens.
|
||||||
|
# forcefully unset it (regardless of if it even was a function):
|
||||||
eval "$1() (:)"
|
eval "$1() (:)"
|
||||||
unset -f "$1"
|
unset -f "$1"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
which -- "$1" >/dev/null 2>&1 && which -- "$1"
|
which -- "$1" >/dev/null 2>&1 && which -- "$1"
|
||||||
) # this uses parentheses instead of braces so that the function is always run
|
) # this uses parentheses instead of braces so that the function is always run
|
||||||
# in a subshell. otherwise, the user's aliases and functions may be overwritten.
|
# in a subshell. otherwise, the user's aliases and functions may be overwritten.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user