Skip to content

Instantly share code, notes, and snippets.

@ychaouche
Last active January 29, 2025 14:50
function helpless() {
# ______________________________________________________________________
# /\ \
# \_| some commands don't support --help nor -h flags |
# | and so command --help or command -h results in a error exit code |
# | and a display of the "usage" on stderr. |
# | |
# | some other commands will tell you that --help isn't supported |
# | and print an error message that asks you to use -h instead |
# | |
# | some other commands use -h but not for help |
# | and to get help you need --help |
# | so they'll either print an error message |
# | or print something that is neither an error message |
# | nor a help message |
# | processing -h as any random flag |
# | (grep for example) |
# | _________________________________________________________________|_
# \_/___________________________________________________________________/
tmp=$(mktemp)
trap "rm -f $tmp;" RETURN
# Check if the command supports --help
$@ --help > "$tmp" 2>/dev/null && { less "$tmp"; return; }
# Check if the command supports -h
$@ -h > "$tmp" 2>/dev/null && { less "$tmp"; return; }
# chec if it's a bash builtin
help "$@" > "$tmp" 2>/dev/null && { less "$tmp"; return; }
# does this command even exist?
# if so, see if it's a special case like ifdata
# that doesn't support any help option
# and prints its help message on stderr
type "$1" >/dev/null 2>&1 && { "$1" 2>&1 | less; return; }
# if we're still here,
# it means the command doesn't exist.
echo "no help for $@ or command not found"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment