Skip to content

Instantly share code, notes, and snippets.

@mikeslattery
Last active October 28, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeslattery/a9a3916d6b0f9586d8c594b6fe0da853 to your computer and use it in GitHub Desktop.
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.
#!/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 "$@"
@mikeslattery
Copy link
Author

mikeslattery commented Dec 7, 2018

TODO:

  • Include default args from: ${HOME}/.config/${basename}:${HOME}/.${basename},${PWD}/.${basename}
  • Make a general-purpose library and source include it. It will include xplatform.
  • option: -c/--config - Read config from alternative file. default args in ini format. Sections=subcommands.
  • option: -e/--env <var>=<value> - set environmental variable
  • option: -C <path> - work dir. cd before running command.
  • option: -p/--paginate - send output to less
  • options: -g/--grep - filter output with a regex
  • option: --no-ansi - sed to strip colors
  • option: -q/--quiet - no stderr messages.
  • option: --version - calls version subcommand
  • option: -y/--yes - Use default answers for any questions.
  • options: --version, --help - calls command of same name
  • command: repl - interactive command line to run commands. No prompt if not a tty.
  • commands: help, version, env (show variables)
  • For usage, if command not in source, search path:
    tr ':\n' '\0\0' <<<"$PATH" | xargs -0 -I{} find '{}' -maxdepth 1 -maxdepth 1 -name "$comand"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment