First example!

This commit is contained in:
vlad 2021-12-20 18:44:51 -08:00
parent 9aa75c1170
commit 4f70443bda
3 changed files with 139 additions and 1 deletions

8
examples Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
hello() {
_help-line "Say hello!" "$@"
echo Hello, world!
}
source ⚡️

102
test/test-help Normal file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env bash
testUsageIsShownWhenNoCommandIsSpecified() {
echo '
#!/usr/bin/env bash
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go
assertContains "$STDOUT" "Usage"
}
testHelpSubcommandCannotBeOverridden() {
echo '
#!/usr/bin/env bash
help() { echo "ah hah hah" ; }
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go help
assertContains "$STDOUT" "Usage"
}
testHelpIncludesInvocationInstructions() {
SCRIPT_NAME="foo-$RANDOM"
echo '
#!/usr/bin/env bash
' > $SCRIPT_NAME
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> $SCRIPT_NAME
chmod +x $SCRIPT_NAME
try ./$SCRIPT_NAME help
assertContains "$STDOUT" "$SCRIPT_NAME SUBCOMMAND"
}
testHelpListsAllAvailableLightningCommands() {
echo '
#!/usr/bin/env bash
foo() { echo "ah hah hah" ; }
bar() { echo "ah hah hah" ; }
baz() { echo "ah hah hah" ; }
potato() { echo "ah hah hah" ; }
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go help
assertContains "$STDOUT" "foo"
assertContains "$STDOUT" "bar"
assertContains "$STDOUT" "baz"
assertContains "$STDOUT" "potato"
assertContains "$STDOUT" "help"
}
testHelpLinesFromDefinedCommandsAreShown() {
echo '
#!/usr/bin/env bash
hello() {
_help-line "Say Hello" "$@"
echo "Hello, World!" ;
}
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go
assertContains "$STDOUT" "Say Hello"
}
testHelpDoesNotExecuteFunctionsWithNoHelpLine() {
echo '
#!/usr/bin/env bash
hello() { echo "Hello, World!" ; }
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go
assertNotContains "$STDOUT" "Hello, World"
}
testLightningShouldNotBeIncludedInSubcommandNames() {
echo '
#!/usr/bin/env bash
hello() { echo "Hello, World!" ; }
' > go
echo "source $(dirname $BASH_SOURCE)/../⚡️" >> go
chmod +x go
try ./go
assertNotContains "$STDOUT" "⚡hello"
}
source $(dirname $0)/runner

30
⚡️
View File

@ -1,7 +1,35 @@
#!/usr/bin/env bash
main() {
⚡️$1
if [[ $(type -t "⚡️$1") == "function" ]]; then
⚡️$1
else
help
fi
}
help() {
_help-line "Show this list" "$@"
echo Usage: "$0" SUBCOMMAND
echo
echo "Available subcommands:"
for subcommand in $(_subcommands); do
if [[ $(type ⚡️$subcommand) =~ _help-line ]]; then
printf "\t%-20s - %s\n" "$subcommand" "$(⚡️$subcommand help-line)"
else
printf "\t%-20s" $subcommand
fi
done
}
_subcommands() {
compgen -c ⚡️ | cut -c3-
}
_help-line() {
if [[ "$#" == 2 && "$2" == "help-line" ]]; then
echo "$1" && exit 0
fi
}
main "$@"