zsh:funcs:mkcd: Improve argument parsing

Do not switch into a directory, if multiple were created. Support flags
given at the end of the commandline (e.g. `mkdir foo/bar -p`)
This commit is contained in:
2022-07-13 17:24:29 +02:00
parent d4ee271740
commit ee1ec50322

View File

@@ -20,18 +20,28 @@ bwpwd() {
fi
}
# creates directory and changes into it
# mkdir wrapper that changes into the created directory if only one was given
mkcd () {
# Create directory
mkdir "$@"
# shift arguments if mkdir options were used
while [[ $# -gt 1 ]]; do
mkdir "$@" || return
# Remove flags and their arguments
nargs="$#"
for ((i = 0; i < nargs; i++)); do
if [[ ${1[1]} = '-' ]]; then
# When `-m` is given, shift it's MODE argument as well
! [[ $1 =~ ^-([^-].*)?m$ ]] || { shift; i+=1 }
shift
else
set -- "${@:2}" "$1"
fi
done
if [[ -d "$1" ]]; then
# cd into the created directory if only one was specified
[[ $# -eq 1 && -d $1 ]] || return 0
cd "$1"
pwd
fi
}
# Encode and decode qr-codes