commit 241cd6325eb2748852b65c9365ce04cf58caa06c
parent 6c00bfb1ace953764711cbdb098cb793668d2dd6
Author: Matthias Beyer <mail@beyermatthias.de>
Date: Thu, 1 Apr 2021 20:39:45 +0200
Use argbash to generate CLI framework
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Tested-by: Matthias Beyer <mail@beyermatthias.de>
Link: https://lore.kernel.org/r/20210401183949.25894-2-mail@beyermatthias.de
Diffstat:
M | notmuch-am | | | 102 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
1 file changed, 95 insertions(+), 7 deletions(-)
diff --git a/notmuch-am b/notmuch-am
@@ -1,12 +1,39 @@
#!/usr/bin/env bash
-B4AM_ARGS=${B4AM_ARGS:-"--add-link"}
+# ###
+#
+# Generated by argbash.io
+#
+# ###
-usage () {
- printf %s "\
-Usage: notmuch-am <thread-id>
+die()
+{
+ local _ret="${2:-1}"
+ test "${_PRINT_HELP:-no}" = yes && print_help >&2
+ echo "$1" >&2
+ exit "${_ret}"
+}
+
+
+begins_with_short_option()
+{
+ local first_option all_short_options='h'
+ first_option="${1:0:1}"
+ test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
+}
-Use b4 to apply a patchset from a thread.
+# THE DEFAULTS INITIALIZATION - POSITIONALS
+_positionals=()
+# THE DEFAULTS INITIALIZATION - OPTIONALS
+
+
+print_help()
+{
+ printf '%s\n' "Use b4 to apply a patchset from a thread."
+ printf 'Usage: %s [-h|--help] <thread-id>\n' "$0"
+ printf '\t%s\n' "<thread-id>: The notmuch thread id of the thread to apply"
+ printf '\t%s\n' "-h, --help: Prints help"
+ printf %s "\
Example
@@ -18,8 +45,68 @@ Example
+ Reviewed-by: Xinglu Chen <public@yoctocell.xyz> (✓ DKIM/yoctocell.xyz)
+ Reviewed-by: Matthias Beyer <mail@beyermatthias.de>
"
-exit 0
}
+parse_commandline()
+{
+ _positionals_count=0
+ while test $# -gt 0
+ do
+ _key="$1"
+ case "$_key" in
+ -h|--help)
+ print_help
+ exit 0
+ ;;
+ -h*)
+ print_help
+ exit 0
+ ;;
+ *)
+ _last_positional="$1"
+ _positionals+=("$_last_positional")
+ _positionals_count=$((_positionals_count + 1))
+ ;;
+ esac
+ shift
+ done
+}
+
+
+handle_passed_args_count()
+{
+ local _required_args_string="'thread-id'"
+ test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
+ test "${_positionals_count}" -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
+}
+
+
+assign_positional_args()
+{
+ local _positional_name _shift_for=$1
+ _positional_names="_arg_thread_id "
+
+ shift "$_shift_for"
+ for _positional_name in ${_positional_names}
+ do
+ test $# -gt 0 || break
+ eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
+ shift
+ done
+}
+
+parse_commandline "$@"
+handle_passed_args_count
+assign_positional_args 1 "${_positionals[@]}"
+
+# ###
+#
+# / Generated by argbash.io
+#
+# ###
+
+
+
+B4AM_ARGS=${B4AM_ARGS:-"--add-link"}
main () {
mbox=$(mktemp)
@@ -29,4 +116,5 @@ main () {
rm -f "$mbox"
}
-[ -n "$1" ] || usage && main "$@"
+[ -n "$1" ] || usage && main "$_arg_thread_id"
+