program-utils
A module to parse arguments and to provide easy configuration file.
1.1.0 Changelog
- It is now possible to prevent the value parsing with the config parser using
:=
assignment. - Linted README.md and modified some things in it.
Parse config
The config style is inspired from the windows INI file. But it works slightly differently.
const programUtils = ;let parsedConfig = programUtils;
Exemples :
# example.conf foo = barspam = truet = falsenbr = 5.5 # you can do a comment with an '#' on a blank line # => {foo:"bar", spam: true, t: false, nbr:5.5}
Every value that is not true, false, or a number is a string.
But you can do subobjects with the syntax [obj subobj]
# example.conf var_in_global = bar [test]var_in_test_in_global = bar [test comp]var_in_comp_in_test_in_global = bar # => {var_in_global:"bar",test:{var_in_test_in_global:"bar",comp:{var_in_comp_in_test_in_global:"bar"}}}
Notes and other examples
# example.conf hey1 = this is a hey1test = is this going to be overridden ? [test]res = yes !# if you create a subobject with the same name as an existing property, the property will be overriden by the # subobject name []hey2 = this is a hey2# you can return to global scope with [], so "hey2" will be along "hey1" [test]# this case will not override the current test, so there is already "res" defined here []test = not a subobject anymore# if test is reassigned as a property in parent scope, the test subobject will be overriden also [hey this is very cool]# this scope is the "cool" object in "very" in "is" in "this" in "hey" in global [hey this is]# this scope targetting "is" in "this" in "hey" in global []no_space= spaceto_much_space= space# it's possible to have no space around the equal sign, or one (but more will add a space in the value) # so in this example "no_space" is equal to "space" but "to_much_space" is equal to " space"
You can also force the config parser to not parse the value using :=
instead of =
:
# Will be equal to 45 as a number my_number=45# Will be equal to "45" as a string my_other_number:=45 # It works also with booleans a_bool=truestring_bool:=true
Build a config
You might want to build a config, for example the first time your program is run, in this case you can use the config builder.
const programUtils = ; let builder = ; builder // this will add a [hey] // this will add "t=t" // this will jump a line // .toString(); -- Returns a string containing the config content ; // Directly writes config into a file /* outputs in config.conf : [hey] t=t # this is cool */
Args parser
If you want to parse args you can do this by doing for example :
const programUtils = ; let argsParser = ; let arr = '-v'; let args = argsParser // Adds a mono char flag to be recognized // Adds a second//.setSourceArray(arr, startingIndex) Sets the source array and its starting point // Adds a string arg ;
So now when the program is launched it will result according to args passed For example
args => "-v" args => "-vh" args => "-vhp" args => "-vh=45" args => "-vh=100,bar" args => "-h=100 -v=bar" args => "-h=100 -v=bar --help=true" args => "-h=100 -v=bar --help=true --wtf"
Here it is
License
MIT