ENH: Handle -h, shorthelp and help when a Fatal Error happens.

The user may need the help to solve any fatal error that appears while todo.sh isn't properly set up. As the help actions do not depend on any setting that the fatal errors check, we can still invoke them.

Factor out dieWithHelp() and use that for printing the fatal errors.
This commit is contained in:
Ingo Karkat
2012-08-30 13:16:02 +02:00
parent 8d8ef812a2
commit 8ff79102a5
2 changed files with 33 additions and 12 deletions

View File

@@ -40,23 +40,35 @@ EOF
# Verify that custom configuration is actually processed (when the -d option # Verify that custom configuration is actually processed (when the -d option
# precedes the -h option) by specifying a different actions directory and moving # precedes the -h option) by specifying a different actions directory and moving
# our custom action there. The help output should mention the "Add-On Actions". # our custom action there. The help output should mention the "Add-On Actions".
set -o pipefail # So that the sed filter doesn't swallow todo.sh's exit code.
mv todo.cfg custom.cfg mv todo.cfg custom.cfg
mv .todo.actions.d custom.actions mv .todo.actions.d custom.actions
echo 'export TODO_ACTIONS_DIR=$HOME/custom.actions' >> custom.cfg echo 'export TODO_ACTIONS_DIR=$HOME/custom.actions' >> custom.cfg
test_todo_session '-h fatal error without config' <<EOF test_todo_session '-h and fatal error without config' <<EOF
>>> todo.sh -h >>> todo.sh -h | sed '/^ \\{0,2\\}[A-Z]/!d'
Fatal Error: Cannot read configuration file $HOME/.todo/config Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description]
=== 1 Actions:
EOF Actions can be added and overridden using scripts in the actions
test_todo_session '-h fatal error with trailing custom config' <<EOF See "help" for more details.
>>> todo.sh -h -d custom.cfg
Fatal Error: Cannot read configuration file $HOME/.todo/config Fatal Error: Cannot read configuration file $HOME/.todo/config
=== 1 === 1
EOF EOF
# Config option comes too late; "Add-on Actions" is *not* mentioned here.
test_todo_session '-h and fatal error with trailing custom config' <<EOF
>>> todo.sh -h -d custom.cfg | sed '/^ \\{0,2\\}[A-Z]/!d'
Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description]
Actions:
Actions can be added and overridden using scripts in the actions
See "help" for more details.
Fatal Error: Cannot read configuration file $HOME/.todo/config
=== 1
EOF
# Config option processed; "Add-on Actions" is mentioned here.
test_todo_session '-h output with preceding custom config' <<EOF test_todo_session '-h output with preceding custom config' <<EOF
>>> todo.sh -d custom.cfg -h | sed '/^ [A-Z]/!d' >>> todo.sh -d custom.cfg -h | sed '/^ \\{0,2\\}[A-Z]/!d'
Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description]
Actions: Actions:
Actions can be added and overridden using scripts in the actions Actions can be added and overridden using scripts in the actions

17
todo.sh
View File

@@ -81,7 +81,6 @@ shorthelp()
See "help" for more details. See "help" for more details.
EndHelpFooter EndHelpFooter
exit 0
} }
help() help()
@@ -327,6 +326,16 @@ actionUsage()
done done
} }
dieWithHelp()
{
case "$1" in
help) help;;
shorthelp) shorthelp;;
esac
shift
die "$@"
}
die() die()
{ {
echo "$*" echo "$*"
@@ -655,7 +664,7 @@ fi
} }
# === SANITY CHECKS (thanks Karl!) === # === SANITY CHECKS (thanks Karl!) ===
[ -r "$TODOTXT_CFG_FILE" ] || die "Fatal Error: Cannot read configuration file $TODOTXT_CFG_FILE" [ -r "$TODOTXT_CFG_FILE" ] || dieWithHelp "$1" "Fatal Error: Cannot read configuration file $TODOTXT_CFG_FILE"
. "$TODOTXT_CFG_FILE" . "$TODOTXT_CFG_FILE"
@@ -694,8 +703,8 @@ fi
ACTION=${1:-$TODOTXT_DEFAULT_ACTION} ACTION=${1:-$TODOTXT_DEFAULT_ACTION}
[ -z "$ACTION" ] && usage [ -z "$ACTION" ] && usage
[ -d "$TODO_DIR" ] || die "Fatal Error: $TODO_DIR is not a directory" [ -d "$TODO_DIR" ] || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory"
( cd "$TODO_DIR" ) || die "Fatal Error: Unable to cd to $TODO_DIR" ( cd "$TODO_DIR" ) || dieWithHelp "$1" "Fatal Error: Unable to cd to $TODO_DIR"
[ -f "$TODO_FILE" ] || cp /dev/null "$TODO_FILE" [ -f "$TODO_FILE" ] || cp /dev/null "$TODO_FILE"
[ -f "$DONE_FILE" ] || cp /dev/null "$DONE_FILE" [ -f "$DONE_FILE" ] || cp /dev/null "$DONE_FILE"