From 161a886e1907cc53aa003129990a776e4e328239 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Tue, 16 Jul 2019 21:38:27 +0100 Subject: [PATCH] Progress.. --- SPEC | 16 ++- TODO | 3 +- bits/comments.ini | 4 +- bits/readline/readline | 22 ++-- bits/sections.ini | 7 ++ parse_ini | 271 ++++++++++++++++++++++++----------------- 6 files changed, 196 insertions(+), 127 deletions(-) create mode 100644 bits/sections.ini diff --git a/SPEC b/SPEC index 5c02a28..ed9f0e7 100644 --- a/SPEC +++ b/SPEC @@ -1,6 +1,8 @@ -Blank lines are ignored. -Lines starting with # and ; (configurable; after leading whitespace removal) are treated as comments. - - Comments must appear on their own line. +General file format +------------------- +* Blank lines are ignored. +* Lines starting with # and ; (configurable), after leading whitespace removal, are treated as comments. + - Comments must appear on their own line. Values can optionally be bookmarked with single or double quotes. - If quotes are to be used, they must be the first and last characters of the value - Occurances of the bookending quotes to be used within the value must be \ escaped. ??? @@ -10,3 +12,11 @@ Values can be continued by use of \ in the last column. - Subsequent lines are subject to leading whitespace removal as normal. - Comments are not recognised on subsequent lines - they are treated as part of the value. Escaping of shell special characters is not required. ??? + + +[section] format +---------------- +* Section names must only be comprised of alphanumeric characters, plus _.-+ +* The .-+ characters in section names will be converted to _ +* Section names are case sensitive (unless --ignore-case? is used), so 'Foo' and 'foo' are different sections. +* Whitespace is ignored before and after the section name. diff --git a/TODO b/TODO index 2a2623f..175a15f 100644 --- a/TODO +++ b/TODO @@ -10,4 +10,5 @@ This option should not be used except for debugging. -Have the parser accept a filename of '-' to indicate it should read from stdin. +* Allow changing the characters accepted as comments in the INI file. +* Allow the key/value deliminator to be more than one character? diff --git a/bits/comments.ini b/bits/comments.ini index 4d5148a..7e13912 100644 --- a/bits/comments.ini +++ b/bits/comments.ini @@ -35,8 +35,8 @@ Valid 1 Valid 2 Valid 3 Valid 4 - [ header 2 ] + [ header 2 ] Valid 5 -[ header 3 ] +[ header 3 ] [header_____4____] [ $%"%^ ] diff --git a/bits/readline/readline b/bits/readline/readline index 26524c7..bf6b94a 100755 --- a/bits/readline/readline +++ b/bits/readline/readline @@ -4,19 +4,19 @@ shopt -s extglob exec {FD} -Parse an ini file into environment variables which can be used natively in Bash. +parser_getopts() { + while [ ! -z "$1" ]; do + case "$1" in + -h|-help|--help) + parser_help + return 0 + ;; + -v|-version|--version) + parser_version + return 0 + ;; + --) + # Stop option processing. + break + ;; + -*|--*) + echo "${0##*/}: invalid option: $1" + return 1 + ;; + esac + done +} + +parser_help() { +#........1.........2.........3.........4.........5.........6.........7.........8 + cat <<-EOF + Usage: ${0##*/} [options] + Parse an ini file into environment variables which can be used natively in Bash. + + Options: + -e , --envvar= + The prefix of the environment variables set by the parser. + The default is 'INI'. + # -p , --prefix= Set the prefix to all environment variables set by the parser. A single + # underscore '_' is automatically added to the end. + # Default: INI -Options: - -e , --envvar= - The prefix of the environment variables set by the parser. - The default is 'INI'. -d , --envdelim= The character(s) to use as a deliminator between the environment variable and the section name. This is used when creating the environment @@ -58,19 +85,21 @@ Options: deliminator at all, use -d '' or --envdelim=''. The default deliminator is a single underscore '_' ??? -i, --implied-boolean - Options usually require a parameter (after the =) in order to be set. - With this option, any option without a parameter contained in the ini file - if assumed to be a boolean 'true' and set accordingly. Likewise, any option - preceeded with 'no_' (eg: no_foo) will set the option 'foo' to boolean 'false'. + Options usually require a value (after the =) in order to be set. + With this option, any key without a value contained in the ini file + if assumed to be a boolean 'true' and set accordingly. Likewise, any key + preceeded with 'no_' (eg: no_foo) will set the value of 'foo' to boolean 'false'. Cannot be used with --no-boolean. -c, --case-sensitive Be case sensitive with section names and properties. Section names and property names will be used as is - no translation. +-d, --delim +The deliminator between the key and value. Must be a single character. Default = -g, --global-name INI files can contain an optional implied "global" section - where there are property names/values before any [section] header. This option specified what section name the implied "global" section should be given - in the environment variables which are set. The default is 'GLOBAL'. + in the environment variables which are set. The default is 'global'. -l, --lowercase Usually, environment variables are converted to all uppercase before being set. @@ -92,9 +121,9 @@ Options: Usually sections with the same name will have their options merged, and duplicate option values will overwrite previous ones. - - --test - Test/validate the ini file by running it through the parser. Testing the +# -c, --check-only Only validate the ini file, don't parse it into the environment + --check + Check/validate the INI file by running it through the parser. Testing the ini file will report any problems or syntax errors in the file, but will not set up the environment variables as would happen in normal parsing. Any parse errors are reported to stderr. When combined with the --debug @@ -107,11 +136,23 @@ Options: Show (this) help. -v, --version Show version and copyright information. + # -b, --booleans Allow 'yes', 'true', 'on', 'no', 'false', 'off' to be used as values + # and interpited as boolean values. 'yes', 'true', 'on' set option value to "1". + # 'no', 'false', 'off' set option value to "0". + # -?, --???? Interprite the presense of an option name without any value as a boolean + # 'true', and no_