X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=topgit.git;a=blobdiff_plain;f=tg.sh;h=6282250ee0d4e63fcca583a52cd47fc39aefc53e;hp=7c6c09f2eaa709933dc4631495c7b440556882db;hb=6e92736dd815e29c3715362970380f5e9a759a11;hpb=d0fd65c3d26419ec9f21310c7aaea73610b058d8 diff --git a/tg.sh b/tg.sh index 7c6c09f..6282250 100644 --- a/tg.sh +++ b/tg.sh @@ -265,6 +265,29 @@ do_help() fi } +# Check whether we are supposed to output the help message +should_do_help() +{ + # we are being sourced for utility functions, never run help + [ -z "$tg__include" ] || return 1 + + local prev + while [ -n "$1" ]; do + case "$1" in + help|--help|-h) + shift + echo "${1:-$prev}" + return 0 + esac + prev="$1" + shift + done + + # run help when there was no previous topic, meaning that there where + # no arguments at all + test -z "$prev" +} + ## Pager stuff # isatty FD @@ -309,6 +332,9 @@ setup_pager() [ -d "@cmddir@" ] || die "No command directory: '@cmddir@'" +# check if we should run help and get the topic while we're at it +help_topic="$(should_do_help "$@")" && { do_help "$help_topic"; exit 0; } + ## Initial setup set -e @@ -344,9 +370,6 @@ cmd="$1" shift case "$cmd" in -help|--help|-h) - do_help "$1" - exit 0;; --hooks-path) # Internal command echo "@hooksdir@";;