@@ -4,23 +4,18 @@ Demonstrates how to add a `help` command to your script. This is only needed
44if you prefer to have the ` your-cli help COMMAND ` syntax in addition to the
55natively supported ` your-cli COMMAND --help ` syntax.
66
7- This functionality is achieved by adding a custom validation filter that
8- verifies the requested help function is callable.
9-
107This example was generated with:
118
129``` bash
1310$ bashly init
14- $ bashly add lib
1511# ... now edit src/bashly.yml to match the example ...
16- # ... now edit src/lib/validate_help_exists.sh to match the example ...
1712$ bashly generate
1813# ... now edit src/help_command.sh to match the example ...
1914$ bashly generate
2015
2116```
2217
23- <!-- include: src/lib/validate_help_exists.sh src/ help_command.sh -->
18+ <!-- include: src/help_command.sh -->
2419
2520-----
2621
@@ -41,11 +36,7 @@ commands:
4136 alias : h
4237 help : Show help about a command
4338 args :
44-
45- # The command argument will be validated with our custom validation
46- # `help_exists`, which is placed in `src/lib/validate_help_exists.sh`
4739 - name : command
48- validate : help_exists
4940 help : Help subject
5041
5142# The rest of this file is not important for the purpose of this example
@@ -94,45 +85,29 @@ commands:
9485 help : Password to use for logging in
9586` ` `
9687
97- ## ` src/lib/validate_help_exists.sh`
98-
99- ` ` ` bash
100- # This function will be called automatically with the argument under validation
101- # as provided by the user.
102- # It will then check that the internal bashly function responsible for showing
103- # help exists, and echo an error if it does not.
104- validate_help_exists() {
105- command="$1"
106-
107- if [[ -z "$command" ]]; then
108- # The user called ` cli help` - so the help function is the root cli_usage
109- # function (replace 'cli' with the name of your script).
110- function_name="cli_usage"
111-
112- else
113- # The user called `cli help COMMAND` - so we check that the command is valid
114- # by ensuring we have a help function for it.
115- function_name="cli_${command}_usage"
116- fi
117-
118- [[ $(type -t "$function_name") ]] || echo "no help for this command"
119- }
120- ```
121-
12288## ` src/help_command.sh`
12389
12490` ` ` bash
12591command="${args[command]}"
12692long_usage=yes
12793
128- # If we have reached this point, we know that the help subject is valid, so
129- # just call the relevant help function.
13094if [[ -z "$command" ]]; then
131- cli_usage
95+ # No command argument, show the global help
96+ help_function=cli_usage
13297else
133- " cli_${command} _usage"
98+ # Show the help for the requested command
99+ help_function="cli_${command}_usage"
134100fi
135101
102+ # Call the help function if it exists
103+ if [[ $(type -t "$help_function") ]]; then
104+ "$help_function"
105+ else
106+ echo "No help available for this command"
107+ exit 1
108+ fi
109+
110+
136111` ` `
137112
138113
@@ -257,8 +232,7 @@ Examples:
257232# ## `$ ./cli help no_such_command`
258233
259234` ` ` shell
260- validation error in COMMAND:
261- no help for this command
235+ No help available for this command
262236
263237
264238` ` `
0 commit comments