Last active
October 28, 2019 20:31
-
-
Save mikeslattery/a9a3916d6b0f9586d8c594b6fe0da853 to your computer and use it in GitHub Desktop.
An empty Bash script as a template. See #TODO for places to change.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() { sed 's/^ //' >&2 <<<USAGETEXT | |
#TODO: Description of script here. | |
Usage: | |
$(sed -r 's/^#@@ (.*)$/ \1/p' < "${BASH_SOURCE[0]}") | |
Variables: | |
$(sed -r 's/^#@# (.*)$/ \1/p' < "${BASH_SOURCE[0]}") | |
USAGETEXT | |
exit 1 | |
} | |
set -euo pipefail; IFS=$'\n\t' | |
if command -v shellcheck &>/dev/null; then shellcheck "$0"; fi | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
BASENAME="$(basename "${BASH_SOURCE[0]}")" | |
shopt -s expand_aliases | |
source "${dir}/xplatform.inc.sh" | |
log() { echo "$(date +%T) $1: $2" >&2; } | |
info() { log "INFO" "$1"; } | |
warn() { log "WARN" "$1"; } | |
# Run command while echoing what it's doing | |
x() { (set -x -euo pipefail; "$@";); } | |
die() { log "ERROR" "$1"; exit 1; } | |
# Like xargs but works for a function. | |
fargs() { "$@" $(</dev/stdin); } | |
# Load configuration | |
config() { if [[ -f "$1" ]]; then source "$1"; fi; } | |
config "$DIR/${BASENAME}.conf" | |
config "$HOME/.config/${BASENAME}" | |
config "$PWD/.${BASENAME}" | |
# Sub-Commands go here. Pattern: $basename-subcommand() {} | |
# Lines that start with #@@ are available in usage. | |
#TODO: remove xxx-subcommand, VAR1 examples | |
#@# VAR1 | |
#@# A configuration variable for some use. | |
VAR1="${VAR1:-defaultvalue}" | |
#@@ subcommand | |
#@@ Does something. | |
xxx-subcommand() { | |
set -euo pipefail | |
# Code goes here | |
} | |
main() { | |
[[ "$#" -gt 0 ]] || usage | |
case "$1"; do | |
help|usage) | |
usage 2>&1 || true; exit 0 | |
;; | |
repl) | |
echo 'Type jf-help for help' | |
while read -e -i "$*" -p "$0 \$ " line; do $line; done | |
;; | |
*) | |
command="${BASENAME}-$1"; shift | |
command -v "$command" 2>/dev/null || die "Unknown command: ${command}" | |
"$command" "$@" | |
;; | |
done | |
} | |
# This is the main entrypoint. This should stay at end of this file. | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO:
${HOME}/.config/${basename}:${HOME}/.${basename},${PWD}/.${basename}
-c/--config
- Read config from alternative file. default args in ini format. Sections=subcommands.-e/--env <var>=<value>
- set environmental variable-C <path>
- work dir. cd before running command.-p/--paginate
- send output toless
-g/--grep
- filter output with a regex--no-ansi
- sed to strip colors-q/--quiet
- no stderr messages.--version
- calls version subcommand-y/--yes
- Use default answers for any questions.tr ':\n' '\0\0' <<<"$PATH" | xargs -0 -I{} find '{}' -maxdepth 1 -maxdepth 1 -name "$comand"