Skip to content

Commit d879ae1

Browse files
committed
- Config Library: Add ability to set default value in config_get
1 parent 833e0b9 commit d879ae1

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

examples/config/src/get_command.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ else
77
echo "No such key: $key"
88
fi
99

10-
# Example of how to assign the config value to a variable:
11-
# result=$(config_get "${args[key]}")
12-
# echo $result
10+
# Or, assign a default value if value not found
11+
config_get "$key" "the default value"
12+
13+
# Or, assign the result to a variable
14+
result=$(config_get "$key")
15+
echo $result
1316

examples/config/src/lib/config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ config_init() {
1717
}
1818

1919
## Get a value from the config.
20-
## Usage: result=$(config_get hello)
20+
## Usage: result=$(config_get key) -or- result=$(config_get key "default value")
2121
config_get() {
2222
local key=$1
23+
local value=$2
2324
local regex="^$key *= *(.+)$"
24-
local value=""
2525

2626
config_init
2727

lib/bashly/libraries/config/config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ config_init() {
1717
}
1818

1919
## Get a value from the config.
20-
## Usage: result=$(config_get hello)
20+
## Usage: result=$(config_get key) -or- result=$(config_get key "default value")
2121
config_get() {
2222
local key=$1
23+
local value=$2
2324
local regex="^$key *= *(.+)$"
24-
local value=""
2525

2626
config_init
2727

spec/approvals/examples/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ saved: hello = world
3333
saved: bashly = works
3434
+ ./configly get hello
3535
world
36+
world
37+
world
3638
+ ./configly get invalid_key
3739
No such key: invalid_key
40+
the default value
41+
3842
+ ./configly list
3943
; comments are allowed
4044
hello = world

0 commit comments

Comments
 (0)