From 9913010f15f1fb7a3f331e655592fa6fc2807a12 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Fri, 19 Jun 2020 01:04:24 -0400 Subject: [PATCH] mpdthumb: Add --help and --version options. --- mpdthumb.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/mpdthumb.sh b/mpdthumb.sh index 30f30a1..f41c5f6 100755 --- a/mpdthumb.sh +++ b/mpdthumb.sh @@ -12,10 +12,11 @@ : "${THUMBNAILDIR=$XDG_CACHE_HOME/mpdthumb}" # Try to find mpdexec... -case $0 in -/*) self=$0 ;; -*/*) self=$PWD/${0#./} ;; -*) self=`command -v $0` ;; +argv0=$0 +case $argv0 in +/*) self=$argv0 ;; +*/*) self=$PWD/${argv0#./} ;; +*) self=`command -v $argv0` ;; esac owndir=${self%/*} @@ -33,8 +34,49 @@ else : "${CONVERT=convert}" fi -size=x128 +print_version () { + cat <<'EOF' +mpdthumb.sh 0.8 +Copyright © 2020 Nick Bowler +License GPLv3+: GNU General Public License version 3 or any later version. +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. +EOF +} + +print_usage () { + test $# -eq 0 || { printf '%s: ' "$argv0"; printf "$@"; } + printf 'Usage: %s [options] file [file ...]\n' "$argv0" + test $# -eq 0 || printf 'Try %s --help for more information.\n' "$argv0" +} + +print_help () { + print_usage + cat <. +EOF +} +size=x128 lastarg= dashdash= for arg; do @@ -47,8 +89,10 @@ for arg; do --size=*) size=${arg#--size=} ;; --small) size=56 ;; --size) lastarg=$arg ;; + --version) print_version; exit ;; + --help) print_help; exit ;; --) dashdash=: ;; - -*) printf '%s: unrecognized argument: %s\n' "$0" "$arg" 1>&2; exit 1 ;; + -*) print_usage 'unrecognized argument: %s\n' "$arg" 1>&2; exit 1 ;; *) set x "$@" "$arg"; shift esac @@ -57,12 +101,12 @@ done w=${size%x*} h=${size#*x} if expr "$w$h" : '[0-9][0-9]*$' >/dev/null; then :; else - printf '%s: invalid --size setting: %s\n' "$0" "$size" 1>&2 + print_usage 'invalid --size setting: %s\n' "$size" 1>&2 exit 1 fi case $# in 0) - printf 'usage: %s [options] file [file ...]\n' "$0" 1>&2 + print_usage 'at least one filename is required\n' 1>&2 exit 1 esac -- 2.43.2