Skip to content

Commit 9386436

Browse files
committed
feat(arguments): support long options
1 parent 1c7e4cb commit 9386436

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

abbr

+29-15
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ zle -N abbr_expand _zsh_abbr_expand_widget
132132

133133
function abbr() {
134134
{
135+
local abbr_number_opts=0
135136
local abbr_opt_add=false
136137
local abbr_opt_create_aliases=false
137138
local abbr_opt_erase=false
@@ -341,7 +342,7 @@ function abbr() {
341342
}
342343

343344
function abbr_error() {
344-
printf "abbr%s\\nFor help run abbr -h\\n" "$@"
345+
printf "abbr%s\\nFor help run abbr --help\\n" "$@"
345346
abbr_should_exit=true
346347
}
347348

@@ -472,57 +473,70 @@ function abbr() {
472473
print "$abbr_usage\\n"
473474
}
474475

475-
while getopts ":haerslpicgU" opt; do
476+
local abbr_number_opts=0
477+
for opt in "$@"; do
476478
if $abbr_should_exit; then
477479
abbr_should_exit=false
478480
return
479481
fi
480482

481483
case "$opt" in
482-
h)
484+
"-h"|"--help")
483485
abbr_usage
484486
abbr_should_exit=true
485487
;;
486-
a)
488+
"-a"|"--add")
487489
abbr_check_options $abbr_opt_rename $abbr_opt_show $abbr_opt_list $abbr_opt_erase $abbr_opt_populate $abbr_opt_git_populate $abbr_opt_create_aliases
490+
((abbr_number_opts++))
488491
abbr_opt_add=true
489492
;;
490-
e)
493+
"-e"|"--erase")
491494
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_show $abbr_opt_list $abbr_opt_populate $abbr_opt_git_populate $abbr_opt_create_aliases
495+
((abbr_number_opts++))
492496
abbr_opt_erase=true
493497
;;
494-
r)
498+
"-r"|"--rename")
495499
abbr_check_options $abbr_opt_add $abbr_opt_show $abbr_opt_list $abbr_opt_erase $abbr_opt_populate $abbr_opt_git_populate $abbr_opt_create_aliases
500+
((abbr_number_opts++))
496501
abbr_opt_rename=true
497502
;;
498-
s)
503+
"-s"|"--show")
499504
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_list $abbr_opt_erase $abbr_opt_populate $abbr_opt_git_populate $abbr_opt_create_aliases
505+
((abbr_number_opts++))
500506
abbr_opt_show=true
501507
;;
502-
l)
508+
"-l"|"--list")
503509
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_show $abbr_opt_erase $abbr_opt_populate $abbr_opt_git_populate $abbr_opt_create_aliases
510+
((abbr_number_opts++))
504511
abbr_opt_list=true
505512
;;
506-
p)
513+
"-p"|"--populate")
507514
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_show $abbr_opt_erase $abbr_opt_list $abbr_opt_git_populate $abbr_opt_create_aliases
515+
((abbr_number_opts++))
508516
abbr_opt_populate=true
509517
;;
510-
i)
518+
"-i"|"--git-populate")
511519
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_show $abbr_opt_erase $abbr_opt_list $abbr_opt_populate $abbr_opt_create_aliases
520+
((abbr_number_opts++))
512521
abbr_opt_git_populate=true
513522
;;
514-
c)
523+
"-c"|"--create-aliases")
515524
abbr_check_options $abbr_opt_add $abbr_opt_rename $abbr_opt_show $abbr_opt_erase $abbr_opt_list $abbr_opt_populate $abbr_opt_git_populate
525+
((abbr_number_opts++))
516526
abbr_opt_create_aliases=true
517527
;;
518-
g)
528+
"-g"|"--global")
519529
abbr_check_options $abbr_opt_universal
530+
((abbr_number_opts++))
520531
abbr_opt_global=true
521532
;;
522-
U)
533+
"-U"|"--universal")
523534
abbr_check_options $abbr_opt_global
535+
((abbr_number_opts++))
536+
;;
537+
"-"*)
538+
abbr_error ": Unknown option '$opt'"
524539
;;
525-
*) abbr_error " Unknown option '-$OPTARG'" ;;
526540
esac
527541
done
528542

@@ -531,7 +545,7 @@ function abbr() {
531545
return
532546
fi
533547

534-
shift $((OPTIND-1))
548+
shift $abbr_number_opts
535549

536550
if ! $abbr_opt_global; then
537551
abbr_opt_universal=true

0 commit comments

Comments
 (0)