| 
									
										
										
										
											2020-07-01 03:09:23 +03:00
										 |  |  | # argv.js
 | 
					
						
							| 
									
										
										
										
											2020-06-29 03:24:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | Simple yet complete 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  | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | learning its API, quirks and adapting the project 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | - Simple / well documented | 
					
						
							| 
									
										
										
										
											2020-07-19 18:33:48 +03:00
										 |  |  | - Supports both the _option_ (a-la `ls`) 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 | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  | - Option/command value asignment   | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  |   implicit `-a 123` (requires either _definition_ or manual handling) or  | 
					
						
							|  |  |  |   explicit `-a=123` | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  | - Read option/command value defaults from environment variables | 
					
						
							| 
									
										
										
										
											2020-07-26 01:51:36 +03:00
										 |  |  | - Option/command value conversion | 
					
						
							|  |  |  | - Option/command value collection | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  | - Multiple option prefix support (by default `-` and `+` are handled) | 
					
						
							|  |  |  | - Dynamic option/command handling | 
					
						
							|  |  |  | - Customizable error and stop condition handling | 
					
						
							| 
									
										
										
										
											2020-07-19 18:33:48 +03:00
										 |  |  | - Reasonable defaults: | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  |   - Metadata read from `package.json` | 
					
						
							| 
									
										
										
										
											2020-07-19 18:33:48 +03:00
										 |  |  |   - `-help` – generate and print help | 
					
						
							|  |  |  |   - `-version` – print version | 
					
						
							| 
									
										
										
										
											2020-07-28 03:39:05 +03:00
										 |  |  |   - `-quiet` – suppress printing | 
					
						
							| 
									
										
										
										
											2020-07-19 18:33:48 +03:00
										 |  |  |   - `-` – stop argument processing | 
					
						
							| 
									
										
										
										
											2020-08-26 16:38:19 +03:00
										 |  |  | - Extensible and self-applicable | 
					
						
							| 
									
										
										
										
											2020-07-01 03:09:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 03:09:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | ### Planned
 | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | - Run `<command>-<sub-command>` scripts | 
					
						
							| 
									
										
										
										
											2020-07-26 01:51:36 +03:00
										 |  |  | - Option doc grouping (???) | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-12-10 15:33:36 +03:00
										 |  |  |   - [Motivation](#motivation) | 
					
						
							|  |  |  |   - [Features](#features) | 
					
						
							|  |  |  |     - [Planned](#planned) | 
					
						
							|  |  |  |   - [Contents](#contents) | 
					
						
							|  |  |  |   - [Architecture](#architecture) | 
					
						
							|  |  |  |   - [Basics and quick start](#basics-and-quick-start) | 
					
						
							|  |  |  |   - [Options in more detail](#options-in-more-detail) | 
					
						
							|  |  |  |     - [Help and metadata](#help-and-metadata) | 
					
						
							|  |  |  |     - [Basic options](#basic-options) | 
					
						
							|  |  |  |     - [Commands](#commands) | 
					
						
							|  |  |  |     - [Active options/commands](#active-optionscommands) | 
					
						
							| 
									
										
										
										
											2020-12-10 21:51:34 +03:00
										 |  |  |     - [Pattern options](#pattern-options) | 
					
						
							| 
									
										
										
										
											2020-12-10 15:33:36 +03:00
										 |  |  |     - [Nested parsers](#nested-parsers) | 
					
						
							|  |  |  |     - [Stopping](#stopping) | 
					
						
							|  |  |  |     - [Error reporting](#error-reporting) | 
					
						
							|  |  |  |     - [Before parsing begins](#before-parsing-begins) | 
					
						
							|  |  |  |     - [Handling the result](#handling-the-result) | 
					
						
							|  |  |  |     - [Calling the script](#calling-the-script) | 
					
						
							|  |  |  |   - [Advanced docs](#advanced-docs) | 
					
						
							|  |  |  |   - [License](#license) | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 13:55:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 03:38:46 +03:00
										 |  |  | ## Architecture
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | This module provides the following workflow: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 03:38:46 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | Parser(..) -> <parser>(..) -> <parsed> | 
					
						
							| 
									
										
										
										
											2020-07-31 03:38:46 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - define/declare a parser (parse grammar) | 
					
						
							|  |  |  | 	``` | 
					
						
							|  |  |  | 	Parser(<spec>) | 
					
						
							|  |  |  | 		-> <parser> | 
					
						
							|  |  |  | 	``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - define post-parse callbacks (optional) | 
					
						
							|  |  |  | 	``` | 
					
						
							|  |  |  | 	<parser> | 
					
						
							|  |  |  | 		.then(<callback>) | 
					
						
							|  |  |  | 		.stop(<callback>) | 
					
						
							|  |  |  | 		.error(<callback>) | 
					
						
							|  |  |  | 	``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - parse  | 
					
						
							|  |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-07-31 03:40:34 +03:00
										 |  |  | 	<parser>(...) | 
					
						
							| 
									
										
										
										
											2020-07-31 03:38:46 +03:00
										 |  |  | 		-> <parsed> | 
					
						
							|  |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | 	- arguments are handled in order of occurrence, | 
					
						
							|  |  |  | 	- argument handlers (defined in `<spec>`) are called while parsing, | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | 	- then/stop/error `<callback>`'s are called after the `<parser>` is done, | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	- everything is run in the _context_ of the `<parsed>` object so any | 
					
						
							| 
									
										
										
										
											2020-07-31 03:44:08 +03:00
										 |  |  | 	  data set on it is accessible after parsing is done for further | 
					
						
							| 
									
										
										
										
											2020-07-31 03:38:46 +03:00
										 |  |  | 	  reference. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that the `<parser>` is fully reusable and on each call will produce | 
					
						
							|  |  |  | a new `<parsed>` object. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `<parsed>` object has the `<parser>` as its `.__proto__`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | ## Basics and quick start
 | 
					
						
							| 
									
										
										
										
											2020-07-01 03:09:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | To install: | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-08-02 16:29:09 +03:00
										 |  |  | $ npm install --save ig-argv | 
					
						
							| 
									
										
										
										
											2020-07-01 03:09:23 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Create a [bare.js](./examples/bare.js) script and make it runnable | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ touch bare.js | 
					
						
							|  |  |  | $ chmod +x bare.js | 
					
						
							| 
									
										
										
										
											2020-07-01 03:09:23 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Now for the code | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var argv = require('ig-argv') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | var parser =  | 
					
						
							|  |  |  | exports.parser =  | 
					
						
							|  |  |  | 	argv.Parser({ | 
					
						
							|  |  |  | 			// option definitions... | 
					
						
							|  |  |  | 			// ... | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.then(function(){ | 
					
						
							|  |  |  | 			// things to do after the options are handled... | 
					
						
							|  |  |  | 			// ... | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | // run the parser... | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | __filename == (require.main || {}).filename | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	&& parser() | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:14:02 +03:00
										 |  |  | This script already knows how to respond to `-help` and friends. | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./bare.js --help  | 
					
						
							|  |  |  | Usage: bare.js [OPTIONS] | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Options: | 
					
						
							|  |  |  |         -h,  --help             - print this message and exit | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |         -v,  --version          - show bare.js verion and exit | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  |         -q,  --quiet            - quiet mode | 
					
						
							|  |  |  |         -                       - stop processing arguments after this point | 
					
						
							| 
									
										
										
										
											2020-08-02 07:30:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 07:28:43 +03:00
										 |  |  | Written by John Smith <j.smith@some-mail.com> | 
					
						
							|  |  |  | Varsion: 0.0.1 / License: BSD-3-Clause | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | ## Options in more detail
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Start by creating an [`options.js`](./examples/options.js) script... | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ touch options.js | 
					
						
							|  |  |  | $ chmod +x options.js | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | ...and a parser: | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // compatible with both node's and RequireJS' require(..) | 
					
						
							|  |  |  | var argv = require('ig-argv') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | var parser = argv.Parser({ | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 02:08:52 +03:00
										 |  |  | Now let us populate the option definitions, splitting the job into sections... | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | ### Help and metadata
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Basic script description | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	doc: 'Example script options', | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Note that `argv.js` exposes [object.js](https://github.com/flynx/object.js)'s  | 
					
						
							|  |  |  | [`normalizeIndent(..)` / `normalizeTextIndent(..)`](https://github.com/flynx/object.js#normalizeindent--normalizetextindent) for convenient text/code formatting. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | Metadata: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | 	// to make things consistent we'll take the version from package.json | 
					
						
							| 
									
										
										
										
											2020-07-31 03:00:47 +03:00
										 |  |  | 	version: require('./package.json').version, | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	author: 'John Smith <j.smith@some-mail.com>', | 
					
						
							|  |  |  | 	license: 'BSD-3-Clause', | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-18 20:17:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 07:30:24 +03:00
										 |  |  | If not set, `.version`, `.author` and `.license` are acquired from `package.json`  | 
					
						
							| 
									
										
										
										
											2020-08-02 07:32:13 +03:00
										 |  |  | located at the same path as the main script. To explicitly set the path of the  | 
					
						
							|  |  |  | JSON file from which metadata is read set  | 
					
						
							| 
									
										
										
										
											2020-08-02 07:28:43 +03:00
										 |  |  | [`<parser>.packageJson`](./ADVANCED.md#parserpackagejson). | 
					
						
							| 
									
										
										
										
											2020-08-02 07:21:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | These basic bits of metadata can be referenced in other `-help` sections,  | 
					
						
							|  |  |  | for example: | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-08-02 07:28:43 +03:00
										 |  |  | 	footer: 'Written by: $AUTHOR\nVersion: $VERSION / License: $LICENSE', | 
					
						
							| 
									
										
										
										
											2020-07-30 19:00:04 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | ### Basic options
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:38:36 +03:00
										 |  |  | These, if encountered, simply assign a value to an attribute on the parsed object.  | 
					
						
							|  |  |  | This attribute's name is defined by the option name (without the prefix) or by  | 
					
						
							|  |  |  | setting [`<option>.arg`](#optionarg)'s `<key>`.   | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | Any option/command can be passed a value, either explicitly (e.g. `-opt=123`) or  | 
					
						
							| 
									
										
										
										
											2020-08-02 19:38:36 +03:00
										 |  |  | implicitly by first setting [`<option>.arg`](#optionarg)'s `<arg-name>` component  | 
					
						
							|  |  |  | and and then passing `-opt 123`.   | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Option/command values can be set on the command-line as well as via  | 
					
						
							|  |  |  | [`<option>.env`](./ADVANCED.md#optionenv) and/or  | 
					
						
							|  |  |  | [`<option>.default`](./ADVANCED.md#optiondefault). | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | If option is given but no value is set, `undefined` is assigned to option  | 
					
						
							|  |  |  | attribute on the parsed object to indicate that the option/command is present  | 
					
						
							|  |  |  | in the command-line.   | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | Note that repeating a basic option/command will overwrite the previous occurrences' | 
					
						
							|  |  |  | value unless `.collect` is set (see `-push` example below). | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | Note that in the general case option order in the command-line is not critical,  | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | but option context can matter (see: [Active options/commands](#active-optionscommands) and [Nested parsers](#nested-parsers)) | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | 	'-flag': { | 
					
						
							|  |  |  | 		doc: 'if given, set .flag' }, | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	// option with a value... | 
					
						
							|  |  |  | 	'-value': { | 
					
						
							| 
									
										
										
										
											2020-08-10 23:17:10 +03:00
										 |  |  | 		doc: [ | 
					
						
							|  |  |  | 			'set .x to X', | 
					
						
							|  |  |  | 			'NOTE: .doc can be multiple lines'], | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 		// 'X' (i.e. VALUE) is used to indicate the option value in -help  | 
					
						
							|  |  |  | 		// while 'x' (key) is the attribute where the value will be written... | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// NOTE: if no .arg is set option attribute name is used. | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// See the first example in "Calling the script" section below for output. | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		arg: 'X | x', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// the value is optional by default but we can make it required... | 
					
						
							|  |  |  | 		valueRequired: true, | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	// setup an alias -r -> -required | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 	// | 
					
						
							|  |  |  | 	// NOTE: aliases are used only for referencing, all naming is done via the  | 
					
						
							|  |  |  | 	//		actual option/command name. | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'-r': '-required', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	// a required option... | 
					
						
							|  |  |  | 	'-required': { | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | 		doc: 'set .required_option_given to true', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// NOTE: we can omit the VALUE part to not require a value... | 
					
						
							|  |  |  | 		// NOTE: of no attr is specified in arg option name is used. | 
					
						
							|  |  |  | 		arg: '| required_option_given', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | 		default: true, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 		// NOTE: by default required options/commands are sorted above normal | 
					
						
							|  |  |  | 		//		options but bellow -help/-version/-quiet/... | 
					
						
							|  |  |  | 		//		(by default at priority 80) | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		required: true, | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 	'-i': { | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		doc: 'pass an integer value', | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// NOTE: if not key is given the VALUE name is used as a key, so the  | 
					
						
							|  |  |  | 		// 		value here is assigned to .INT... | 
					
						
							|  |  |  | 		arg: 'INT', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// convert the input value to int... | 
					
						
							|  |  |  | 		type: 'int', | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'-default': { | 
					
						
							|  |  |  | 		doc: 'option with default value', | 
					
						
							|  |  |  | 		arg: 'VALUE | default', | 
					
						
							| 
									
										
										
										
											2020-07-17 00:01:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		default: 'some value', | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// keep this near the top of the options list in -help... | 
					
						
							|  |  |  | 		priority: 80, | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'-home': { | 
					
						
							|  |  |  | 		doc: 'set home path', | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | 		arg: 'PATH | home_path', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// get the default value from the environment variable $HOME... | 
					
						
							|  |  |  | 		env: 'HOME', | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// collecting values... | 
					
						
							|  |  |  | 	'-p': '-push', | 
					
						
							|  |  |  | 	'-push': { | 
					
						
							|  |  |  | 		doc: 'push elements to a .list', | 
					
						
							|  |  |  | 		arg: 'ELEM | list', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		// this will add each argument to a -push option to a list... | 
					
						
							|  |  |  | 		collect: 'list', | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | For more info on available `.type` and `.collect` handlers see:  | 
					
						
							|  |  |  | [`<option>.type`](#optiontype) and [`<option>.collect`](#optioncollect) | 
					
						
							|  |  |  | respectively. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 21:51:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | ### Commands
 | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | The only difference between an _option_ and a _command_ is the prefix (`"-"` vs. `"@"`)  | 
					
						
							|  |  |  | that determines how it is parsed, otherwise they are identical and everything  | 
					
						
							|  |  |  | above applies here too. | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'@command': { | 
					
						
							|  |  |  | 		// ... | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	// Since options and commands are identical, aliases from one to the  | 
					
						
							|  |  |  | 	// other work as expected... | 
					
						
							|  |  |  | 	'-c': '@command', | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ### Active options/commands 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | These define `.handler`s which are executed when the option is encountered  | 
					
						
							|  |  |  | by the parser | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'-active': { | 
					
						
							|  |  |  | 		doc: 'basic active option', | 
					
						
							|  |  |  | 		handler: function(args, key, value){ | 
					
						
							|  |  |  | 			// ... | 
					
						
							|  |  |  | 		} }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-29 20:29:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | And for quick-n-dirty hacking stuff together, a shorthand (_not for production use_): | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'-s': '-shorthand-active', | 
					
						
							|  |  |  | 	'-shorthand-active': function(args, key, value){ | 
					
						
							|  |  |  | 		// ... | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | The `.handler(..)` will get called if the option is present in the command-line, | 
					
						
							|  |  |  | if either `.default` is not `undefined` or if `.env` and its environment | 
					
						
							|  |  |  | variable are defined, or any combination of the above. And vice-versa, if none  | 
					
						
							|  |  |  | of the above conditions are met the `.handler(..)` will not be called. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | Option's `.handler(..)` only sees the `args` that follow it in the command line,  | 
					
						
							|  |  |  | thus anything it may expect/get from the arguments must follow it (in the manner  | 
					
						
							|  |  |  | it expects), `argv.js` poses no restrictions on full or partial manual handling | 
					
						
							|  |  |  | of arguments by options/commands. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 18:49:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 21:51:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Pattern options
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Pattern option/command keys enable partial input key matching. | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  |     '-prefix-*': { | 
					
						
							|  |  |  |         doc: 'Pattern option', | 
					
						
							|  |  |  |         handler: function(rest, key){ | 
					
						
							|  |  |  |             // ... | 
					
						
							|  |  |  |         } }, | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The above code will match any _unhandled_ input option starting with  | 
					
						
							|  |  |  | `-prefix-`/`--prefix-` and push the explicitly `false` value back to the option  | 
					
						
							|  |  |  | queue. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A pattern option/command is any option with a key containing `"*"`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ### Nested parsers
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | An options/command handler can also be a full fledged parser. | 
					
						
							| 
									
										
										
										
											2020-07-29 22:41:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 	'@nested': argv.Parser({ | 
					
						
							|  |  |  | 			// ... | 
					
						
							|  |  |  | 		}).then(function(){ | 
					
						
							|  |  |  | 			// ... | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// and for fun, import the bare parser... | 
					
						
							|  |  |  | 	'@bare': require('./bare').parser, | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | This can be useful when there is a need to define a sub-context with it's own  | 
					
						
							|  |  |  | options and settings but it does not need to be isolated into a separate  | 
					
						
							|  |  |  | external command. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When a nested parser is started it will consume subsequent arguments until it  | 
					
						
							|  |  |  | exits, then the parent parser will pick up where it left. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Externally it is treated in exactly the same way as a normal _function_ handler,  | 
					
						
							|  |  |  | essentially, the parent parser does not know the difference between the two. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  | As with [Active options/commands](#active-optionscommands) the nested  | 
					
						
							|  |  |  | parser (essentially an active option itself) only sees the arguments that  | 
					
						
							|  |  |  | follow it in the command-line and may have arbitrary expectations of them. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:41:15 +03:00
										 |  |  | For more detail see the [Nested parsers](./ADVANCED.md#nested-parsers) section  | 
					
						
							|  |  |  | in detailed docs. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ### Stopping
 | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | To stop option processing either return `STOP` or `THEN` from the handler. | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:35:55 +03:00
										 |  |  | - `THEN` is the normal case, stop processing and trigger [`<parser>.then(..)`](./ADVANCED.md#parserthen): | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		'-then': {  | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				return argv.THEN } }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:35:55 +03:00
										 |  |  | - `STOP` will stop processing and trigger [`<parser>.stop(..)`](./ADVANCED.md#parserstop): | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		'-stop': {  | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				return argv.STOP } }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 20:28:56 +03:00
										 |  |  | 	`STOP` is needed in cases where we want to stop the parser and not trigger  | 
					
						
							|  |  |  | 	it's main functionality (i.e. [`<parser>.then(..)`](./ADVANCED.md#parserthen)), | 
					
						
							|  |  |  | 	for example this is needed when printing `-help` and related tasks like  | 
					
						
							|  |  |  | 	listing commands and other script interface documentation/introspection. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-19 20:58:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | ### Error reporting
 | 
					
						
							| 
									
										
										
										
											2020-07-27 20:21:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | There are three ways to stop and/or report errors: | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | - Simply `throw` a `ParserError(..)` instance: | 
					
						
							|  |  |  | 	```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		'-error': { | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				throw argv.ParserError('something went wrong.') } }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | 	Here processing will stop and the error will be reported automatically | 
					
						
							|  |  |  | 	before [`<parser>.error(..)`](./ADVANCED.md#parsererror-1) is triggered. | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | - _Silently_ `return` a `ParserError(..)` instance: | 
					
						
							|  |  |  | 	```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		'-silent-error': { | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				return argv.ParserError('something went wrong.') } }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	``` | 
					
						
							|  |  |  | 	This will _not_ report the error but will stop processing and trigger  | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | 	[`<parser>.error(..)`](./ADVANCED.md#parsererror-1), so the user can either  | 
					
						
							|  |  |  | 	recover from or report the issue manually. | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | - For a critical error simply `throw` any other JavaScript error/exception: | 
					
						
							|  |  |  | 	```javascript | 
					
						
							| 
									
										
										
										
											2020-07-29 23:26:31 +03:00
										 |  |  | 		'-critical-error': { | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				throw 'something went really wrong.' } }, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | Note that [`<parser>.then(..)`](./ADVANCED.md#parserthen) will not be triggered  | 
					
						
							|  |  |  | in any of these cases. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:35:55 +03:00
										 |  |  | Also see: [`<parser>.printError(..)`](./ADVANCED.md#parserprint--parserprinterror) | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | And to close things off for the `<spec>` ;) | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 11:09:11 +03:00
										 |  |  | ### Before parsing begins
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `<parser>` can notify the user if any arguments were passed or not before the parsing starts: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - [`<parser>.onArgs(..)`](./ADVANCED.md#parseronargs) triggered when one or  | 
					
						
							|  |  |  |   more arguments were passed | 
					
						
							|  |  |  |   ```javascript | 
					
						
							|  |  |  |   .onArgs(function(args){ | 
					
						
							|  |  |  | 	  console.log('### input arguments:', args) }) | 
					
						
							|  |  |  |   ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - [`<parser>.onNoArgs(..)`](./ADVANCED.md#parseronnoargs) triggered when no  | 
					
						
							|  |  |  |   arguments were passed | 
					
						
							|  |  |  |   ```javascript | 
					
						
							|  |  |  |   .onNoArgs(function(args){ | 
					
						
							|  |  |  | 	  console.log('### no arguments passed.') }) | 
					
						
							|  |  |  |   ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | ### Handling the result
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `<parser>` will call different sets of callbacks on different stop conditions: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - [`<parser>.then(..)`](./ADVANCED.md#parserthen) for normal exit | 
					
						
							|  |  |  | 	```javascript | 
					
						
							|  |  |  | 	.then(function(unhandled, root_value, rest){ | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | 		console.log('### finished normally.') | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | 		console.log(this) }) | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - [`<parser>.stop(..)`](./ADVANCED.md#parserstop) when parser is stopped | 
					
						
							|  |  |  | 	```javascript | 
					
						
							|  |  |  | 	.stop(function(arg, rest){ | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | 		console.log(`### stopped at ${arg}.`) }) | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 02:13:02 +03:00
										 |  |  | - [`<parser>.error(..)`](./ADVANCED.md#parsererror) when an error is detected | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	```javascript | 
					
						
							|  |  |  | 	.error(function(reason, arg, rest){ | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | 		console.log(`### something went wrong when parsing ${arg}.`) }) | 
					
						
							| 
									
										
										
										
											2020-07-31 04:30:13 +03:00
										 |  |  | 	``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 21:48:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 23:20:17 +03:00
										 |  |  | ### Calling the script
 | 
					
						
							| 
									
										
										
										
											2020-07-19 20:58:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | This will create a parser that supports the following: | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./options.js --help  | 
					
						
							| 
									
										
										
										
											2020-12-10 15:33:36 +03:00
										 |  |  | Usage: options.js -r [OPTIONS] | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Example script options | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Options: | 
					
						
							|  |  |  |         -h,  --help             - print this message and exit | 
					
						
							|  |  |  |         -v,  --version          - show options.js verion and exit | 
					
						
							|  |  |  |         -q,  --quiet            - quiet mode | 
					
						
							|  |  |  |         -r,  --required         - set .required_option_given to true | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (required) | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |              --default=VALUE    - option with default value | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (default: some value) | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  |              --flag             - if given set .flag | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |              --value=X          - set .x to X | 
					
						
							| 
									
										
										
										
											2020-08-10 23:17:10 +03:00
										 |  |  | 			 					  NOTE: .doc can be multiple lines | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (required value) | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  |         -i=INT                  - pass an integer value | 
					
						
							|  |  |  |              --home=PATH        - set home path | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (env: $HOME) | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |         -p,  --push=ELEM        - push elements to a .list | 
					
						
							|  |  |  |         -c                      - command | 
					
						
							|  |  |  |              --active           - basic active option | 
					
						
							|  |  |  |         -s,  --shorthand-active - shorthand-active | 
					
						
							| 
									
										
										
										
											2020-12-10 21:51:34 +03:00
										 |  |  |              --prefix-*         - Pattern option | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |              --then             - then | 
					
						
							|  |  |  |              --stop             - stop | 
					
						
							|  |  |  |              --error            - error | 
					
						
							|  |  |  |              --silent-error     - silent-error | 
					
						
							|  |  |  |              --critical-error   - critical-error | 
					
						
							|  |  |  |         -                       - stop processing arguments after this point | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Commands: | 
					
						
							|  |  |  |         command                 - command | 
					
						
							|  |  |  |         nested                  - nested | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (more: .. nested -h) | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  |         bare                    - bare | 
					
						
							| 
									
										
										
										
											2020-08-02 04:24:24 +03:00
										 |  |  |                                   (more: .. bare -h) | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 07:28:43 +03:00
										 |  |  | Written by John Smith <j.smith@some-mail.com> | 
					
						
							|  |  |  | Varsion: 0.0.1 / License: BSD-3-Clause | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | ### stopped at --help.
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | Required argument handling | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./options.js | 
					
						
							|  |  |  | options.js: ParserError: required but missing: -required | 
					
						
							|  |  |  | ### something went wrong when parsing -required.  
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./options.js -r | 
					
						
							|  |  |  | ### finished normally.
 | 
					
						
							|  |  |  | Parser { | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  |   required_option_given: true, | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  |   ... | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  |   default: 'some value', | 
					
						
							| 
									
										
										
										
											2020-08-02 16:18:14 +03:00
										 |  |  |   home_path: '...' | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | Notice the default values are set in the output above (output partially truncated | 
					
						
							|  |  |  | for brevity). | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Passing values implicitly | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./script.js -r --value 321 | 
					
						
							|  |  |  | ### finished normally.
 | 
					
						
							|  |  |  | Parser { | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  |   required_option_given: true, | 
					
						
							|  |  |  |   x: '321', | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  |   ... | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Passing values explicitly | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | $ ./script.js -r --value=321 | 
					
						
							|  |  |  | ### finished normally.
 | 
					
						
							|  |  |  | Parser { | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  |   required_option_given: true, | 
					
						
							|  |  |  |   x: '321', | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  |   ... | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | Call a nested parser | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | $ ./script.js nested -h | 
					
						
							|  |  |  | Usage: options.js nested [OPTIONS] | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | Options: | 
					
						
							|  |  |  |         -h,  --help             - print this message and exit | 
					
						
							|  |  |  |         -v,  --version          - show options.js nested verion and exit | 
					
						
							|  |  |  |         -q,  --quiet            - quiet mode | 
					
						
							|  |  |  |         -                       - stop processing arguments after this point | 
					
						
							| 
									
										
										
										
											2020-08-02 07:28:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Written by John Smith <j.smith@some-mail.com> | 
					
						
							|  |  |  | Varsion: 0.0.1 / License: BSD-3-Clause | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | ### stopped at nested.
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | $ ./script.js -r bare | 
					
						
							|  |  |  | ### finished normally.
 | 
					
						
							|  |  |  | Parser { | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  |   required_option_given: true, | 
					
						
							|  |  |  |   bare: Parser { | 
					
						
							|  |  |  |     rest: [], | 
					
						
							|  |  |  |     argv: [], | 
					
						
							|  |  |  |     nested: true, | 
					
						
							|  |  |  |     script: 'options.js bare', | 
					
						
							|  |  |  |     scriptName: 'options.js bare', | 
					
						
							|  |  |  |     scriptPath: '', | 
					
						
							|  |  |  |     unhandled: [] | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | Split options and pass value to the last one | 
					
						
							| 
									
										
										
										
											2020-08-02 01:59:10 +03:00
										 |  |  | ```shell_session | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  | $ ./options.js -rsc=321 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | ### finished normally.
 | 
					
						
							|  |  |  | Parser { | 
					
						
							|  |  |  |   ... | 
					
						
							|  |  |  |   required_option_given: true, | 
					
						
							| 
									
										
										
										
											2020-08-02 01:49:02 +03:00
										 |  |  |   command: '321', | 
					
						
							|  |  |  |   ... | 
					
						
							| 
									
										
										
										
											2020-07-31 05:11:49 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 19:01:58 +03:00
										 |  |  | ## Advanced docs
 | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:17:35 +03:00
										 |  |  | For a more detailed set of docs see [ADVANCED.md](./ADVANCED.md). | 
					
						
							| 
									
										
										
										
											2020-07-27 03:13:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 05:17:35 +03:00
										 |  |  | For even more detail see the [source](./argv.js)... | 
					
						
							| 
									
										
										
										
											2020-07-18 20:17:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-08-02 16:18:14 +03:00
										 |  |  | <!-- vim:set ts=4 sw=4 spell : --> |