From 97035d34257b8723faea58da169d732f718c7ebc Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Fri, 27 Apr 2012 13:37:14 +0200 Subject: [PATCH] ENH: Print usage help for all passed actions. Extend the support for specific usage help to built-in actions. --- tests/t2110-help-action.sh | 66 +++++++++++++++++++++++++++++++++++++ tests/t8020-actions-help.sh | 29 +++++++++++++--- todo.sh | 45 ++++++++++++++----------- 3 files changed, 117 insertions(+), 23 deletions(-) create mode 100755 tests/t2110-help-action.sh diff --git a/tests/t2110-help-action.sh b/tests/t2110-help-action.sh new file mode 100755 index 0000000..06d910c --- /dev/null +++ b/tests/t2110-help-action.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +test_description='built-in actions help functionality + +This test checks listing the usage help of a built-in action. +' +. ./test-lib.sh + +test_todo_session 'nonexisting action help' <<'EOF' +>>> todo.sh help doesnotexist +TODO: No action "doesnotexist" exists. +=== 1 + +>>> todo.sh help hel +TODO: No action "hel" exists. +=== 1 + +>>> todo.sh help h +TODO: No action "h" exists. +=== 1 +EOF + +test_todo_session 'single action help' <<'EOF' +>>> todo.sh help shorthelp + shorthelp + List the one-line usage of all built-in and add-on actions. +\ +EOF + +test_todo_session 'multiple actions help' <<'EOF' +>>> todo.sh help shorthelp append + shorthelp + List the one-line usage of all built-in and add-on actions. +\ + append ITEM# "TEXT TO APPEND" + app ITEM# "TEXT TO APPEND" + Adds TEXT TO APPEND to the end of the task on line ITEM#. + Quotes optional. +\ +EOF + +test_todo_session 'short and long form of action help' <<'EOF' +>>> todo.sh help append + append ITEM# "TEXT TO APPEND" + app ITEM# "TEXT TO APPEND" + Adds TEXT TO APPEND to the end of the task on line ITEM#. + Quotes optional. +\ + +>>> todo.sh help app + app ITEM# "TEXT TO APPEND" + Adds TEXT TO APPEND to the end of the task on line ITEM#. + Quotes optional. +\ +EOF + +test_todo_session 'mixed existing and nonexisting action help' <<'EOF' +>>> todo.sh help shorthelp doesnotexist list + shorthelp + List the one-line usage of all built-in and add-on actions. +\ +TODO: No action "doesnotexist" exists. +=== 1 +EOF + +test_done diff --git a/tests/t8020-actions-help.sh b/tests/t8020-actions-help.sh index 3d6ee1f..cc79bc1 100755 --- a/tests/t8020-actions-help.sh +++ b/tests/t8020-actions-help.sh @@ -1,6 +1,6 @@ #!/bin/bash -test_description='actions help functionality +test_description='custom actions help functionality This test checks listing the usage help of a custom action. ' @@ -9,12 +9,13 @@ This test checks listing the usage help of a custom action. test_todo_session 'custom action help with no custom action directory' <<'EOF' >>> todo.sh help foo -TODO: No actions directory exists. +TODO: No action "foo" exists. === 1 EOF make_action "foo" make_action "bar" +make_action "ls" make_action "quux" test_todo_session 'custom action help' <<'EOF' @@ -41,15 +42,35 @@ EOF test_todo_session 'nonexisting action help' <<'EOF' >>> todo.sh help doesnotexist -TODO: No add-on action "doesnotexist" exists. +TODO: No action "doesnotexist" exists. === 1 >>> todo.sh help foo doesnotexist bar foo ITEM#[, ITEM#, ...] [TERM...] This custom action does foo. \ -TODO: No add-on action "doesnotexist" exists. +TODO: No action "doesnotexist" exists. === 1 EOF +test_todo_session 'mixed built-in and custom actions help' <<'EOF' +>>> todo.sh help foo shorthelp bar + foo ITEM#[, ITEM#, ...] [TERM...] + This custom action does foo. +\ + shorthelp + List the one-line usage of all built-in and add-on actions. +\ + bar ITEM#[, ITEM#, ...] [TERM...] + This custom action does bar. +\ +EOF + +test_todo_session 'custom override of built-in action help' <<'EOF' +>>> todo.sh help ls + ls ITEM#[, ITEM#, ...] [TERM...] + This custom action does ls. +\ +EOF + test_done diff --git a/todo.sh b/todo.sh index 8d1080e..807ac2c 100755 --- a/todo.sh +++ b/todo.sh @@ -54,7 +54,7 @@ shorthelp() del|rm ITEM# [TERM] depri|dp ITEM#[, ITEM#, ITEM#, ...] do ITEM#[, ITEM#, ITEM#, ...] - help + help [ACTION...] list|ls [TERM...] listall|lsa [TERM...] listaddons @@ -154,6 +154,13 @@ help() EndVerboseHelp + actionsHelp + addonHelp + exit 1 +} + +actionsHelp() +{ cat <<-EndActionsHelp Built-in Actions: add "THING I NEED TO DO +project @context" @@ -200,8 +207,9 @@ help() do ITEM#[, ITEM#, ITEM#, ...] Marks task(s) on line ITEM# as done in todo.txt. - help - Display this help message. + help [ACTION...] + Display help about usage, options, built-in and add-on actions, + or just the usage help for the passed ACTION(s). list [TERM...] ls [TERM...] @@ -278,9 +286,6 @@ help() List the one-line usage of all built-in and add-on actions. EndActionsHelp - - addonHelp - exit 1 } addonHelp() @@ -302,21 +307,23 @@ addonHelp() fi } -addonUsage() +actionUsage() { - if [ -d "$TODO_ACTIONS_DIR" ]; then - for actionName - do - action="${TODO_ACTIONS_DIR}/${actionName}" - if [ -f "$action" -a -x "$action" ]; then - "$action" usage + for actionName + do + action="${TODO_ACTIONS_DIR}/${actionName}" + if [ -f "$action" -a -x "$action" ]; then + "$action" usage + else + builtinActionUsage=$(actionsHelp | sed -ne "/^ ${actionName//\//\\/}\\( \\|\$\\)/,/^\$/p") + if [ "$builtinActionUsage" ]; then + echo "$builtinActionUsage" + echo else - die "TODO: No add-on action \"${actionName}\" exists." + die "TODO: No action \"${actionName}\" exists." fi - done - else - die "TODO: No actions directory exists." - fi + fi + done } die() @@ -1095,7 +1102,7 @@ case $action in shift ## Was help; new $1 is first help topic / action name if [ $# -gt 0 ]; then # Don't use PAGER here; we don't expect much usage output from one / few actions. - addonUsage "$@" + actionUsage "$@" else if [ -t 1 ] ; then # STDOUT is a TTY if which "${PAGER:-less}" >/dev/null 2>&1; then