Skip to content

Commit b30d2aa

Browse files
committed
- Add examples/command-examples-on-error
1 parent 5e58388 commit b30d2aa

File tree

8 files changed

+220
-0
lines changed

8 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Command Examples on Error Example
2+
3+
Demonstrates how to show examples whenever the user does not provide all the
4+
required arguments.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init
10+
# ... now edit src/bashly.yml to match the example ...
11+
$ bashly add settings
12+
# ... now edit settings.yml to match the example ...
13+
$ bashly generate
14+
```
15+
16+
<!-- include: settings.yml -->
17+
18+
-----
19+
20+
## `bashly.yml`
21+
22+
````yaml
23+
name: cli
24+
help: Sample application
25+
version: 0.1.0
26+
27+
commands:
28+
- name: download
29+
alias: d
30+
help: Download a file
31+
32+
args:
33+
- name: source
34+
required: true
35+
help: URL to download from
36+
- name: target
37+
help: "Target filename (default: same as source)"
38+
39+
flags:
40+
- long: --force
41+
short: -f
42+
help: Overwrite existing files
43+
44+
# Examples can be provided either as an array, or as a string.
45+
# The array form is convenient when you just want to provide one-liner
46+
# examples.
47+
examples:
48+
- cli download example.com
49+
- cli download example.com ./output -f
50+
51+
- name: upload
52+
alias: u
53+
help: Upload a file
54+
args:
55+
- name: source
56+
required: true
57+
help: File to upload
58+
59+
flags:
60+
- long: --user
61+
short: -u
62+
arg: user
63+
help: Username to use for logging in
64+
required: true
65+
- long: --password
66+
short: -p
67+
arg: password
68+
help: Password to use for logging in
69+
70+
# The string form examples is useful when you wish to have more control
71+
# over how the examples are displayed. Note the use of the '|-' marker
72+
# that tells YAML to use the string as is, including the newlines it contains.
73+
examples: |-
74+
Upload a file
75+
$ cli upload profile.png -u admin -p s3cr3t
76+
77+
Upload a file (you will be prompted to provide a password)
78+
$ cli upload profile.png --user admin
79+
````
80+
81+
## `settings.yml`
82+
83+
````yaml
84+
show_examples_on_error: true
85+
86+
````
87+
88+
89+
## Output
90+
91+
### `$ ./cli download`
92+
93+
````shell
94+
missing required argument: SOURCE
95+
usage: cli download SOURCE [TARGET] [OPTIONS]
96+
examples:
97+
cli download example.com
98+
cli download example.com ./output -f
99+
100+
101+
````
102+
103+
### `$ ./cli upload`
104+
105+
````shell
106+
missing required argument: SOURCE
107+
usage: cli upload SOURCE [OPTIONS]
108+
examples:
109+
Upload a file
110+
$ cli upload profile.png -u admin -p s3cr3t
111+
112+
Upload a file (you will be prompted to provide a password)
113+
$ cli upload profile.png --user admin
114+
115+
116+
````
117+
118+
119+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
show_examples_on_error: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
commands:
6+
- name: download
7+
alias: d
8+
help: Download a file
9+
10+
args:
11+
- name: source
12+
required: true
13+
help: URL to download from
14+
- name: target
15+
help: "Target filename (default: same as source)"
16+
17+
flags:
18+
- long: --force
19+
short: -f
20+
help: Overwrite existing files
21+
22+
# Examples can be provided either as an array, or as a string.
23+
# The array form is convenient when you just want to provide one-liner
24+
# examples.
25+
examples:
26+
- cli download example.com
27+
- cli download example.com ./output -f
28+
29+
- name: upload
30+
alias: u
31+
help: Upload a file
32+
args:
33+
- name: source
34+
required: true
35+
help: File to upload
36+
37+
flags:
38+
- long: --user
39+
short: -u
40+
arg: user
41+
help: Username to use for logging in
42+
required: true
43+
- long: --password
44+
short: -p
45+
arg: password
46+
help: Password to use for logging in
47+
48+
# The string form examples is useful when you wish to have more control
49+
# over how the examples are displayed. Note the use of the '|-' marker
50+
# that tells YAML to use the string as is, including the newlines it contains.
51+
examples: |-
52+
Upload a file
53+
$ cli upload profile.png -u admin -p s3cr3t
54+
55+
Upload a file (you will be prompted to provide a password)
56+
$ cli upload profile.png --user admin
57+
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/download_command.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/upload_command.sh'"
2+
echo "# code for 'cli upload' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./src/*.sh
4+
5+
set -x
6+
7+
bashly generate
8+
9+
### Try Me ###
10+
11+
./cli download
12+
./cli upload
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
+ bashly generate
2+
creating user files in src
3+
created src/download_command.sh
4+
created src/upload_command.sh
5+
created ./cli
6+
run ./cli --help to test your bash script
7+
+ ./cli download
8+
missing required argument: SOURCE
9+
usage: cli download SOURCE [TARGET] [OPTIONS]
10+
examples:
11+
cli download example.com
12+
cli download example.com ./output -f
13+
+ ./cli upload
14+
missing required argument: SOURCE
15+
usage: cli upload SOURCE [OPTIONS]
16+
examples:
17+
Upload a file
18+
$ cli upload profile.png -u admin -p s3cr3t
19+
20+
Upload a file (you will be prompted to provide a password)
21+
$ cli upload profile.png --user admin

0 commit comments

Comments
 (0)