Enable flag combining in worktree subcommands

This commit is contained in:
2026-11-10 12:40:53 +00:00
parent 150f50c9fa
commit a2e95fddb7
2 changed files with 45 additions and 14 deletions

View File

@@ -68,12 +68,12 @@ _worktree() {
_arguments '1:: :__worktree_all_branches' _arguments '1:: :__worktree_all_branches'
;; ;;
(find) (find)
_arguments \ _arguments -s -S \
'(-i --ignore-case)'{-i,--ignore-case}'[Match the glob case insensitively]' \ '(-i --ignore-case)'{-i,--ignore-case}'[Match the glob case insensitively]' \
'1:: :_files' '1:: :_files'
;; ;;
(rm|remove) (rm|remove)
_arguments \ _arguments -s -S \
'(-f --force)'{-f,--force}'[Force removal even with modified or untracked files]' \ '(-f --force)'{-f,--force}'[Force removal even with modified or untracked files]' \
'(-d --delete-branch)'{-d,--delete-branch}'[Also delete the branch]' \ '(-d --delete-branch)'{-d,--delete-branch}'[Also delete the branch]' \
'*: :__worktree_active_branches' '*: :__worktree_active_branches'

View File

@@ -54,6 +54,27 @@ __worktree_labels() {
done done
} }
# Sort arguments into the caller's flags and operands arrays. Combined single
# character flags are split, e.g. -fd into -f -d. Flags may appear anywhere
# before a --, after which every argument is an operand.
__worktree_parse_args() {
local arg i
while (( $# )); do
arg=$1
shift
case "$arg" in
--) operands+=("$@"); return ;;
--?*) flags+=("$arg") ;;
-?*)
for (( i = 2; i <= ${#arg}; i++ )); do
flags+=("-${arg[i]}")
done
;;
*) operands+=("$arg") ;;
esac
done
}
__worktree_find_in() { __worktree_find_in() {
local dir=$1 pat=$2 icase=$3 local dir=$1 pat=$2 icase=$3
if (( $+commands[fd] )); then if (( $+commands[fd] )); then
@@ -66,9 +87,9 @@ __worktree_find_in() {
fd_args+=(--case-sensitive) fd_args+=(--case-sensitive)
fi fi
if [[ "$pat" == */* ]]; then if [[ "$pat" == */* ]]; then
fd "${fd_args[@]}" --full-path "**/$pat" fd "${fd_args[@]}" --full-path -- "**/$pat"
else else
fd "${fd_args[@]}" "$pat" fd "${fd_args[@]}" -- "$pat"
fi fi
else else
local ic= local ic=
@@ -177,14 +198,19 @@ which must be merged unless -f is given."
find) find)
local icase=0 local icase=0
local -a globs local -a flags operands
local arg __worktree_parse_args "${@:2}"
for arg in "${@:2}"; do local flag
case "$arg" in for flag in "${flags[@]}"; do
case "$flag" in
-i|--ignore-case) icase=1 ;; -i|--ignore-case) icase=1 ;;
*) globs+=("$arg") ;; *)
print -P "%F{red}error:%f unknown option: $flag"
return 1
;;
esac esac
done done
local -a globs=("${operands[@]}")
if (( ${#globs} == 0 )); then if (( ${#globs} == 0 )); then
print -P "%F{red}error:%f missing <glob> argument" print -P "%F{red}error:%f missing <glob> argument"
return 1 return 1
@@ -291,15 +317,20 @@ which must be merged unless -f is given."
rm|remove) rm|remove)
local force=0 local force=0
local delete_branch=0 local delete_branch=0
local branches=() local -a flags operands
local arg __worktree_parse_args "${@:2}"
for arg in "${@:2}"; do local flag
case "$arg" in for flag in "${flags[@]}"; do
case "$flag" in
-f|--force) force=1 ;; -f|--force) force=1 ;;
-d|--delete-branch) delete_branch=1 ;; -d|--delete-branch) delete_branch=1 ;;
*) branches+=("$arg") ;; *)
print -P "%F{red}error:%f unknown option: $flag"
return 1
;;
esac esac
done done
local branches=("${operands[@]}")
if (( ${#branches[@]} == 0 )); then if (( ${#branches[@]} == 0 )); then
print -P "%F{red}error:%f missing <branch> argument" print -P "%F{red}error:%f missing <branch> argument"
return 1 return 1