# argv.js Simple argv parser ## Motivation I needed a new argv parser for a quick and dirty project I was working on and evaluating and selecting the proper existing parser and then learning its API, quirks and adapting the architecture to it seemed to be more complicated, require more effort and far less fun than putting together a trivial parser myself in a couple of hours. This code is an evolution of that parser. ## Features - Simple - Supports both the _option_ (a-la `find`) and _command_ (a-la `git`) paradigms - Nestable parsers can be nested as option/command handlers defining independent nested contexts - Option expansion `-abc` expands to `-a -b -c` if `-abc` is not defined - Option/command value passing implicit `-a 123` (requires definition or manual handling) or explicit `-a=123` - Environment variable option/command values env can control option defaults - Reasonable defaults - `-help` – generate and print help, - `-version` – print version, - `-` – stop argument processing, - Extensible: - Hooks for option value conversion _(XXX should this be implemented???)_ - Hooks for dynamic option/command handling - Customizable error and stop condition handling ## Planned Features - Run `-` scripts - Multiple option prefix support ## Contents - [argv.js](#argvjs) - [Motivation](#motivation) - [Features](#features) - [Planned Features](#planned-features) - [Contents](#contents) - [Installation](#installation) - [Basic usage](#basic-usage) - [Configuration](#configuration) - [Options, commands and aliases](#options-commands-and-aliases) - [Help](#help) - [Value placeholders](#value-placeholders) - [Automatically defined values](#automatically-defined-values) - [`.doc`](#doc) - [`.usage`](#usage) - [`.version`](#version) - [`.license`](#license) - [`.examples`](#examples) - [`.footer`](#footer) - [More control over help...](#more-control-over-help) - [Nested parsers](#nested-parsers) - [Components and API](#components-and-api) - [`THEN`, `STOP` and `ERROR`](#then-stop-and-error) - [`Parser(..)`](#parser) - [`.then(..)`](#then) - [`.stop(..)`](#stop) - [`.error(..)`](#error) - [`.off(..)`](#off) - [`(..)`](#parser-1) - [Advanced parser API](#advanced-parser-api) - [`.print(..)` / `.printError(..)`](#print--printerror) - [`.handlerDefault(..)`](#handlerdefault) - [`.handleArgument(..)`](#handleargument) - [`.handleArgumentValue(..)`](#handleargumentvalue) - [`.handleErrorExit(..)`](#handleerrorexit) - [More...](#more) - [License](#license-1) ## Installation ```shell $ npm install ig-argv ``` ## Basic usage Create a script and make it runnable ```shell $ touch script.js $ chmod +x script.js ``` Now for the code ```javascript #!/usr/bin/env node // compatible with both node's and RequireJS' require(..) var argv = require('ig-argv') var parser = argv.Parser({ // option definitions... // ... }) .then(function(){ // things to do after the options are handled... // ... }) // run the parser... __filename == require.main && parser(process.argv) ``` Option definitions in a bit more detail ```javascript var parser = argv.Parser({ // XXX config... // basic quick-n-dirty option... '-b': '-basic', '-basic': function(opts, key, value){ // ... }, // basic value-getter option... '-value': { doc: 'Value option', arg: 'X | x', }, // full option settings... '-f': '-full', '-full': { // option doc (optional) doc: 'Option help', // option value to be displayed in help (optional) // NOTE: "attr" is used as a key to set the value if .handler // was not defined and is ingored in all other cases... arg: 'VALUE | attr', // value type handler (optional) type: 'int', // envioroment value (optional) env: 'VALUE', // default value (optional) default: 123, // required status (optional) required: false, // handler (optional) handler: function(opts, key, value){ // ... }, }, // command... // NOTE: the only difference between an option and a command is // the prefix ('-' vs. '@') that determines how it is parsed, // otherwise they are identical and can alias each other... '@cmd', '@command', '@command': { // ... }, // example command-option alias... '@help': '-help', // nested parser... '@nested': argv.Parser({ // ... }).then(function(){ // ... }), // ... }) ``` This will create a parser that supports the folowing: ```shell $ ./script.js --help $ ./script.js command $ ./script.js nested -h $ ./script.js -fb ``` ## Configuration This sections lists attributes and methods designed to be set/modified in `` passed to `Parser(..)`. Note that these attributes are the same attributes inherited by `` (parser instance) and are simply merged into the new instance created by `Parser(..)`, this there are no restrictions on what attributes/methods can be overloaded in this way but care must be taken when overloading elements that were not designed to be overloaded. ```javascript var parser = Parser({ }) ``` ### Options, commands and aliases ### Help `Parser` defines a default help generator via the `-h` and `-help` options. By default `-help` will output in the following format: ``` Options: - (, , ) ... Dynamic options: ... Commands: ... Examples: ...