Skip to content

Instantly share code, notes, and snippets.

@minhtrancccp
Last active April 26, 2024 18:41
Show Gist options
  • Save minhtrancccp/77bd34c68d2af920b3109118813e953d to your computer and use it in GitHub Desktop.
Save minhtrancccp/77bd34c68d2af920b3109118813e953d to your computer and use it in GitHub Desktop.
a hopefuly more improved version of the `helpfix` function
#!/bin/bash
helpfix() {
local status=$?
test $status -eq 0 && return
local command
local stdout
command=$(mktemp)
fc -ln -1 >"$command"
if grep -Eq '\S\s+(-h|--help)(\s|$)' "$command"; then
stdout=$(mktemp)
bash "$command" &>"$stdout"
while test "$status" -ne 0 && ! grep -Eq '^\s+-' "$stdout"; do
if grep -Eq '\s-h(\s|$)' "$command"; then
sed -Ei 's/\s-h(\s|$)/ --help\1/' "$command"
elif grep -Eq '\s--help(\s|$)' "$command"; then
sed -Ei 's/\s--help(\s|$)/ -help\1/' "$command"
else
break
fi
bash -x "$command" |& tee "$stdout"
status=${PIPESTATUS[0]}
done
fi
rm -f "$command" "$stdout"
return "$status"
}
PROMPT_COMMAND=helpfix"${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment