<a href="https://github.com/DaveJarvis/keenwrite/blob/main/scripts/build-template">https://github.com/DaveJarvis/keenwrite/blob/main/scripts/bu...</a><p>My template script provides a way to make user-friendly shell scripts. In a script that uses the template, you define the dependencies and their sources as comma-separated values:<p><pre><code> DEPENDENCIES=(
"gradle,https://gradle.org"
"warp-packer,https://github.com/Reisz/warp/releases"
"tar,https://www.gnu.org/software/tar"
"wine,https://www.winehq.org"
"unzip,http://infozip.sourceforge.net"
)
</code></pre>
You define the command-line arguments:<p><pre><code> ARGUMENTS+=(
"a,arch,Target operating system architecture (amd64)"
"o,os,Target operating system (linux, windows, mac)"
"u,update,Java update version number (${ARG_JAVA_UPDATE})"
"v,version,Full Java version (${ARG_JAVA_VERSION})"
)
</code></pre>
You define the "execute()" method that is called after the arguments are parsed:<p><pre><code> execute() {
// Make the computer do the work.
return 1
}
</code></pre>
If the script takes arguments, handle each one individually:<p><pre><code> argument() {
local consume=2
case "$1" in
-a|--arch)
ARG_JAVA_ARCH="$2"
;;
-o|--os)
ARG_JAVA_OS="$2"
;;
esac
return ${consume}
}
</code></pre>
Then call the template's main to start the script rolling:<p><pre><code> main "$@"
</code></pre>
For 99% of the scripts I write, this provides:<p>* Built-in software dependencies verification.<p>* Instructions to the user when requirements are missing.<p>* Simple command-line argument parsing.<p>* Help and logging using ANSI colour.<p>Here's a complete script that builds the Windows, Linux, and Mac installers for my Markdown editor:<p><a href="https://github.com/DaveJarvis/KeenWrite/blob/main/installer.sh">https://github.com/DaveJarvis/KeenWrite/blob/main/installer....</a><p>There's a write-up about creating the script that has a lot more details about how the template works:<p><a href="https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdown-part-1/" rel="nofollow noreferrer">https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdow...</a><p>Note that it is technically possible to improve the scripts such that handling individual arguments can be done in the template itself. This would require a slightly different argument definition semantics:<p><pre><code> ARGUMENTS+=(
"ARG_JAVA_ARCH,a,arch,Target operating system architecture (amd64)"
"ARG_JAVA_OS,o,os,Target operating system (linux, windows, mac)"
"usage=utile_usage,h,help,Show this help message then exit"
)
</code></pre>
By detecting an `=` symbol for the first item in the lists, it's possible to know whether a command-line argument is assigning a variable value, or whether it means to perform additional functionality. (PR welcome!)