commit 91d80e31001a74700adee70767c2f06c1870a3aa
parent 3eaec5cf35c401d1e5310c2bdaee206e9ae762a3
Author: Ayush Agarwal <ayush@fastmail.in>
Date: Mon, 20 Sep 2021 00:38:27 +0530
modified the argparse menu using argbash
`argbash` seems like a decent tool to generate argparse while loop for
bash. Although `getopts` built-in is nice, it's restricted to short `-a`
style options only. I think short options are fine but one can't deny
that long options are easier to remember and perhaps more user-friendly.
Diffstat:
| M | tessen | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++---------------- |
1 file changed, 44 insertions(+), 16 deletions(-)
diff --git a/tessen b/tessen
@@ -193,6 +193,8 @@ die() {
}
main() {
+ local _opt
+
# exit if the password store directory doesn't exist
if ! [[ -d "$PASS_STORE" ]]; then
notify-send "password store not found"
@@ -200,30 +202,56 @@ main() {
fi
# parse any options given by the user
- # shellcheck disable=SC2178
- # we initialized BACKEND as a variable but then used it as an array in case rofi was chosen
- # but this is done after this getopts loop, not before
- while getopts ':hab:s:' opt; do
- case "$opt" in
- h)
+ while [[ "$#" -gt 0 ]]; do
+ _opt="${1-}"
+ case "$_opt" in
+ -b | --backend)
+ [[ "$#" -lt 2 ]] && {
+ printf '%s\n' "Please specify a backend: bemenu|rofi" >&2
+ exit 1
+ }
+ BACKEND="${2-}"
+ validate_backend
+ shift
+ ;;
+ --backend=*)
+ BACKEND="${_opt##--backend=}"
+ validate_backend
+ ;;
+ -t | --autotype)
+ unset -v AT_TYPE 2> /dev/null || {
+ printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2
+ exit 1
+ }
+ readonly AT_TYPE=true
+ ;;
+ -c | --clipboard)
+ unset -v AT_TYPE 2> /dev/null || {
+ printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2
+ exit 1
+ }
+ readonly AT_TYPE=false
+ ;;
+ -h | --help)
print_help
exit 0
;;
- a) WTYPE=1 ;;
- b) BACKEND="$OPTARG" ;;
- s) CLIP_TIME="$OPTARG" ;;
- \?)
- notify-send "invalid option: -$OPTARG"
- exit 1
+ -v | --version)
+ printf '%s\n' "tessen version $VERSION"
+ exit 0
+ ;;
+ --)
+ shift
+ break
;;
- :)
- notify-send "option -$OPTARG requires a value"
+ *)
+ printf '%s\n' "invalid argument detected" >&2
exit 1
;;
esac
+ shift
done
- unset -v opt
- shift $((OPTIND - 1))
+ unset -v _opt
validate_backend
validate_clip_time