Skip to content

Commit ac21a97

Browse files
author
Damien Nadé
committed
argsparse-completion.sh: prototype for automatic completion for argsparse-based scripts
1 parent 2832a1f commit ac21a97

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

argsparse-completion.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
# -*- tab-width: 4; encoding: utf-8; -*-
3+
#
4+
## @file
5+
## @author Damien Nadé <bash-argsparse@livna.org>
6+
## @brief ...
7+
## @copyright WTFPLv2
8+
## @version 1.7
9+
#
10+
#########
11+
# License:
12+
#
13+
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
14+
# Version 2, December 2004
15+
#
16+
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
17+
#
18+
# Everyone is permitted to copy and distribute verbatim or modified
19+
# copies of this license document, and changing it is allowed as long
20+
# as the name is changed.
21+
#
22+
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
23+
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
24+
#
25+
# 0. You just DO WHAT THE FUCK YOU WANT TO.
26+
#
27+
#########
28+
#
29+
30+
__argsparse_complete() {
31+
local script=$1
32+
(
33+
set +o posix
34+
ARGSPARSE_COMPLETION_MODE=1
35+
. "$script" 2>/dev/null
36+
case "$cur" in
37+
""|-)
38+
shorts=( "${!__argsparse_short_options[@]}" )
39+
shorts=( "${shorts[@]/#/-}" )
40+
;;&
41+
""|-*)
42+
longs=( "${!__argsparse_options_descriptions[@]}" )
43+
longs=( "${longs[@]/#/--}" )
44+
printf %s "${shorts[*]} ${longs[*]}"
45+
;;
46+
*)
47+
longs=( "${!__argsparse_options_descriptions[@]}" )
48+
longs=( "${longs[@]/#/--}" )
49+
[[ "$prev" = --* ]] || return 0
50+
option=${prev#--}
51+
__argsparse_index_of "$prev" "${longs[@]}" >/dev/null || \
52+
return 0
53+
if array=$(__argsparse_values_array_identifier "$option")
54+
then
55+
values=( ${!array} )
56+
printf %s "${values[*]}"
57+
fi
58+
;;
59+
esac
60+
)
61+
}
62+
63+
_argsparse_complete() {
64+
local cur prev words cword split
65+
_init_completion -s || return
66+
local argsparse_complete=$(__argsparse_complete "${words[0]}")
67+
COMPREPLY=( $(compgen -W "$argsparse_complete" -- "$cur" ) )
68+
}
69+
70+
complete -F _argsparse_complete 1-basics 2-values 3-cumulative-options 4-types 5-custom-types 6-properties 7-value-checking 8-setting-hook 9-misc
71+

0 commit comments

Comments
 (0)