Use a default --merge-delim of \003 (hex) when --duplicates-merge is used.

This commit is contained in:
Darren 'Tadgy' Austin 2024-08-02 15:09:36 +01:00
commit 0a942fd0b7
2 changed files with 34 additions and 7 deletions

View file

@ -208,6 +208,31 @@ Note that there are some extra rules that come into play when leaving out the
example. example.
When the option `--duplicates-merge` is used with a default `--merge-delim`
setting, each record in the array element will be separated by the character
hex \003.
For example, whith an `example.ini` containing:
```
Key = Value 1
Key = Value 2
Key = Value 3
```
The values within the array element will be separated by \003 (hex).
The output of `parse-ini` will not directly show the delimiter character in
the terminal, but it *is* there when used in a script:
```bash
eval `/path/to/parse-ini --duplicates-merge example.ini`
awk -F $'\003' '{ print $2 }' <<<"${INI_global[Key]}"
```
Would output the second value:
```
Value 2
```
Using these options allows you to access the arrays in your preferred style - Using these options allows you to access the arrays in your preferred style -
mixed case, all lowercase or all uppercase, and with any prefix or delimiter mixed case, all lowercase or all uppercase, and with any prefix or delimiter
you prefer. you prefer.

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Bash INI file parser version: 0.1.2 # Bash INI file parser version: 0.1.3
# Copyright (c) 2019-2024: # Copyright (c) 2019-2024:
# Darren 'Tadgy' Austin <darren (at) afterdark.org.uk> # Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3. # Licensed under the terms of the GNU General Public License version 3.
@ -198,10 +198,10 @@ parser_help() {
If a duplicate key for a specific section is found, the normal behaviour If a duplicate key for a specific section is found, the normal behaviour
is to have the latter instance of the key overwrite the value of the is to have the latter instance of the key overwrite the value of the
earlier. With this option, the keys are merged, and a new, concatinated, earlier. With this option, the keys are merged, and a new, concatinated,
value will result. The concatinated values are not separated by any value will result. The concatinated values are separated by the
characters, unless '--merge-delim' is specified. Booleans are the character(s) specified by '--merge-delim' (default hex \003).
exception to this behaviour, as the latter bool will always override an Booleans are the exception to this behaviour, as the latter bool will
earlier setting. always override an earlier setting.
--global-name <name> --global-name <name>
The name of the 'global' section used when declaring the arrays. Only The name of the 'global' section used when declaring the arrays. Only
alphanumerics and "_" may be used with this option, which cannot be empty. alphanumerics and "_" may be used with this option, which cannot be empty.
@ -216,8 +216,9 @@ parser_help() {
--lowercase-keys --lowercase-keys
Convert the key name to lowercase. Convert the key name to lowercase.
--merge-delim --merge-delim
When '--duplicates-merge' is used, this sets the delimiters between each When '--duplicates-merge' is used, this sets the delimiter characters
of the merged values. between each of the merged values. The default character is hex \003,
which is known as the "end of text" marker.
--no-booleans --no-booleans
Normally, the parser interprites the presence of a key without an Normally, the parser interprites the presence of a key without an
associated value as a boolean. Keys which are proceeded by "no_" are associated value as a boolean. Keys which are proceeded by "no_" are
@ -281,6 +282,7 @@ parse_ini() {
local DECLARE_SCOPE="-g" # The scope given in the array definitions. "-g" = global scope, "-l" = local scope, "-x" = export values. local DECLARE_SCOPE="-g" # The scope given in the array definitions. "-g" = global scope, "-l" = local scope, "-x" = export values.
local DUPLICATES_MERGE="0" # Whether to merge latter duplicate key's values with earlier key's values. 0 = don't merge, 1 = do merge. local DUPLICATES_MERGE="0" # Whether to merge latter duplicate key's values with earlier key's values. 0 = don't merge, 1 = do merge.
local KEYVALUE_DELIM="=" # Delimiter between key and value. Must be a single character. local KEYVALUE_DELIM="=" # Delimiter between key and value. Must be a single character.
local MERGE_DELIM=$'\3' # The delimiter between each of the merged values when --duplicates-merge is used.
local REPEAT_SECTIONS="0" # Whether to allow section names to repeat. 0 = no repeats, 1 = allow repeats. local REPEAT_SECTIONS="0" # Whether to allow section names to repeat. 0 = no repeats, 1 = allow repeats.
local SQUASH_SPACES="1" # Whether to squash multiple consecutive blanks into a single space. 0 = don't squash, 1 = do squash. local SQUASH_SPACES="1" # Whether to squash multiple consecutive blanks into a single space. 0 = don't squash, 1 = do squash.
local TEXTUAL_BOOLEANS="0" # Whether to use "false" and "true" for booleans. 0 = use "0" and "1", 1 = use "false" and "true". local TEXTUAL_BOOLEANS="0" # Whether to use "false" and "true" for booleans. 0 = use "0" and "1", 1 = use "false" and "true".