A simple parser for config files.
-
Fork the project on GitHub and clone your fork locally.
$ git clone git://github.com/username/config_parser.git $ cd config_parser $ git remote add upstream https://github.com/acamino/config_parser.git
-
Run the setup script.
$ bin/setup
-
Make sure the tests succeed.
$ rake test
A config file is a plain text file which must obey the following rules:
- A property is composed of a key and a value separated by the
=
character - A comment is any text prefixed by the
#
character - You can express
true
values with true, on, or yes - You can express
false
values with false, off, or no
You can use this library like this:
When the configuration file has the right format, the parsing mechanism produces a hash with it.
# File's path: /path/to/config
# Configuration file
host = test.com
server_id= 55331
config = ParserConfig.open_and_parse('/path/to/config')
=> {:host=>"test.com", :server_id=>55331}
In the other hand, when the config file is has an invalid format, calling the parsing method will throw a custom exception. Thus, you can handle this exception gracefully.
# File's path: /path/to/config
# Invalid configuration file
host = test.com
server_id 55331
config = ParserConfig.open_and_parse('/path/to/config')
=> ConfigParser::Parser::InvalidContentError: invalid content "server_id 55331"
Edits and enhancements are welcome. Just fork the repository, make your changes and send me a pull request.
The code in this repository is licensed under the terms of the
MIT License.
Please see the LICENSE file for details.