Demonstrates how to add functions for reading and writing INI files.
Note that this is a low-level library that provides the reading and writing functions for the config library.
This example was generated with:
$ bashly init
# ... now edit src/bashly.yml to match the example ...
$ bashly add ini
$ bashly generate
# ... now edit all files in the src folder ...
$ bashly generate
Running the bashly add ini
command simply added the
src/lib/ini.sh file, which includes functions for
reading and writing values from an INI file.
See the files in the src folder for usage examples.
name: configly
help: Sample application that uses the config functions
version: 0.1.0
commands:
- name: list
alias: l
help: Show the entire config file
- name: get
alias: g
help: Read a value from the config file
args:
- name: key
required: true
help: Config key
examples:
- configly get hello
- configly get user.name
- name: set
alias: s
help: Save a value in the config file
args:
- name: key
required: true
help: Config key
- name: value
required: true
help: Config value
examples:
- configly set hello world
- configly set user.email me@example.com
- name: del
alias: d
help: Remove a value from the config file
args:
- name: key
required: true
help: Config key
examples:
- configly del hello
- configly del user.name
; comments are allowed, sections are optional
hello = world
bashly = works
[options]
name = value for options.name
path = value for options.path
[user]
name = value for user.name
email = value for user.email
# Using the standard library (lib/ini.sh) to show a value from the config
ini_load config.ini
key="${args[key]:-}"
value=${ini[$key]:-}
if [[ "$value" ]]; then
echo "$key = $value"
else
echo "No such key: $key"
fi
# Using the standard library (lib/ini.sh) to show the entire config file
ini_load config.ini
ini_show
## Or iterate through keys manually
# for key in $(ini_keys); do
# echo "$key = ${ini[$key]}"
# done
# Using the standard library (lib/ini.sh) to store a value to the config
ini_load config.ini
key="${args[key]}"
value="${args[value]}"
ini["$key"]="$value"
ini_save saved.ini
cat saved.ini
# Using the standard library (lib/ini.sh) to delete a value from the config
ini_load config.ini
key="${args[key]}"
unset "ini[$key]"
ini_save saved.ini
cat saved.ini
configly - Sample application that uses the config functions
Usage:
configly COMMAND
configly [COMMAND] --help | -h
configly --version | -v
Commands:
list Show the entire config file
get Read a value from the config file
set Save a value in the config file
del Remove a value from the config file
Options:
--help, -h
Show this help
--version, -v
Show version number
bashly = works
hello = WORLD
[options]
name = value for options.name
path = value for options.path
[user]
email = value for user.email
name = value for user.name
bashly = works
hello = world
[options]
name = value for options.name
path = value for options.path
[user]
email = value for user.email
name = Megatron
hello = world
user.name = value for user.name
No such key: invalid_key
bashly = works
hello = world
[options]
name = value for options.name
path = value for options.path
[user]
name = value for user.name
bashly = works
hello = world
options.name = value for options.name
options.path = value for options.path
user.email = value for user.email
user.name = value for user.name