Skip to content

Commit 5c8f611

Browse files
authored
Merge pull request #37 from DannyBen/env-vars
Add support for required environment variables
2 parents 1864144 + 284ddb0 commit 5c8f611

File tree

37 files changed

+618
-153
lines changed

37 files changed

+618
-153
lines changed

README.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ See the [examples](examples) folder for more examples.
119119
Configuration Reference
120120
--------------------------------------------------
121121

122+
The `bashly.yml` configuration file consists of these types:
123+
124+
- [Command](#command-options) - defines the root command as well as any
125+
subcommand.
126+
- [Argument](#argument-options) - defines positional arguments.
127+
- [Flag](#flag-options) - defines option flags.
128+
- [Environment Variable](#environment-variable-options) - defines
129+
environment variables required (or desired) by your script.
130+
122131
### Command options
123132

124133
Unless otherwise specified, these definitiona can be used for both the root
@@ -149,11 +158,9 @@ examples:
149158
- myscript download
150159
- myscript download --force
151160

152-
# Specify an array of environment variables needed by your script
153-
# This is used purely for displaying in the help text (when using --help)
154-
# The help for each variable can have multiple lines.
155-
environment_variable:
156-
VARIABLE_NAME: Variable help text
161+
# Specify an array of environment variables needed by your script.
162+
environment_variables:
163+
- ... see below ...
157164

158165
# Specify the array of subcommands to generate.
159166
# Each subcommand will have its own args and flags.
@@ -164,12 +171,12 @@ commands:
164171
# Specify the array of positional arguments this script needs.
165172
# If this is provided, then you cannot specify commands.
166173
args:
167-
- ...
174+
- ... see below ...
168175

169176
# Specify the array of option flags this script needs.
170177
# If this is provided, then you cannot specify commands.
171178
flags:
172-
- ...
179+
- ... see below ...
173180
```
174181
175182
@@ -208,8 +215,9 @@ required: true
208215
The below configuration generates this flag:
209216

210217
```
211-
-o, --output DIRECTORY (required)
212-
Specify the output directory
218+
Options:
219+
-o, --output DIRECTORY (required)
220+
Specify the output directory
213221
```
214222

215223
The flag's value will be available to you as `${args[--output]}` in your
@@ -234,6 +242,33 @@ arg: directory
234242
required: true
235243
```
236244

245+
### Environment Variable options
246+
247+
The below configuration generates this environment variable usage text:
248+
249+
```
250+
Environment Variables:
251+
SECRET_KEY (required)
252+
Your API secret key
253+
```
254+
255+
If an environment variable is defined as required (false by default), the
256+
execution of the script will be halted with a friendly error if it is not
257+
set.
258+
259+
```yaml
260+
# The name of the variable (it will be automatically capitalized).
261+
name: secret_key
262+
263+
# The message to display when using --help.
264+
# This can have multiple lines.
265+
help: Your API secret key
266+
267+
# Specify if this variable is required.
268+
required: true
269+
```
270+
271+
237272

238273
Real World Examples
239274
--------------------------------------------------

Runfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def examples
3535
"examples/minimal/download",
3636
"examples/multiline/multi",
3737
"examples/subcommands/cli",
38+
"examples/environment-variables/cli",
3839
"examples/yaml/yaml",
3940
"spec/fixtures/workspaces/short-flag/rush",
4041
]

examples/colors/colorly

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ cyan_underlined() { echo -e "\e[4;36m$*\e[0m" ; }
9999

100100
# :command.command_functions
101101

102-
# :command.parse_args
103-
parse_args() {
102+
# :command.parse_requirements
103+
parse_requirements() {
104104
# :command.fixed_flag_filter
105105
case "$1" in
106106
--version )
@@ -115,11 +115,12 @@ parse_args() {
115115
;;
116116

117117
esac
118+
# :command.environment_variables_filter
118119
# :command.command_filter
119120
action="root"
120121
# :command.required_args_filter
121122
# :command.required_flags_filter
122-
# :command.parse_args_while
123+
# :command.parse_requirements_while
123124
while [[ $# -gt 0 ]]; do
124125
key="$1"
125126
case "$key" in
@@ -130,7 +131,7 @@ parse_args() {
130131
;;
131132

132133
* )
133-
# :command.parse_args_case
134+
# :command.parse_requirements_case
134135
if [[ ! ${args[message]} ]]; then
135136
args[message]=$1
136137
shift
@@ -162,7 +163,7 @@ initialize() {
162163
# :command.run
163164
run() {
164165
declare -A args
165-
parse_args "$@"
166+
parse_requirements "$@"
166167

167168
if [[ ${args[--version]} ]]; then
168169
version_command

examples/config-ini/configly

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ configly_list_command() {
274274
config_show
275275
}
276276

277-
# :command.parse_args
278-
parse_args() {
277+
# :command.parse_requirements
278+
parse_requirements() {
279279
# :command.fixed_flag_filter
280280
case "$1" in
281281
--version )
@@ -290,6 +290,7 @@ parse_args() {
290290
;;
291291

292292
esac
293+
# :command.environment_variables_filter
293294
# :command.command_filter
294295
action=$1
295296

@@ -300,21 +301,21 @@ parse_args() {
300301
set | s )
301302
action="set"
302303
shift
303-
configly_set_parse_args "$@"
304+
configly_set_parse_requirements "$@"
304305
shift $#
305306
;;
306307

307308
get | g )
308309
action="get"
309310
shift
310-
configly_get_parse_args "$@"
311+
configly_get_parse_requirements "$@"
311312
shift $#
312313
;;
313314

314315
list | l )
315316
action="list"
316317
shift
317-
configly_list_parse_args "$@"
318+
configly_list_parse_requirements "$@"
318319
shift $#
319320
;;
320321

@@ -326,7 +327,7 @@ parse_args() {
326327
esac
327328
# :command.required_args_filter
328329
# :command.required_flags_filter
329-
# :command.parse_args_while
330+
# :command.parse_requirements_while
330331
while [[ $# -gt 0 ]]; do
331332
key="$1"
332333
case "$key" in
@@ -337,7 +338,7 @@ parse_args() {
337338
;;
338339

339340
* )
340-
# :command.parse_args_case
341+
# :command.parse_requirements_case
341342
echo -e "invalid argument: $key"
342343
exit 1
343344
;;
@@ -346,8 +347,8 @@ parse_args() {
346347
done
347348
}
348349

349-
# :command.parse_args
350-
configly_set_parse_args() {
350+
# :command.parse_requirements
351+
configly_set_parse_requirements() {
351352
# :command.fixed_flag_filter
352353
case "$1" in
353354
--version )
@@ -362,6 +363,7 @@ configly_set_parse_args() {
362363
;;
363364

364365
esac
366+
# :command.environment_variables_filter
365367
# :command.command_filter
366368
action="set"
367369
# :command.required_args_filter
@@ -381,7 +383,7 @@ configly_set_parse_args() {
381383
exit 1
382384
fi
383385
# :command.required_flags_filter
384-
# :command.parse_args_while
386+
# :command.parse_requirements_while
385387
while [[ $# -gt 0 ]]; do
386388
key="$1"
387389
case "$key" in
@@ -392,7 +394,7 @@ configly_set_parse_args() {
392394
;;
393395

394396
* )
395-
# :command.parse_args_case
397+
# :command.parse_requirements_case
396398
if [[ ! ${args[key]} ]]; then
397399
args[key]=$1
398400
shift
@@ -409,8 +411,8 @@ configly_set_parse_args() {
409411
done
410412
}
411413

412-
# :command.parse_args
413-
configly_get_parse_args() {
414+
# :command.parse_requirements
415+
configly_get_parse_requirements() {
414416
# :command.fixed_flag_filter
415417
case "$1" in
416418
--version )
@@ -425,6 +427,7 @@ configly_get_parse_args() {
425427
;;
426428

427429
esac
430+
# :command.environment_variables_filter
428431
# :command.command_filter
429432
action="get"
430433
# :command.required_args_filter
@@ -436,7 +439,7 @@ configly_get_parse_args() {
436439
exit 1
437440
fi
438441
# :command.required_flags_filter
439-
# :command.parse_args_while
442+
# :command.parse_requirements_while
440443
while [[ $# -gt 0 ]]; do
441444
key="$1"
442445
case "$key" in
@@ -447,7 +450,7 @@ configly_get_parse_args() {
447450
;;
448451

449452
* )
450-
# :command.parse_args_case
453+
# :command.parse_requirements_case
451454
if [[ ! ${args[key]} ]]; then
452455
args[key]=$1
453456
shift
@@ -461,8 +464,8 @@ configly_get_parse_args() {
461464
done
462465
}
463466

464-
# :command.parse_args
465-
configly_list_parse_args() {
467+
# :command.parse_requirements
468+
configly_list_parse_requirements() {
466469
# :command.fixed_flag_filter
467470
case "$1" in
468471
--version )
@@ -477,11 +480,12 @@ configly_list_parse_args() {
477480
;;
478481

479482
esac
483+
# :command.environment_variables_filter
480484
# :command.command_filter
481485
action="list"
482486
# :command.required_args_filter
483487
# :command.required_flags_filter
484-
# :command.parse_args_while
488+
# :command.parse_requirements_while
485489
while [[ $# -gt 0 ]]; do
486490
key="$1"
487491
case "$key" in
@@ -492,7 +496,7 @@ configly_list_parse_args() {
492496
;;
493497

494498
* )
495-
# :command.parse_args_case
499+
# :command.parse_requirements_case
496500
echo -e "invalid argument: $key"
497501
exit 1
498502
;;
@@ -519,7 +523,7 @@ initialize() {
519523
# :command.run
520524
run() {
521525
declare -A args
522-
parse_args "$@"
526+
parse_requirements "$@"
523527

524528
if [[ $action == "set" ]]; then
525529
if [[ ${args[--help]} ]]; then

examples/custom-includes/download

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ my_extra_function() {
7171

7272
# :command.command_functions
7373

74-
# :command.parse_args
75-
parse_args() {
74+
# :command.parse_requirements
75+
parse_requirements() {
7676
# :command.fixed_flag_filter
7777
case "$1" in
7878
--version )
@@ -87,11 +87,12 @@ parse_args() {
8787
;;
8888

8989
esac
90+
# :command.environment_variables_filter
9091
# :command.command_filter
9192
action="root"
9293
# :command.required_args_filter
9394
# :command.required_flags_filter
94-
# :command.parse_args_while
95+
# :command.parse_requirements_while
9596
while [[ $# -gt 0 ]]; do
9697
key="$1"
9798
case "$key" in
@@ -102,7 +103,7 @@ parse_args() {
102103
;;
103104

104105
* )
105-
# :command.parse_args_case
106+
# :command.parse_requirements_case
106107
if [[ ! ${args[source]} ]]; then
107108
args[source]=$1
108109
shift
@@ -134,7 +135,7 @@ initialize() {
134135
# :command.run
135136
run() {
136137
declare -A args
137-
parse_args "$@"
138+
parse_requirements "$@"
138139

139140
if [[ ${args[--version]} ]]; then
140141
version_command

0 commit comments

Comments
 (0)