2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# argv.js
  
						 
					
						
							
								
									
										
										
										
											2020-06-29 03:24:42 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Simple argv parser
							 
						 
					
						
							
								
									
										
										
										
											2020-06-29 03:24:42 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Motivation
  
						 
					
						
							
								
									
										
										
										
											2020-06-29 03:24:42 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:17:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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.  
							 
						 
					
						
							
								
									
										
										
										
											2020-06-29 03:24:42 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This code is an evolution of that parser.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Features
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Simple 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Supports both the _option_  (a-la `find` ) and _command_  (a-la `git` ) paradigms 
						 
					
						
							
								
									
										
										
										
											2020-07-01 13:55:52 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  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
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  Reasonable defaults 
						 
					
						
							
								
									
										
										
										
											2020-07-03 03:53:32 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  `-help`  –  generate and print help,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  `-version`  –  print version,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  `-`  –  stop argument processing,
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  Extensible: 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  Hooks for option value conversion _(XXX should this be implemented???)_ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  Hooks for dynamic option/command handling
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  Customizable error and stop condition handling
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Planned Features
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Run `<command>-<sub-command>`  scripts 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Multiple option prefix support 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 13:55:52 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<!--  XXX ### Alternatives  -->  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Contents
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  [argv.js ](#argvjs ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Motivation ](#motivation )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Features ](#features )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									-  [Planned Features ](#planned-features )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									-  [Contents ](#contents )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Installation ](#installation )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Basic usage ](#basic-usage )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Configuration ](#configuration )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										-  [Option/command configuration ](#optioncommand-configuration )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.handler(..)` ](#optionhandler )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.doc` ](#optiondoc )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.arg` ](#optionarg )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.type` ](#optiontype )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.env` ](#optionenv )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.default` ](#optiondefault )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.required` ](#optionrequired )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`<option>.valueRequired` ](#optionvaluerequired )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [Built-in options ](#built-in-options )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`-v` / `--version` ](#-v----version )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											-  [`-h` / `--help` ](#-h----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 )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										-  [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>(..)` ](#parser-1 )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-  [Advanced parser API ](#advanced-parser-api )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [`.print(..)` / `.printError(..)` ](#print--printerror )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [`.handlerDefault(..)` ](#handlerdefault )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [`.handleArgument(..)` ](#handleargument )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [`.handleArgumentValue(..)` ](#handleargumentvalue )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-  [`.handleErrorExit(..)` ](#handleerrorexit )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										-  [More... ](#more )
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									-  [License ](#license-1 )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 13:55:52 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Installation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```shell
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ npm install ig-argv
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Basic usage
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Create a script and make it runnable
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```shell
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:12:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ touch script.js
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ 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({
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// option definitions...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									.then(function(){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// things to do after the options are handled...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// run the parser...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								__filename == require.main
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									& &  parser(process.argv)
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Option definitions in a bit more detail
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								XXX make this a set of practical options and leave the attr explanation to later...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var parser = argv.Parser({
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// XXX config...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// alias, this tells the parser that '-b' is the same as '-basic'
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										'-b': '-basic',
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// basic quick-n-dirty option...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										'-basic': function(opts, key, value){
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// basic value-getter option...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										'-value': {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											doc: 'Value option',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											arg: 'X | x',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// full option settings...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										'-f': '-full',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										'-full': {
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// option doc (optional)
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											doc: 'Option help',
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// option value to be displayed in help (optional)
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 00:28:28 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// 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',
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// value type handler (optional)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											type: 'int',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// envioroment value (optional)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											env: 'VALUE',
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// default value (optional)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											default: 123,
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// required status (optional)
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											required: false,
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// 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({
							 
						 
					
						
							
								
									
										
										
										
											2020-07-17 00:01:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											}).then(function(){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											}),
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This will create a parser that supports the folowing:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```shell
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ./script.js --help 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ ./script.js --value 321
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ./script.js --value=321
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ ./script.js command
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ./script.js nested -h
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ./script.js -fb
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 18:49:55 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Configuration
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Parser(< spec > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `<spec>`  object is "merged" int the `<parser>`  instance overriding or extending 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								it's API/data.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `<parser>`  expects/handles the following data in the `<spec>`  object:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  the configuration attributes and methods   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									This sections lists attributes and methods designed to be set/modified in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									`<spec>`  passed to `Parser(..)` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Note that these attributes are the same attributes inherited by `<parser>` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  option/command definitions   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									The keys for these are prefixed either by `"-"`  for options or by `"@"`  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for commands and are either _objects_ , _functions_  or _parser_  instances.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									The only difference between an _option_  and a _command_  is that the former 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									are passed to the _script_  with a `"-"`  or `"--"`  prefix (by default) and 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									the later are passed by name without prefixes. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									In all other regards options and commands are the same.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  option/command aliases   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									An alias is an option/command key with a _string_  value.  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									That value _references_  a different option or command, i.e. is an 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									option/commnad name.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Looping (referencing the original alias) or dead-end (referencing non-existant 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									options) aliases are ignored.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Option/command configuration
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.handler(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Option handler.
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								'-option': function(opts, key, value){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// handle the option...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								or
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								'-option': {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									handler: function(opts, key, value){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// handle the option...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The handler gets called if the option is given or if it has a default value but was not given.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`opts`  contains the mutable list of arguments passed to the script starting with but not including the current argument. If the handler needs to handle it's own arguments it can modify this list in place and the _parser_  will continue from that state. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								One usecase for this would be and option handler that needs to handle it's arguemnts on its own.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`key`  is the actual normalized (`[<prefix-char>]<name-str>` ) option/command triggering the `.handler(..)` .  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This can be useful to identify the actual option triggering the handler when using aliases or if a single handler is used for multiple options, or when it is needed to handle a specific prefix differently (a-la `find` ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`value`  is the option value. A _value_  either ecplicitly passed (via `=`  syntax), implicitly parsed from the `argv`  via the `<option>.arg`  definition or `undefined`  otherwise. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								A handler can return one of the `THEN` , `STOP`  or `ERROR`  to control further parsing and/or execution.  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								(See: [`THEN`, `STOP` and `ERROR` ](#then-stop-and-error ) for more info.)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### `<option>.doc`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Option/command documentation string used in `-help` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.arg`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Option/command argument definition.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								arg: '< arg-name > '
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								arg: '< arg-name >  | < key > '
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If defined and no explicit value is passed to the option comand (via `=` ) then the _parser_  will consume the directly next non-option if present in `argv`  as a value, passing it to the `<option>.type`  handler, if defined, then the `<option>.handler(..)` , if defined, or setting it to `<key>`  otherwise.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Sets the option/command arument name given in `-help`  for the option and the key where the value will be written.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `<key>`  is not used if `<option>.handler(..)`  is defined.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.type`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Option/command argument type definition. The given type handler will be used to convert the option value before it is passed to the handler or set to the given `<key>` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Supported types:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `"int"`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `"float"`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `"number"`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `"string"`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `"date"`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Type handlers are defined in `Parser.typeHandlers`  or can be overwritten by `<spec>.typeHandlers` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.env`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If set, determines the environment variable to be used as the default value for option/command.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If this is set, the corresponding environment variable is non-zero and `<option>.handler(..)`  is defined, the handler will be called regardless of weather the option was given by the user or not.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.default`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Sets the default value for option/command's value.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If this is set to a value other than `undefined`  and `<option>.handler(..)`  is defined, the handler will be called regardless of weather the option was given by the user or not.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.required`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Sets weather the _parser_  should complain/err if option/command is not given.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<option>.valueRequired`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Sets weather the _parser_  should complain/err if option/value value is not given.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Built-in options
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `-v` / `--version`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This will output the value of `.version`  and exit.
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### `-h` / `--help`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								By default `-help`  will output in the following format:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< usage >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< doc >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Options:
							 
						 
					
						
							
								
									
										
										
										
											2020-07-05 00:05:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									< option-spec >  < option-val > 		
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												-  < option-doc > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												  (< opt-required > , < opt-default > , < opt-env > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Dynamic options:
							 
						 
					
						
							
								
									
										
										
										
											2020-07-05 00:05:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Commands:
							 
						 
					
						
							
								
									
										
										
										
											2020-07-05 00:05:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Examples:
							 
						 
					
						
							
								
									
										
										
										
											2020-07-05 00:05:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< footer >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All sections are optional and will not be rendered if they contain no data.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### Value placeholders
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All documentation strings can contain special placeholders that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								will get replaced with appropriate values when rendering help.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `$SCRIPTNAME`  replaced with the value of `.scriptName` , 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `$VERSION`  replaced with `.version` , 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `$LICENSE`  replaced with `.license` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### Automatically defined values
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								These values are set by the parser just before parsing starts:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `.script`  - full script path, usually this is the value of `argv[0]` , 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `.scriptName`  - basename of the script, 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `.scriptPath`  - path of the script. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								These will be overwritten when the parser is called.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.doc`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Script documentation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .doc = < string >  | < function > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `undefined` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.usage`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Basic usage hint.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .usage = < string >  | < function >  | undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `"$SCRIPTNAME [OPTIONS]"` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.version`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Version number.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .usage = < string >  | < function >  | undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If this is not defined `-version`  will print `"0.0.0"` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `undefined` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.license`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Short license information.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .usage = < string >  | < function >  | undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `undefined` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.examples`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .usage = < string >  | < list >  | < function >  | undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Example list format:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									[
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										[< example-code > , < example-doc > , ...],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `undefined` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### `.footer`
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Aditional information.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < spec > .footer = < string >  | < function >  | undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Default value: `undefined` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-19 18:06:17 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								##### More control over help...
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For more info on help formatting see `.help*`  attributes in the [source ](./argv.js ).
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Nested parsers
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Components and API
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `THEN`, `STOP` and `ERROR`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Values that if returned by option/command handlers can control the parse flow.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `THEN`  –  Stop parsing and call `.then(..)`  callbacks. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `STOP`  –  Stop parsing and call `.stop(..)`  callbacks,  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  skipping `.then(..)` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `ERROR`  –  Stop parsing, call `.error(..)`  callbacks and 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  exit with an error.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### `Parser(..)` 
  
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Construct a parser instance
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Parser(< spec > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								See [`<parser>(..)` ](#parser-1 ) for more info.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-04 21:48:25 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### `.then(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Add callback to `then`  "event".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > .then(< callback > ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								callback(< unhandled > , < root-value > , < rest > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < obj > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`then`  is triggered when parsing is done or stopped from an option handler by returning `THEN` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `.stop(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Add callback to `stop`  "event".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > .stop(< callback > ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								callback(< arg > , < rest > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < obj > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`stop`  is triggered when a handler returns `STOP` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `.error(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Add callback to `error`  "event".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > .error(< callback > ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								callback(< reason > , < arg > , < rest > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < obj > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`error`  is triggered when a handler returns `ERROR` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `.off(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Remove callback from "event".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									< parser > .off(< event > , < callback > )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										-> < parser > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### `<parser>(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Execute the `parser`  insatance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Run the parser on `process.argv` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > () 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < result > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Explicitly pass a list of arguments where `<argv>[0]`  is treated as 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								the script path.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > (< argv > ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < result > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Explicitly pass both a list of args and script path.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< parser > (< argv > , < main > ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									-> < result > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If `<main>`  is present in `<argv>`  all the arguments before it will 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								be ignored, otherwise the whole list is processed as if `<main>`  was 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								its head.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Advanced parser API
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `.print(..)` / `.printError(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `.handlerDefault(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `.handleArgument(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `.handleArgumentValue(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `.handleErrorExit(..)`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-18 20:17:15 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### More...
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For more info see the [source ](./argv.js ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-01 03:09:23 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## License
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[BSD 3-Clause License ](./LICENSE )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Copyright (c) 2016-2020, Alex A. Naanou,  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All rights reserved.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-05 00:05:38 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<!--  vim:set ts=4 sw=4 spell :  -->