From 0a942fd0b7d5c399d443a0cf07046885393eaeed Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Fri, 2 Aug 2024 15:09:36 +0100 Subject: [PATCH] Use a default --merge-delim of \003 (hex) when --duplicates-merge is used. --- README.md | 25 +++++++++++++++++++++++++ parse-ini | 16 +++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 84a4206..a9b2858 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,31 @@ Note that there are some extra rules that come into play when leaving out the 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 - mixed case, all lowercase or all uppercase, and with any prefix or delimiter you prefer. diff --git a/parse-ini b/parse-ini index 4cddcae..2406946 100755 --- a/parse-ini +++ b/parse-ini @@ -1,5 +1,5 @@ #!/bin/bash -# Bash INI file parser version: 0.1.2 +# Bash INI file parser version: 0.1.3 # Copyright (c) 2019-2024: # Darren 'Tadgy' Austin # 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 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, - value will result. The concatinated values are not separated by any - characters, unless '--merge-delim' is specified. Booleans are the - exception to this behaviour, as the latter bool will always override an - earlier setting. + value will result. The concatinated values are separated by the + character(s) specified by '--merge-delim' (default hex \003). + Booleans are the exception to this behaviour, as the latter bool will + always override an earlier setting. --global-name The name of the 'global' section used when declaring the arrays. Only alphanumerics and "_" may be used with this option, which cannot be empty. @@ -216,8 +216,9 @@ parser_help() { --lowercase-keys Convert the key name to lowercase. --merge-delim - When '--duplicates-merge' is used, this sets the delimiters between each - of the merged values. + When '--duplicates-merge' is used, this sets the delimiter characters + between each of the merged values. The default character is hex \003, + which is known as the "end of text" marker. --no-booleans Normally, the parser interprites the presence of a key without an 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 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 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 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".