bash_ini_parser icon indicating copy to clipboard operation
bash_ini_parser copied to clipboard

Rebase on upstream/master and add new features.

Open jcass77 opened this issue 9 years ago • 5 comments

Add confifuration parameter to optionally allow empty values.

Add support to handle indented comments and multi-line values.

Additional test scripts to check Python config parser compatibility https://docs.python.org/3/library/configparser.html

jcass77 avatar Mar 24 '15 07:03 jcass77

@jcass77 First nice features.

But I got one little problem with your changes while I was adding an array support (like in php). You have added that you can you '=' and ':' like in Test10.ini. It seems that you will use the last one you find in the line.

Now it is not possible to have for example URLs in your INI file anymore.

Example:
URL = "http://www.example.com/~username"

Error message:

../read_ini.sh: eval: line 343: unexpected EOF while looking for matching `"'
../read_ini.sh: eval: line 344: syntax error: unexpected end of file
pair: 'second_section__URL = "http' = '//www.example.com/~username"'
../read_ini.sh: line 372: second_section__URL = "http: syntax error: operand expected (error token is ""http")

I think the right way is to use the first character of '=' or ':' unless this character is in a quotation marks section.

Example:
URL = "http://www.example.com/~username"
-> key: URL
-> value: http://www.example.com/~username
URL : "http://www.example.com/~username="
-> key: URL
-> value: http://www.example.com/~username=
"URL:123" : "http://www.example.com/~username"
-> key: URL:123
-> value: http://www.example.com/~username
'URL:123' = http://www.example.com/~username
-> key: URL:123
-> value: http//www.example.com/~username
'URL:123' http://www.example.com/~username
-> Not valid

What do you think is the right way?

HeinrichAD avatar Apr 21 '16 00:04 HeinrichAD

You will probably have to change the regular expressions in RE_REG and RE_OPT to cater for this.

I don't have an answer for you off hand, but you could perhaps use a regular expression debugger to try a few variations until you come up with something that works for the examples above.

jcass77 avatar Apr 21 '16 04:04 jcass77

I had some time this weekend to look into this problem.

My intention was to implement the support for arrays like in php. Info and example

The problem now is that the hole syntax was changed. Before: INI__SECTION__VAR Now: INI[SECTION__VAR] <-- Array I understand the reason for this. You want to support keys with spaces and other special characters. But bash does not support nested arrays and my changes are not really possible.

Is there really a reason to support keys with special characters? Which ini file does use this?

If I find time in the next weeks I will probably look for a solution again.


To share some knowledge:

Declaring a global array: declare -g -A array_name

To clear a whole array unset is perfectly fine.

To support array with the old syntax's something like this could be possible: IMPORTANT: The following code is not tested. This is only an idea how to handle the problem.

# Array?
# Bash Version 4?
if (( "${BASH_VERSION:0:1}" >= 4 ))
then
    if [[ ! -z "$(echo "${VARNAME:(-1)}")" ]]
    then
        local ARRAY_BASE=${VARNAME%[*}

        # Exist character '['?
        if [ "${ARRAY_BASE}" != "${VARNAME}" ]
        then
            # Is it really an array and not a section?
            # Maybe this query is not even necessary 
            #    due to the section query in the code before.
            if [[ ! -z "${ARRAY_BASE%[*}" ]]
            then
                # Declare global array.
                # New in bash Version 4 if I am right.
                eval "declare -g -A ${ARRAY_BASE}"

                if [ "${VARNAME:(-2)}" == "[]" ]
                then
                    ## Normal Array - No Dictionary (Key-Value Array)
                    eval "VARNAME=${ARRAY_BASE}[\${#${ARRAY_BASE}[@]}]"
                fi
            fi
        fi
    fi
fi

If I am not wrong the right place for this code should be right after the following code snippet:

# Construct variable name:
# ${VARNAME_PREFIX}__$SECTION__$VAR
# Or if not in a section:
# ${VARNAME_PREFIX}__$VAR
# In both cases, full stops ('.') are replaced with underscores ('_')
if [ -z "$SECTION" ]
then
    VARNAME=${VAR//./_}
else
    VARNAME=${SECTION}__${VAR//./_}
fi

#### HERE ####

HeinrichAD avatar Apr 24 '16 16:04 HeinrichAD

Sorry for not responding for a long time. I haven't looked into this pull request yet.

@HeinrichAD the array version of bash_ini_parser is already there in the "next" branch. git checkout next

rudimeier avatar Apr 24 '16 16:04 rudimeier

@rudimeier thanks for the information. I will check this later.

HeinrichAD avatar Apr 24 '16 18:04 HeinrichAD