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

feature request, allow special characters for section and var names #2

Open
yixinrock opened this issue Sep 22, 2012 · 7 comments
Open

Comments

@yixinrock
Copy link

subject changed by Rudi to match the feature request of post #2

@yixinrock
Copy link
Author

sorry , i use ./read_ini.sh to run it instead of .read_ini , and i cant close this issue.
but i just use it to read application.ini which has a section [staging : production] , then the function exit with
Error: Invalid line: 58: [staging : production]
Can we built a more smart lib to deal with this ?

@rudimeier
Copy link
Owner

Allowing such special characters is not easy possible because read_ini's interface wants to use these strings as bash variables.
However if you check out the next branch you can try another interface using bash associative arrays (requires bash >=4.0). I'd written this 2 years ago but the documentations is not up-to-date yet.
It should work like this:

$ cat /tmp/bla.ini
a="xyz"
b="uvw"
[staging : production]
a="mnl"

$ read_ini /tmp/bla.ini
$ echo "${INI["a"]}"
xyz
$ echo "${INI["a"]}"
xyz
$ echo "${INI["b"]}"
uvw
$ echo "${INI["staging : production__a"]}"
mnl

hope that helps,
Rudi

@rudimeier
Copy link
Owner

Forgot to note, you need to declare $INI before calling read_ini:
unset INI
declare -A INI
read_ini test1.ini

see also the examples in the test directory.

@yixinrock
Copy link
Author

Thank your for reminding :P , i will try to use it .

@jcass77
Copy link

jcass77 commented Feb 27, 2015

Would it perhaps be an option to be less strict when reading the sections and values, and just convert special characters to underscore before creating the variables? This is already being done for '.' correct?

https://github.com/rudimeier/bash_ini_parser/blob/8fb95e3b335823bc85604fd06c32b0d25f2854c5/read_ini.sh#L232-237

@jcass77
Copy link

jcass77 commented Mar 1, 2015

I switched to using the 'next' branch which supports special characters in the section names.

However I came across a complex example in one of the configuration files that I am parsing that is still not handled as expected:

enabled = true

# top-level directories for browsing, as <name> <uri>
directories =
    Albums                  local:directory?type=album
    Artists                 local:directory?type=artist
    Composers               local:directory?type=artist&role=composer
    Folders                 local:directory:
    Genres                  local:directory?type=genre
    Performers              local:directory?type=artist&role=performer
    Release Years           local:directory?type=date&format=%25Y
    Tracks                  local:directory?type=track
    Last Week's Updates     local:directory?max-age=604800
    Last Month's Updates    local:directory?max-age=2592000

# database connection timeout in seconds
timeout = 10

In this example, each line is handled as a separate parameter value pair instead of forming part of directories. The script also fails on Folders local:directory:, expecting an '=' at the end. Any ideas on how to deal with the above?

@jcass77
Copy link

jcass77 commented Mar 24, 2015

I've created pull request #9 to add support for multiline values, and added a few other features similar to what can be found in Python's RawConfigParser.

All of the tests still run and I've added a few new ones. @rudimeier will you please review?

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

No branches or pull requests

3 participants