ENH: Print usage help for all passed actions.

Extend the support for specific usage help to built-in actions.
This commit is contained in:
Ingo Karkat
2012-04-27 13:37:14 +02:00
parent 99e5e57a75
commit 97035d3425
3 changed files with 117 additions and 23 deletions

66
tests/t2110-help-action.sh Executable file
View File

@@ -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

View File

@@ -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

33
todo.sh
View File

@@ -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
else
die "TODO: No add-on action \"${actionName}\" exists."
builtinActionUsage=$(actionsHelp | sed -ne "/^ ${actionName//\//\\/}\\( \\|\$\\)/,/^\$/p")
if [ "$builtinActionUsage" ]; then
echo "$builtinActionUsage"
echo
else
die "TODO: No action \"${actionName}\" exists."
fi
fi
done
else
die "TODO: No actions directory exists."
fi
}
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