Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase on upstream/master and add new features. #9

Open
wants to merge 6 commits into
base: next
Choose a base branch
from

Conversation

jcass77
Copy link

@jcass77 jcass77 commented Mar 24, 2015

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

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

Tests passed!

Tests passed!

Tests passed!
@HeinrichAD
Copy link

@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?

@jcass77
Copy link
Author

jcass77 commented Apr 21, 2016

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.

@HeinrichAD
Copy link

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 ####

@rudimeier
Copy link
Owner

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

@HeinrichAD
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants