better help text on invalid commands

adding help again is a bad idea. code should never
document itself; that's what documentation is for.

so, make the code do a better job telling the user
where to find documentation.

Signed-off-by: Leah Rowe <leah@libreboot.org>
audit2-merge1
Leah Rowe 2024-06-03 00:17:36 +01:00 committed by Leah Rowe
parent 1204bc3c96
commit 1d866d17d8
4 changed files with 17 additions and 8 deletions

12
build
View File

@ -23,7 +23,7 @@ linkname="${linkpath##*/}"
main() main()
{ {
x_ id -u 1>/dev/null 2>/dev/null x_ id -u 1>/dev/null 2>/dev/null
[ $# -lt 1 ] && $err "Check $projectname documentation for help." [ $# -lt 1 ] && badcmd
spath="script/$1" spath="script/$1"
[ "$1" = "dependencies" ] && x_ install_packages $@ && return 0 [ "$1" = "dependencies" ] && x_ install_packages $@ && return 0
@ -39,15 +39,15 @@ main()
version) printf "%s\nWebsite: %s\n" "$relname" "$projectsite" ;; version) printf "%s\nWebsite: %s\n" "$relname" "$projectsite" ;;
release) shift 1; mkrelease $@ ;; release) shift 1; mkrelease $@ ;;
*) *)
[ -f "$spath" ] || $err "Bad command. Check docs." [ -f "$spath" ] || badcmd
shift 1; "$spath" $@ || $err "excmd: $spath $@" ;; shift 1; "$spath" $@ || $err "excmd: $spath $@" ;;
esac esac
} }
install_packages() install_packages()
{ {
[ $# -lt 2 ] && $err "Bad command. Check docs." [ $# -lt 2 ] && badcmd "fewer than two arguments"
[ -f "config/dependencies/$2" ] || $err "Unsupported target" [ -f "config/dependencies/$2" ] || badcmd "unsupported target"
. "config/dependencies/$2" . "config/dependencies/$2"
@ -74,11 +74,11 @@ mkrelease()
vdir="release" vdir="release"
while getopts d:m: option; do while getopts d:m: option; do
[ -z "$OPTARG" ] && $err "Empty argument not allowed" [ -z "$OPTARG" ] && badcmd "empty argument not allowed"
case "$option" in case "$option" in
d) vdir="$OPTARG" ;; d) vdir="$OPTARG" ;;
m) mode="$OPTARG" ;; m) mode="$OPTARG" ;;
*) $err "Invalid option" ;; *) badcmd "invalid option '-$option'" ;;
esac esac
done done

View File

@ -15,6 +15,15 @@ tmpgit="$PWD/tmp/gitclone"
grubdata="config/data/grub" grubdata="config/data/grub"
err="err_" err="err_"
badcmd()
{
errmsg="no context given"
[ $# -gt 0 ] && errmsg="$1"
dstr="Check $projectname docs via $projectsite"
[ -d "docs" ] && dstr="$dstr (local docs available via docs/)"
$err "Bad command ($errmsg). $dstr"
}
err_() err_()
{ {
printf "ERROR %s: %s\n" "$0" "$1" 1>&2 printf "ERROR %s: %s\n" "$0" "$1" 1>&2

View File

@ -69,7 +69,7 @@ main()
handle_serprog() handle_serprog()
{ {
[ -z "${1+x}" ] && $err "bad command. Check $projectname docs." [ -z "${1+x}" ] && badcmd
[ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && $err "bad command" [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && $err "bad command"
if [ "$1" = "rp2040" ]; then if [ "$1" = "rp2040" ]; then
serprog_boards_dir="$picosdk/src/boards/include/boards" serprog_boards_dir="$picosdk/src/boards/include/boards"

View File

@ -27,7 +27,7 @@ main()
-s) mode="savedefconfig" ;; -s) mode="savedefconfig" ;;
-l) mode="olddefconfig" ;; -l) mode="olddefconfig" ;;
-n) mode="nconfig" ;; -n) mode="nconfig" ;;
*) $err "Invalid option" ;; *) badcmd "invalid option '-$option'" ;;
esac esac
shift; project="${OPTARG#src/}"; shift shift; project="${OPTARG#src/}"; shift
done done