Senzu, convenient CLI options parser
Introduction
Senzu is lightweight tool without dependency for CLI options parsing. Options are not typed and the parser don't throw error. Senzu is a parser, not a checker, for greater flexibility it is your responsibility to check the input of the user.
Examples
cmdcmd -hcmd -hhhhcmd --optimize-level 2cmd -O2 path/to/srccmd --includes ../inc0 ../inc1 -- path/to/srccmd -I../inc0 -I ../inc2 path/to/src -o a.exe
Configuration for this example:
const senzu = const options = options"paths"; // access an option, undefined if the user dont specify itoptionswanderers; // access other args (like "path/to/src" in examples above)
Let's check the respectives outputs:
'help': true 'help': true 'optimize-level': 2 wanderers: 'path/to/src' 'optimize-level': 2 wanderers: 'path/to/src' 'includes': '../inc0' '../inc1' wanderers: 'path/to/src' 'includes': '../inc0' '../inc2' 'output': 'a.exe'
Features
Prototype of the senzu
function:
List of parameters available for an option template:
text
: The text that appears in the "help" command.ch
(optional): A character alias which will be used with a single-
. Character can be directly followed by its corresponding value like-I../inc
and can only grab one argument forward.atomic
(optional): The option character can be grouped with other atomics characters and will not grab following arguments. It handle option of the form-hVfz
for example.array
(optional): The option will be an array (it will grab all following arguments if the option is specified like--includes
but not like-I
). It isn't compatible withatomic
.parse
(optional): A parse function to transform arguments. It is good practice to not use it as a checker and to not throw errors. It has no effect withatomic
.
Help message generation:
use senzu.helpmessage
to generate a basic help message like:
Usage: cmd [options] <files>
Options:
--includes, -I set includes paths
--output, -o set output path
--optimize-level, -O set optimization level
--help, -h show help
Full example:
const options = ; const config = usage: "cmd [options] <files>" // set to false to desactivate indent_level: 0 line if optionshelp === true senzu; // config can be ommited
Notes
If an option isn't specified it will be undefined
.
If options specified by the user are not recognized they will be pushed in wanderers
, for example cmd -a -b path/to/src
will give ["-a", "-b", "path/to/src"]
.
If you want to throw an error in this case, loop and check in the array.
wanderers
is reserved option name
Options are recognized using object as hashmap.