Skip to content

Commit 1805a1e

Browse files
committed
simplify help-command example
1 parent 41b0438 commit 1805a1e

File tree

6 files changed

+29
-84
lines changed

6 files changed

+29
-84
lines changed

examples/help-command/README.md

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@ Demonstrates how to add a `help` command to your script. This is only needed
44
if you prefer to have the `your-cli help COMMAND` syntax in addition to the
55
natively 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-
107
This 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
12591
command="${args[command]}"
12692
long_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.
13094
if [[ -z "$command" ]]; then
131-
cli_usage
95+
# No command argument, show the global help
96+
help_function=cli_usage
13297
else
133-
"cli_${command}_usage"
98+
# Show the help for the requested command
99+
help_function="cli_${command}_usage"
134100
fi
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
```

examples/help-command/src/bashly.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ commands:
1212
alias: h
1313
help: Show help about a command
1414
args:
15-
16-
# The command argument will be validated with our custom validation
17-
# `help_exists`, which is placed in `src/lib/validate_help_exists.sh`
1815
- name: command
19-
validate: help_exists
2016
help: Help subject
2117

2218
# The rest of this file is not important for the purpose of this example
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
command="${args[command]}"
22
long_usage=yes
33

4-
# If we have reached this point, we know that the help subject is valid, so
5-
# just call the relevant help function.
64
if [[ -z "$command" ]]; then
7-
cli_usage
5+
# No command argument, show the global help
6+
help_function=cli_usage
87
else
9-
"cli_${command}_usage"
8+
# Show the help for the requested command
9+
help_function="cli_${command}_usage"
1010
fi
11+
12+
# Call the help function if it exists
13+
if [[ $(type -t "$help_function") ]]; then
14+
"$help_function"
15+
else
16+
echo "No help available for this command"
17+
exit 1
18+
fi
19+

examples/help-command/src/lib/sample_function.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/help-command/src/lib/validate_help_exists.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

spec/approvals/examples/help-command

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,4 @@ Examples:
9999
cli download example.com ./output -f
100100

101101
+ ./cli help no_such_command
102-
validation error in COMMAND:
103-
no help for this command
102+
No help available for this command

0 commit comments

Comments
 (0)