[zsh] Make prepend_to_path and append_to_path more interesting

Add a +e/-e argument (parsed with getopt) that conditionally exports $path.
This commit is contained in:
Eryn Wells 2023-03-28 23:24:29 +00:00
parent 6f1d8b4ecb
commit 5d74548f21
2 changed files with 46 additions and 14 deletions

View file

@ -1,13 +1,29 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
# vim:ft=zsh:
function append_to_path
{
if [[ -d "$1" ]]; then
path+="$1"
append_to_path() {
local should_export_path=1
while getopts "e" opt; do
case $opt in
"e") should_export_path=1;;
"+e") should_export_path=0;;
*) >&2 echo "$0: unknown option '$opt'"
esac
done
local path_to_add=$@[$OPTIND]
if [[ -d "$path_to_add" ]]; then
path+="$path_to_add"
if (( $should_export_path )); then
export path
fi
return 1
fi
return 0
}
append_to_path "$@"

View file

@ -1,13 +1,29 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
# vim:ft=zsh:
function prepend_to_path
{
if [[ -d "$1" ]]; then
path=("$1" $path)
prepend_to_path() {
local should_export_path=1
while getopts "e" opt; do
case $opt in
"e") should_export_path=1;;
"+e") should_export_path=0;;
*) >&2 echo "$0: unknown option '$opt'"
esac
done
local path_to_add=$@[$OPTIND]
if [[ -d "$path_to_add" ]]; then
path=("$path_to_add" $path)
if (( $should_export_path )); then
export path
fi
return 0
fi
return 1
}
prepend_to_path "$@"