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 fi
} }
# creates directory and changes into it # mkdir wrapper that changes into the created directory if only one was given
mkcd () { mkcd () {
# Create directory # Create directory
mkdir "$@" mkdir "$@" || return
# shift arguments if mkdir options were used
while [[ $# -gt 1 ]]; do # Remove flags and their arguments
shift 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 done
if [[ -d "$1" ]]; then
cd "$1" # cd into the created directory if only one was specified
pwd [[ $# -eq 1 && -d $1 ]] || return 0
fi cd "$1"
pwd
} }
# Encode and decode qr-codes # Encode and decode qr-codes