From 9bdc8f7af1eca8f23eb901d0c93a01cc61e2680e Mon Sep 17 00:00:00 2001 From: redbeardymcgee Date: Thu, 11 Jul 2024 17:29:28 -0500 Subject: [PATCH 1/2] feat: subcommands example This commit introduces an example project structure to introduce subcommands and simple argument parsing. The goal is to increase support for end-user environments by using a wrapper script which will always execute in a POSIX-compatible shell environment. It is worth noting, however, that this technique may be better applied by calling `bash` instead of `sh`. Bash has, in practical terms, nearly equal coverage as POSIX shell. The only caveat is really MacOS, which only ships the final 3.x release. No problem though, because bash3 has plenty of support for nicer features like arrays. If anything more complex was necessary, this would cease to be worth doing in pure shell script anymore. `getopts` is also another POSIX alternative to parse $@. Unfortunately, it only supports a very narrow use-case; namely only short single letter options following a single dash e.g. `-h` for `help`. Worth noting anyway. Test this script: ```shell ./shmux check foo ./shmux new foo ``` --- example.sh | 0 functions.sh | 0 lib/mgmt.sh | 20 ++++++++++++++++ shmux | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) mode change 100755 => 100644 example.sh mode change 100755 => 100644 functions.sh create mode 100644 lib/mgmt.sh create mode 100755 shmux diff --git a/example.sh b/example.sh old mode 100755 new mode 100644 diff --git a/functions.sh b/functions.sh old mode 100755 new mode 100644 diff --git a/lib/mgmt.sh b/lib/mgmt.sh new file mode 100644 index 0000000..b42dbfd --- /dev/null +++ b/lib/mgmt.sh @@ -0,0 +1,20 @@ +# This is how you manage your sessions +# you can create a new sesssion, which will copy the +# example.sh file with a name of your choosing +# and you can edit from there + +check_for_project() { + printf 'check: %s\n' "$1" +} + +new_project() { + printf 'new: %s\n' "$1" +} + +load_project() { + printf 'load: %s\n' "$1" +} + +edit_project() { + printf 'edit: %s\n' "$1" +} diff --git a/shmux b/shmux new file mode 100755 index 0000000..42a7583 --- /dev/null +++ b/shmux @@ -0,0 +1,68 @@ +#!/usr/bin/env sh + +. ./lib/mgmt.sh + +print_error() { + printf 'ERROR: %s requires a project name.\n' "$1" >&2 +} + +show_help() { + printf 'Help has arrived!\n' +} + +while :; do + case $1 in + -h | -\? | --help | help) + show_help + exit + ;; + check | check_for_project) + if [ -n "$2" ]; then + shift + check_for_project "$@" + else + print_error "$1" + exit 1 + fi + ;; + new | new_project) + if [ -n "$2" ]; then + shift + new_project "$@" + else + print_error "$1" + exit 1 + fi + ;; + load | load_project) + if [ -n "$2" ]; then + shift + load_project "$@" + else + print_error "$1" + exit 1 + fi + ;; + edit | edit_project) + if [ -n "$2" ]; then + shift + edit_project "$@" + else + print_error "$1" + exit 1 + fi + ;; + --) # End of all options. Passthru additional options to tmux? + shift + break + ;; + -?*) + printf 'ERROR: Unknown option: %s\n' "$1" >&2 + exit 1 + ;; + *) # Default case: If no more options then break out of the loop. + break ;; + esac + + shift +done From 28752bb8d5aeea887039a5aec505a160e6478d3d Mon Sep 17 00:00:00 2001 From: redbeardymcgee Date: Thu, 11 Jul 2024 17:58:20 -0500 Subject: [PATCH 2/2] tidy: move helpers into utils.sh --- lib/utils.sh | 7 +++++++ shmux | 9 +-------- 2 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 lib/utils.sh diff --git a/lib/utils.sh b/lib/utils.sh new file mode 100644 index 0000000..e1545ea --- /dev/null +++ b/lib/utils.sh @@ -0,0 +1,7 @@ +print_error() { + printf 'ERROR: %s requires a project name.\n' "$1" >&2 +} + +show_help() { + printf 'Help has arrived!\n' +} diff --git a/shmux b/shmux index 42a7583..e2560d8 100755 --- a/shmux +++ b/shmux @@ -1,15 +1,8 @@ #!/usr/bin/env sh +. ./lib/utils.sh . ./lib/mgmt.sh -print_error() { - printf 'ERROR: %s requires a project name.\n' "$1" >&2 -} - -show_help() { - printf 'Help has arrived!\n' -} - while :; do case $1 in -h | -\? | --help | help)