Commit b807a4c1 authored by Wolfgang Knopki's avatar Wolfgang Knopki
Browse files

Merge branch 'revert-jul13' into 'testing'

Revert "Merge branch 'MLAB-677' into 'testing'"

See merge request !168
parents 81e73777 08d3a3ba
Pipeline #6895 passed with stage
in 16 seconds
argparse
========
[![Build Status](https://secure.travis-ci.org/nodeca/argparse.svg?branch=master)](http://travis-ci.org/nodeca/argparse)
[![NPM version](https://img.shields.io/npm/v/argparse.svg)](https://www.npmjs.org/package/argparse)
CLI arguments parser for node.js. Javascript port of python's
[argparse](http://docs.python.org/dev/library/argparse.html) module
(original version 3.2). That's a full port, except some very rare options,
recorded in issue tracker.
**NB. Difference with original.**
- Method names changed to camelCase. See [generated docs](http://nodeca.github.com/argparse/).
- Use `defaultValue` instead of `default`.
- Use `argparse.Const.REMAINDER` instead of `argparse.REMAINDER`, and
similarly for constant values `OPTIONAL`, `ZERO_OR_MORE`, and `ONE_OR_MORE`
(aliases for `nargs` values `'?'`, `'*'`, `'+'`, respectively), and
`SUPPRESS`.
Example
=======
test.js file:
```javascript
#!/usr/bin/env node
'use strict';
var ArgumentParser = require('../lib/argparse').ArgumentParser;
var parser = new ArgumentParser({
version: '0.0.1',
addHelp:true,
description: 'Argparse example'
});
parser.addArgument(
[ '-f', '--foo' ],
{
help: 'foo bar'
}
);
parser.addArgument(
[ '-b', '--bar' ],
{
help: 'bar foo'
}
);
parser.addArgument(
'--baz',
{
help: 'baz bar'
}
);
var args = parser.parseArgs();
console.dir(args);
```
Display help:
```
$ ./test.js -h
usage: example.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
Argparse example
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-f FOO, --foo FOO foo bar
-b BAR, --bar BAR bar foo
--baz BAZ baz bar
```
Parse arguments:
```
$ ./test.js -f=3 --bar=4 --baz 5
{ foo: '3', bar: '4', baz: '5' }
```
More [examples](https://github.com/nodeca/argparse/tree/master/examples).
ArgumentParser objects
======================
```
new ArgumentParser({parameters hash});
```
Creates a new ArgumentParser object.
**Supported params:**
- ```description``` - Text to display before the argument help.
- ```epilog``` - Text to display after the argument help.
- ```addHelp``` - Add a -h/–help option to the parser. (default: true)
- ```argumentDefault``` - Set the global default value for arguments. (default: null)
- ```parents``` - A list of ArgumentParser objects whose arguments should also be included.
- ```prefixChars``` - The set of characters that prefix optional arguments. (default: ‘-‘)
- ```formatterClass``` - A class for customizing the help output.
- ```prog``` - The name of the program (default: `path.basename(process.argv[1])`)
- ```usage``` - The string describing the program usage (default: generated)
- ```conflictHandler``` - Usually unnecessary, defines strategy for resolving conflicting optionals.
**Not supported yet**
- ```fromfilePrefixChars``` - The set of characters that prefix files from which additional arguments should be read.
Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
addArgument() method
====================
```
ArgumentParser.addArgument(name or flag or [name] or [flags...], {options})
```
Defines how a single command-line argument should be parsed.
- ```name or flag or [name] or [flags...]``` - Either a positional name
(e.g., `'foo'`), a single option (e.g., `'-f'` or `'--foo'`), an array
of a single positional name (e.g., `['foo']`), or an array of options
(e.g., `['-f', '--foo']`).
Options:
- ```action``` - The basic type of action to be taken when this argument is encountered at the command line.
- ```nargs```- The number of command-line arguments that should be consumed.
- ```constant``` - A constant value required by some action and nargs selections.
- ```defaultValue``` - The value produced if the argument is absent from the command line.
- ```type``` - The type to which the command-line argument should be converted.
- ```choices``` - A container of the allowable values for the argument.
- ```required``` - Whether or not the command-line option may be omitted (optionals only).
- ```help``` - A brief description of what the argument does.
- ```metavar``` - A name for the argument in usage messages.
- ```dest``` - The name of the attribute to be added to the object returned by parseArgs().
Details in [original add_argument guide](http://docs.python.org/dev/library/argparse.html#the-add-argument-method)
Action (some details)
================
ArgumentParser objects associate command-line arguments with actions.
These actions can do just about anything with the command-line arguments associated
with them, though most actions simply add an attribute to the object returned by
parseArgs(). The action keyword argument specifies how the command-line arguments
should be handled. The supported actions are:
- ```store``` - Just stores the argument’s value. This is the default action.
- ```storeConst``` - Stores value, specified by the const keyword argument.
(Note that the const keyword argument defaults to the rather unhelpful None.)
The 'storeConst' action is most commonly used with optional arguments, that
specify some sort of flag.
- ```storeTrue``` and ```storeFalse``` - Stores values True and False
respectively. These are special cases of 'storeConst'.
- ```append``` - Stores a list, and appends each argument value to the list.
This is useful to allow an option to be specified multiple times.
- ```appendConst``` - Stores a list, and appends value, specified by the
const keyword argument to the list. (Note, that the const keyword argument defaults
is None.) The 'appendConst' action is typically used when multiple arguments need
to store constants to the same list.
- ```count``` - Counts the number of times a keyword argument occurs. For example,
used for increasing verbosity levels.
- ```help``` - Prints a complete help message for all the options in the current
parser and then exits. By default a help action is automatically added to the parser.
See ArgumentParser for details of how the output is created.
- ```version``` - Prints version information and exit. Expects a `version=`
keyword argument in the addArgument() call.
Details in [original action guide](http://docs.python.org/dev/library/argparse.html#action)
Sub-commands
============
ArgumentParser.addSubparsers()
Many programs split their functionality into a number of sub-commands, for
example, the svn program can invoke sub-commands like `svn checkout`, `svn update`,
and `svn commit`. Splitting up functionality this way can be a particularly good
idea when a program performs several different functions which require different
kinds of command-line arguments. `ArgumentParser` supports creation of such
sub-commands with `addSubparsers()` method. The `addSubparsers()` method is
normally called with no arguments and returns an special action object.
This object has a single method `addParser()`, which takes a command name and
any `ArgumentParser` constructor arguments, and returns an `ArgumentParser` object
that can be modified as usual.
Example:
sub_commands.js
```javascript
#!/usr/bin/env node
'use strict';
var ArgumentParser = require('../lib/argparse').ArgumentParser;
var parser = new ArgumentParser({
version: '0.0.1',
addHelp:true,
description: 'Argparse examples: sub-commands',
});
var subparsers = parser.addSubparsers({
title:'subcommands',
dest:"subcommand_name"
});
var bar = subparsers.addParser('c1', {addHelp:true});
bar.addArgument(
[ '-f', '--foo' ],
{
action: 'store',
help: 'foo3 bar3'
}
);
var bar = subparsers.addParser(
'c2',
{aliases:['co'], addHelp:true}
);
bar.addArgument(
[ '-b', '--bar' ],
{
action: 'store',
type: 'int',
help: 'foo3 bar3'
}
);
var args = parser.parseArgs();
console.dir(args);
```
Details in [original sub-commands guide](http://docs.python.org/dev/library/argparse.html#sub-commands)
Contributors
============
- [Eugene Shkuropat](https://github.com/shkuropat)
- [Paul Jacobson](https://github.com/hpaulj)
[others](https://github.com/nodeca/argparse/graphs/contributors)
License
=======
Copyright (c) 2012 [Vitaly Puzrin](https://github.com/puzrin).
Released under the MIT license. See
[LICENSE](https://github.com/nodeca/argparse/blob/master/LICENSE) for details.
'use strict';
module.exports = require('./lib/argparse');
/**
* class Action
*
* Base class for all actions
* Do not call in your code, use this class only for inherits your own action
*
* Information about how to convert command line strings to Javascript objects.
* Action objects are used by an ArgumentParser to represent the information
* needed to parse a single argument from one or more strings from the command
* line. The keyword arguments to the Action constructor are also all attributes
* of Action instances.
*
* ##### Allowed keywords:
*
* - `store`
* - `storeConstant`
* - `storeTrue`
* - `storeFalse`
* - `append`
* - `appendConstant`
* - `count`
* - `help`
* - `version`
*
* Information about action options see [[Action.new]]
*
* See also [original guide](http://docs.python.org/dev/library/argparse.html#action)
*
**/
'use strict';
// Constants
var c = require('./const');
/**
* new Action(options)
*
* Base class for all actions. Used only for inherits
*
*
* ##### Options:
*
* - `optionStrings` A list of command-line option strings for the action.
* - `dest` Attribute to hold the created object(s)
* - `nargs` The number of command-line arguments that should be consumed.
* By default, one argument will be consumed and a single value will be
* produced.
* - `constant` Default value for an action with no value.
* - `defaultValue` The value to be produced if the option is not specified.
* - `type` Cast to 'string'|'int'|'float'|'complex'|function (string). If
* None, 'string'.
* - `choices` The choices available.
* - `required` True if the action must always be specified at the command
* line.
* - `help` The help describing the argument.
* - `metavar` The name to be used for the option's argument with the help
* string. If None, the 'dest' value will be used as the name.
*
* ##### nargs supported values:
*
* - `N` (an integer) consumes N arguments (and produces a list)
* - `?` consumes zero or one arguments
* - `*` consumes zero or more arguments (and produces a list)
* - `+` consumes one or more arguments (and produces a list)
*
* Note: that the difference between the default and nargs=1 is that with the
* default, a single value will be produced, while with nargs=1, a list
* containing a single value will be produced.
**/
var Action = module.exports = function Action(options) {
options = options || {};
this.optionStrings = options.optionStrings || [];
this.dest = options.dest;
this.nargs = typeof options.nargs !== 'undefined' ? options.nargs : null;
this.constant = typeof options.constant !== 'undefined' ? options.constant : null;
this.defaultValue = options.defaultValue;
this.type = typeof options.type !== 'undefined' ? options.type : null;
this.choices = typeof options.choices !== 'undefined' ? options.choices : null;
this.required = typeof options.required !== 'undefined' ? options.required : false;
this.help = typeof options.help !== 'undefined' ? options.help : null;
this.metavar = typeof options.metavar !== 'undefined' ? options.metavar : null;
if (!(this.optionStrings instanceof Array)) {
throw new Error('optionStrings should be an array');
}
if (typeof this.required !== 'undefined' && typeof this.required !== 'boolean') {
throw new Error('required should be a boolean');
}
};
/**
* Action#getName -> String
*
* Tells action name
**/
Action.prototype.getName = function () {
if (this.optionStrings.length > 0) {
return this.optionStrings.join('/');
} else if (this.metavar !== null && this.metavar !== c.SUPPRESS) {
return this.metavar;
} else if (typeof this.dest !== 'undefined' && this.dest !== c.SUPPRESS) {
return this.dest;
}
return null;
};
/**
* Action#isOptional -> Boolean
*
* Return true if optional
**/
Action.prototype.isOptional = function () {
return !this.isPositional();
};
/**
* Action#isPositional -> Boolean
*
* Return true if positional
**/
Action.prototype.isPositional = function () {
return (this.optionStrings.length === 0);
};
/**
* Action#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Should be implemented in inherited classes
*
* ##### Example
*
* ActionCount.prototype.call = function (parser, namespace, values, optionString) {
* namespace.set(this.dest, (namespace[this.dest] || 0) + 1);
* };
*
**/
Action.prototype.call = function () {
throw new Error('.call() not defined');// Not Implemented error
};
/*:nodoc:*
* class ActionAppend
*
* This action stores a list, and appends each argument value to the list.
* This is useful to allow an option to be specified multiple times.
* This class inherided from [[Action]]
*
**/
'use strict';
var util = require('util');
var Action = require('../action');
// Constants
var c = require('../const');
/*:nodoc:*
* new ActionAppend(options)
* - options (object): options hash see [[Action.new]]
*
* Note: options.nargs should be optional for constants
* and more then zero for other
**/
var ActionAppend = module.exports = function ActionAppend(options) {
options = options || {};
if (this.nargs <= 0) {
throw new Error('nargs for append actions must be > 0; if arg ' +
'strings are not supplying the value to append, ' +
'the append const action may be more appropriate');
}
if (!!this.constant && this.nargs !== c.OPTIONAL) {
throw new Error('nargs must be OPTIONAL to supply const');
}
Action.call(this, options);
};
util.inherits(ActionAppend, Action);
/*:nodoc:*
* ActionAppend#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Save result in namespace object
**/
ActionAppend.prototype.call = function (parser, namespace, values) {
var items = (namespace[this.dest] || []).slice();
items.push(values);
namespace.set(this.dest, items);
};
/*:nodoc:*
* class ActionAppendConstant
*
* This stores a list, and appends the value specified by
* the const keyword argument to the list.
* (Note that the const keyword argument defaults to null.)
* The 'appendConst' action is typically useful when multiple
* arguments need to store constants to the same list.
*
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var Action = require('../../action');
/*:nodoc:*
* new ActionAppendConstant(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionAppendConstant = module.exports = function ActionAppendConstant(options) {
options = options || {};
options.nargs = 0;
if (typeof options.constant === 'undefined') {
throw new Error('constant option is required for appendAction');
}
Action.call(this, options);
};
util.inherits(ActionAppendConstant, Action);
/*:nodoc:*
* ActionAppendConstant#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Save result in namespace object
**/
ActionAppendConstant.prototype.call = function (parser, namespace) {
var items = [].concat(namespace[this.dest] || []);
items.push(this.constant);
namespace.set(this.dest, items);
};
/*:nodoc:*
* class ActionCount
*
* This counts the number of times a keyword argument occurs.
* For example, this is useful for increasing verbosity levels
*
* This class inherided from [[Action]]
*
**/
'use strict';
var util = require('util');
var Action = require('../action');
/*:nodoc:*
* new ActionCount(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionCount = module.exports = function ActionCount(options) {
options = options || {};
options.nargs = 0;
Action.call(this, options);
};
util.inherits(ActionCount, Action);
/*:nodoc:*
* ActionCount#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Save result in namespace object
**/
ActionCount.prototype.call = function (parser, namespace) {
namespace.set(this.dest, (namespace[this.dest] || 0) + 1);
};
/*:nodoc:*
* class ActionHelp
*
* Support action for printing help
* This class inherided from [[Action]]
**/
'use strict';
var util = require('util');
var Action = require('../action');
// Constants
var c = require('../const');
/*:nodoc:*
* new ActionHelp(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionHelp = module.exports = function ActionHelp(options) {
options = options || {};
if (options.defaultValue !== null) {
options.defaultValue = options.defaultValue;
} else {
options.defaultValue = c.SUPPRESS;
}
options.dest = (options.dest !== null ? options.dest : c.SUPPRESS);
options.nargs = 0;
Action.call(this, options);
};
util.inherits(ActionHelp, Action);
/*:nodoc:*
* ActionHelp#call(parser, namespace, values, optionString)
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Print help and exit
**/
ActionHelp.prototype.call = function (parser) {
parser.printHelp();
parser.exit();
};
/*:nodoc:*
* class ActionStore
*
* This action just stores the argument’s value. This is the default action.
*
* This class inherited from [[Action]]
*
**/
'use strict';
var util = require('util');
var Action = require('../action');
// Constants
var c = require('../const');
/*:nodoc:*
* new ActionStore(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionStore = module.exports = function ActionStore(options) {
options = options || {};
if (this.nargs <= 0) {
throw new Error('nargs for store actions must be > 0; if you ' +
'have nothing to store, actions such as store ' +
'true or store const may be more appropriate');
}
if (typeof this.constant !== 'undefined' && this.nargs !== c.OPTIONAL) {
throw new Error('nargs must be OPTIONAL to supply const');
}
Action.call(this, options);
};
util.inherits(ActionStore, Action);
/*:nodoc:*
* ActionStore#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Save result in namespace object
**/
ActionStore.prototype.call = function (parser, namespace, values) {
namespace.set(this.dest, values);
};
/*:nodoc:*
* class ActionStoreConstant
*
* This action stores the value specified by the const keyword argument.
* (Note that the const keyword argument defaults to the rather unhelpful null.)
* The 'store_const' action is most commonly used with optional
* arguments that specify some sort of flag.
*
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var Action = require('../../action');
/*:nodoc:*
* new ActionStoreConstant(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionStoreConstant = module.exports = function ActionStoreConstant(options) {
options = options || {};
options.nargs = 0;
if (typeof options.constant === 'undefined') {
throw new Error('constant option is required for storeAction');
}
Action.call(this, options);
};
util.inherits(ActionStoreConstant, Action);
/*:nodoc:*
* ActionStoreConstant#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Save result in namespace object
**/
ActionStoreConstant.prototype.call = function (parser, namespace) {
namespace.set(this.dest, this.constant);
};
/*:nodoc:*
* class ActionStoreFalse
*
* This action store the values False respectively.
* This is special cases of 'storeConst'
*
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var ActionStoreConstant = require('./constant');
/*:nodoc:*
* new ActionStoreFalse(options)
* - options (object): hash of options see [[Action.new]]
*
**/
var ActionStoreFalse = module.exports = function ActionStoreFalse(options) {
options = options || {};
options.constant = false;
options.defaultValue = options.defaultValue !== null ? options.defaultValue : true;
ActionStoreConstant.call(this, options);
};
util.inherits(ActionStoreFalse, ActionStoreConstant);
/*:nodoc:*
* class ActionStoreTrue
*
* This action store the values True respectively.
* This isspecial cases of 'storeConst'
*
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var ActionStoreConstant = require('./constant');
/*:nodoc:*
* new ActionStoreTrue(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionStoreTrue = module.exports = function ActionStoreTrue(options) {
options = options || {};
options.constant = true;
options.defaultValue = options.defaultValue !== null ? options.defaultValue : false;
ActionStoreConstant.call(this, options);
};
util.inherits(ActionStoreTrue, ActionStoreConstant);
/** internal
* class ActionSubparsers
*
* Support the creation of such sub-commands with the addSubparsers()
*
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var format = require('util').format;
var Action = require('../action');
// Constants
var c = require('../const');
// Errors
var argumentErrorHelper = require('../argument/error');
/*:nodoc:*
* new ChoicesPseudoAction(name, help)
*
* Create pseudo action for correct help text
*
**/
function ChoicesPseudoAction(name, help) {
var options = {
optionStrings: [],
dest: name,
help: help
};
Action.call(this, options);
}
util.inherits(ChoicesPseudoAction, Action);
/**
* new ActionSubparsers(options)
* - options (object): options hash see [[Action.new]]
*
**/
function ActionSubparsers(options) {
options = options || {};
options.dest = options.dest || c.SUPPRESS;
options.nargs = c.PARSER;
this.debug = (options.debug === true);
this._progPrefix = options.prog;
this._parserClass = options.parserClass;
this._nameParserMap = {};
this._choicesActions = [];
options.choices = this._nameParserMap;
Action.call(this, options);
}
util.inherits(ActionSubparsers, Action);
/*:nodoc:*
* ActionSubparsers#addParser(name, options) -> ArgumentParser
* - name (string): sub-command name
* - options (object): see [[ArgumentParser.new]]
*
* Note:
* addParser supports an additional aliases option,
* which allows multiple strings to refer to the same subparser.
* This example, like svn, aliases co as a shorthand for checkout
*
**/
ActionSubparsers.prototype.addParser = function (name, options) {
var parser;
var self = this;
options = options || {};
options.debug = (this.debug === true);
// set program from the existing prefix
if (!options.prog) {
options.prog = this._progPrefix + ' ' + name;
}
var aliases = options.aliases || [];
// create a pseudo-action to hold the choice help
if (!!options.help || typeof options.help === 'string') {
var help = options.help;
delete options.help;
var choiceAction = new ChoicesPseudoAction(name, help);
this._choicesActions.push(choiceAction);
}
// create the parser and add it to the map
parser = new this._parserClass(options);
this._nameParserMap[name] = parser;
// make parser available under aliases also
aliases.forEach(function (alias) {
self._nameParserMap[alias] = parser;
});
return parser;
};
ActionSubparsers.prototype._getSubactions = function () {
return this._choicesActions;
};
/*:nodoc:*
* ActionSubparsers#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Call the action. Parse input aguments
**/
ActionSubparsers.prototype.call = function (parser, namespace, values) {
var parserName = values[0];
var argStrings = values.slice(1);
// set the parser name if requested
if (this.dest !== c.SUPPRESS) {
namespace[this.dest] = parserName;
}
// select the parser
if (this._nameParserMap[parserName]) {
parser = this._nameParserMap[parserName];
} else {
throw argumentErrorHelper(format(
'Unknown parser "%s" (choices: [%s]).',
parserName,
Object.keys(this._nameParserMap).join(', ')
));
}
// parse all the remaining options into the namespace
parser.parseArgs(argStrings, namespace);
};
module.exports = ActionSubparsers;
/*:nodoc:*
* class ActionVersion
*
* Support action for printing program version
* This class inherited from [[Action]]
**/
'use strict';
var util = require('util');
var Action = require('../action');
//
// Constants
//
var c = require('../const');
/*:nodoc:*
* new ActionVersion(options)
* - options (object): options hash see [[Action.new]]
*
**/
var ActionVersion = module.exports = function ActionVersion(options) {
options = options || {};
options.defaultValue = (options.defaultValue ? options.defaultValue : c.SUPPRESS);
options.dest = (options.dest || c.SUPPRESS);
options.nargs = 0;
this.version = options.version;
Action.call(this, options);
};
util.inherits(ActionVersion, Action);
/*:nodoc:*
* ActionVersion#call(parser, namespace, values, optionString) -> Void
* - parser (ArgumentParser): current parser
* - namespace (Namespace): namespace for output data
* - values (Array): parsed values
* - optionString (Array): input option string(not parsed)
*
* Print version and exit
**/
ActionVersion.prototype.call = function (parser) {
var version = this.version || parser.version;
var formatter = parser._getFormatter();
formatter.addText(version);
parser.exit(0, formatter.formatHelp());
};
/** internal
* class ActionContainer
*
* Action container. Parent for [[ArgumentParser]] and [[ArgumentGroup]]
**/
'use strict';
var format = require('util').format;
// Constants
var c = require('./const');
var $$ = require('./utils');
//Actions
var ActionHelp = require('./action/help');
var ActionAppend = require('./action/append');
var ActionAppendConstant = require('./action/append/constant');
var ActionCount = require('./action/count');
var ActionStore = require('./action/store');
var ActionStoreConstant = require('./action/store/constant');
var ActionStoreTrue = require('./action/store/true');
var ActionStoreFalse = require('./action/store/false');
var ActionVersion = require('./action/version');
var ActionSubparsers = require('./action/subparsers');
// Errors
var argumentErrorHelper = require('./argument/error');
/**
* new ActionContainer(options)
*
* Action container. Parent for [[ArgumentParser]] and [[ArgumentGroup]]
*
* ##### Options:
*
* - `description` -- A description of what the program does
* - `prefixChars` -- Characters that prefix optional arguments
* - `argumentDefault` -- The default value for all arguments
* - `conflictHandler` -- The conflict handler to use for duplicate arguments
**/
var ActionContainer = module.exports = function ActionContainer(options) {
options = options || {};
this.description = options.description;
this.argumentDefault = options.argumentDefault;
this.prefixChars = options.prefixChars || '';
this.conflictHandler = options.conflictHandler;
// set up registries
this._registries = {};
// register actions
this.register('action', null, ActionStore);
this.register('action', 'store', ActionStore);
this.register('action', 'storeConst', ActionStoreConstant);
this.register('action', 'storeTrue', ActionStoreTrue);
this.register('action', 'storeFalse', ActionStoreFalse);
this.register('action', 'append', ActionAppend);
this.register('action', 'appendConst', ActionAppendConstant);
this.register('action', 'count', ActionCount);
this.register('action', 'help', ActionHelp);
this.register('action', 'version', ActionVersion);
this.register('action', 'parsers', ActionSubparsers);
// raise an exception if the conflict handler is invalid
this._getHandler();
// action storage
this._actions = [];
this._optionStringActions = {};
// groups
this._actionGroups = [];
this._mutuallyExclusiveGroups = [];
// defaults storage
this._defaults = {};
// determines whether an "option" looks like a negative number
// -1, -1.5 -5e+4
this._regexpNegativeNumber = new RegExp('^[-]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$');
// whether or not there are any optionals that look like negative
// numbers -- uses a list so it can be shared and edited
this._hasNegativeNumberOptionals = [];
};
// Groups must be required, then ActionContainer already defined
var ArgumentGroup = require('./argument/group');
var MutuallyExclusiveGroup = require('./argument/exclusive');
//
// Registration methods
//
/**
* ActionContainer#register(registryName, value, object) -> Void
* - registryName (String) : object type action|type
* - value (string) : keyword
* - object (Object|Function) : handler
*
* Register handlers
**/
ActionContainer.prototype.register = function (registryName, value, object) {
this._registries[registryName] = this._registries[registryName] || {};
this._registries[registryName][value] = object;
};
ActionContainer.prototype._registryGet = function (registryName, value, defaultValue) {
if (arguments.length < 3) {
defaultValue = null;
}
return this._registries[registryName][value] || defaultValue;
};
//
// Namespace default accessor methods
//
/**
* ActionContainer#setDefaults(options) -> Void
* - options (object):hash of options see [[Action.new]]
*
* Set defaults
**/
ActionContainer.prototype.setDefaults = function (options) {
options = options || {};
for (var property in options) {
if ($$.has(options, property)) {
this._defaults[property] = options[property];
}
}
// if these defaults match any existing arguments, replace the previous
// default on the object with the new one
this._actions.forEach(function (action) {
if ($$.has(options, action.dest)) {
action.defaultValue = options[action.dest];
}
});
};
/**
* ActionContainer#getDefault(dest) -> Mixed
* - dest (string): action destination
*
* Return action default value
**/
ActionContainer.prototype.getDefault = function (dest) {
var result = $$.has(this._defaults, dest) ? this._defaults[dest] : null;
this._actions.forEach(function (action) {
if (action.dest === dest && $$.has(action, 'defaultValue')) {
result = action.defaultValue;
}
});
return result;
};
//
// Adding argument actions
//
/**
* ActionContainer#addArgument(args, options) -> Object
* - args (String|Array): argument key, or array of argument keys
* - options (Object): action objects see [[Action.new]]
*
* #### Examples
* - addArgument([ '-f', '--foo' ], { action: 'store', defaultValue: 1, ... })
* - addArgument([ 'bar' ], { action: 'store', nargs: 1, ... })
* - addArgument('--baz', { action: 'store', nargs: 1, ... })
**/
ActionContainer.prototype.addArgument = function (args, options) {
args = args;
options = options || {};
if (typeof args === 'string') {
args = [ args ];
}
if (!Array.isArray(args)) {
throw new TypeError('addArgument first argument should be a string or an array');
}
if (typeof options !== 'object' || Array.isArray(options)) {
throw new TypeError('addArgument second argument should be a hash');
}
// if no positional args are supplied or only one is supplied and
// it doesn't look like an option string, parse a positional argument
if (!args || args.length === 1 && this.prefixChars.indexOf(args[0][0]) < 0) {
if (args && !!options.dest) {
throw new Error('dest supplied twice for positional argument');
}
options = this._getPositional(args, options);
// otherwise, we're adding an optional argument
} else {
options = this._getOptional(args, options);
}
// if no default was supplied, use the parser-level default
if (typeof options.defaultValue === 'undefined') {
var dest = options.dest;
if ($$.has(this._defaults, dest)) {
options.defaultValue = this._defaults[dest];
} else if (typeof this.argumentDefault !== 'undefined') {
options.defaultValue = this.argumentDefault;
}
}
// create the action object, and add it to the parser
var ActionClass = this._popActionClass(options);
if (typeof ActionClass !== 'function') {
throw new Error(format('Unknown action "%s".', ActionClass));
}
var action = new ActionClass(options);
// throw an error if the action type is not callable
var typeFunction = this._registryGet('type', action.type, action.type);
if (typeof typeFunction !== 'function') {
throw new Error(format('"%s" is not callable', typeFunction));
}
return this._addAction(action);
};
/**
* ActionContainer#addArgumentGroup(options) -> ArgumentGroup
* - options (Object): hash of options see [[ArgumentGroup.new]]
*
* Create new arguments groups
**/
ActionContainer.prototype.addArgumentGroup = function (options) {
var group = new ArgumentGroup(this, options);
this._actionGroups.push(group);
return group;
};
/**
* ActionContainer#addMutuallyExclusiveGroup(options) -> ArgumentGroup
* - options (Object): {required: false}
*
* Create new mutual exclusive groups
**/
ActionContainer.prototype.addMutuallyExclusiveGroup = function (options) {
var group = new MutuallyExclusiveGroup(this, options);
this._mutuallyExclusiveGroups.push(group);
return group;
};
ActionContainer.prototype._addAction = function (action) {
var self = this;
// resolve any conflicts
this._checkConflict(action);
// add to actions list
this._actions.push(action);
action.container = this;
// index the action by any option strings it has
action.optionStrings.forEach(function (optionString) {
self._optionStringActions[optionString] = action;
});
// set the flag if any option strings look like negative numbers
action.optionStrings.forEach(function (optionString) {
if (optionString.match(self._regexpNegativeNumber)) {
if (!self._hasNegativeNumberOptionals.some(Boolean)) {
self._hasNegativeNumberOptionals.push(true);
}
}
});
// return the created action
return action;
};
ActionContainer.prototype._removeAction = function (action) {
var actionIndex = this._actions.indexOf(action);
if (actionIndex >= 0) {
this._actions.splice(actionIndex, 1);
}
};
ActionContainer.prototype._addContainerActions = function (container) {
// collect groups by titles
var titleGroupMap = {};
this._actionGroups.forEach(function (group) {
if (titleGroupMap[group.title]) {
throw new Error(format('Cannot merge actions - two groups are named "%s".', group.title));
}
titleGroupMap[group.title] = group;
});
// map each action to its group
var groupMap = {};
function actionHash(action) {
// unique (hopefully?) string suitable as dictionary key
return action.getName();
}
container._actionGroups.forEach(function (group) {
// if a group with the title exists, use that, otherwise
// create a new group matching the container's group
if (!titleGroupMap[group.title]) {
titleGroupMap[group.title] = this.addArgumentGroup({
title: group.title,
description: group.description
});
}
// map the actions to their new group
group._groupActions.forEach(function (action) {
groupMap[actionHash(action)] = titleGroupMap[group.title];
});
}, this);
// add container's mutually exclusive groups
// NOTE: if add_mutually_exclusive_group ever gains title= and
// description= then this code will need to be expanded as above
var mutexGroup;
container._mutuallyExclusiveGroups.forEach(function (group) {
mutexGroup = this.addMutuallyExclusiveGroup({
required: group.required
});
// map the actions to their new mutex group
group._groupActions.forEach(function (action) {
groupMap[actionHash(action)] = mutexGroup;
});
}, this); // forEach takes a 'this' argument
// add all actions to this container or their group
container._actions.forEach(function (action) {
var key = actionHash(action);
if (groupMap[key]) {
groupMap[key]._addAction(action);
} else {
this._addAction(action);
}
});
};
ActionContainer.prototype._getPositional = function (dest, options) {
if (Array.isArray(dest)) {
dest = dest[0];
}
// make sure required is not specified
if (options.required) {
throw new Error('"required" is an invalid argument for positionals.');
}
// mark positional arguments as required if at least one is
// always required
if (options.nargs !== c.OPTIONAL && options.nargs !== c.ZERO_OR_MORE) {
options.required = true;
}
if (options.nargs === c.ZERO_OR_MORE && typeof options.defaultValue === 'undefined') {
options.required = true;
}
// return the keyword arguments with no option strings
options.dest = dest;
options.optionStrings = [];
return options;
};
ActionContainer.prototype._getOptional = function (args, options) {
var prefixChars = this.prefixChars;
var optionStrings = [];
var optionStringsLong = [];
// determine short and long option strings
args.forEach(function (optionString) {
// error on strings that don't start with an appropriate prefix
if (prefixChars.indexOf(optionString[0]) < 0) {
throw new Error(format('Invalid option string "%s": must start with a "%s".',
optionString,
prefixChars
));
}
// strings starting with two prefix characters are long options
optionStrings.push(optionString);
if (optionString.length > 1 && prefixChars.indexOf(optionString[1]) >= 0) {
optionStringsLong.push(optionString);
}
});
// infer dest, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
var dest = options.dest || null;
delete options.dest;
if (!dest) {
var optionStringDest = optionStringsLong.length ? optionStringsLong[0] : optionStrings[0];
dest = $$.trimChars(optionStringDest, this.prefixChars);
if (dest.length === 0) {
throw new Error(
format('dest= is required for options like "%s"', optionStrings.join(', '))
);
}
dest = dest.replace(/-/g, '_');
}
// return the updated keyword arguments
options.dest = dest;
options.optionStrings = optionStrings;
return options;
};
ActionContainer.prototype._popActionClass = function (options, defaultValue) {
defaultValue = defaultValue || null;
var action = (options.action || defaultValue);
delete options.action;
var actionClass = this._registryGet('action', action, action);
return actionClass;
};
ActionContainer.prototype._getHandler = function () {
var handlerString = this.conflictHandler;
var handlerFuncName = '_handleConflict' + $$.capitalize(handlerString);
var func = this[handlerFuncName];
if (typeof func === 'undefined') {
var msg = 'invalid conflict resolution value: ' + handlerString;
throw new Error(msg);
} else {
return func;
}
};
ActionContainer.prototype._checkConflict = function (action) {
var optionStringActions = this._optionStringActions;
var conflictOptionals = [];
// find all options that conflict with this option
// collect pairs, the string, and an existing action that it conflicts with
action.optionStrings.forEach(function (optionString) {
var conflOptional = optionStringActions[optionString];
if (typeof conflOptional !== 'undefined') {
conflictOptionals.push([ optionString, conflOptional ]);
}
});
if (conflictOptionals.length > 0) {
var conflictHandler = this._getHandler();
conflictHandler.call(this, action, conflictOptionals);
}
};
ActionContainer.prototype._handleConflictError = function (action, conflOptionals) {
var conflicts = conflOptionals.map(function (pair) { return pair[0]; });
conflicts = conflicts.join(', ');
throw argumentErrorHelper(
action,
format('Conflicting option string(s): %s', conflicts)
);
};
ActionContainer.prototype._handleConflictResolve = function (action, conflOptionals) {
// remove all conflicting options
var self = this;
conflOptionals.forEach(function (pair) {
var optionString = pair[0];
var conflictingAction = pair[1];
// remove the conflicting option string
var i = conflictingAction.optionStrings.indexOf(optionString);
if (i >= 0) {
conflictingAction.optionStrings.splice(i, 1);
}
delete self._optionStringActions[optionString];
// if the option now has no option string, remove it from the
// container holding it
if (conflictingAction.optionStrings.length === 0) {
conflictingAction.container._removeAction(conflictingAction);
}
});
};
'use strict';
module.exports.ArgumentParser = require('./argument_parser.js');
module.exports.Namespace = require('./namespace');
module.exports.Action = require('./action');
module.exports.HelpFormatter = require('./help/formatter.js');
module.exports.Const = require('./const.js');
module.exports.ArgumentDefaultsHelpFormatter =
require('./help/added_formatters.js').ArgumentDefaultsHelpFormatter;
module.exports.RawDescriptionHelpFormatter =
require('./help/added_formatters.js').RawDescriptionHelpFormatter;
module.exports.RawTextHelpFormatter =
require('./help/added_formatters.js').RawTextHelpFormatter;
'use strict';
var format = require('util').format;
var ERR_CODE = 'ARGError';
/*:nodoc:*
* argumentError(argument, message) -> TypeError
* - argument (Object): action with broken argument
* - message (String): error message
*
* Error format helper. An error from creating or using an argument
* (optional or positional). The string value of this exception
* is the message, augmented with information
* about the argument that caused it.
*
* #####Example
*
* var argumentErrorHelper = require('./argument/error');
* if (conflictOptionals.length > 0) {
* throw argumentErrorHelper(
* action,
* format('Conflicting option string(s): %s', conflictOptionals.join(', '))
* );
* }
*
**/
module.exports = function (argument, message) {
var argumentName = null;
var errMessage;
var err;
if (argument.getName) {
argumentName = argument.getName();
} else {
argumentName = '' + argument;
}
if (!argumentName) {
errMessage = message;
} else {
errMessage = format('argument "%s": %s', argumentName, message);
}
err = new TypeError(errMessage);
err.code = ERR_CODE;
return err;
};
/** internal
* class MutuallyExclusiveGroup
*
* Group arguments.
* By default, ArgumentParser groups command-line arguments
* into “positional arguments” and “optional arguments”
* when displaying help messages. When there is a better
* conceptual grouping of arguments than this default one,
* appropriate groups can be created using the addArgumentGroup() method
*
* This class inherited from [[ArgumentContainer]]
**/
'use strict';
var util = require('util');
var ArgumentGroup = require('./group');
/**
* new MutuallyExclusiveGroup(container, options)
* - container (object): main container
* - options (object): options.required -> true/false
*
* `required` could be an argument itself, but making it a property of
* the options argument is more consistent with the JS adaptation of the Python)
**/
var MutuallyExclusiveGroup = module.exports = function MutuallyExclusiveGroup(container, options) {
var required;
options = options || {};
required = options.required || false;
ArgumentGroup.call(this, container);
this.required = required;
};
util.inherits(MutuallyExclusiveGroup, ArgumentGroup);
MutuallyExclusiveGroup.prototype._addAction = function (action) {
var msg;
if (action.required) {
msg = 'mutually exclusive arguments must be optional';
throw new Error(msg);
}
action = this._container._addAction(action);
this._groupActions.push(action);
return action;
};
MutuallyExclusiveGroup.prototype._removeAction = function (action) {
this._container._removeAction(action);
this._groupActions.remove(action);
};
/** internal
* class ArgumentGroup
*
* Group arguments.
* By default, ArgumentParser groups command-line arguments
* into “positional arguments” and “optional arguments”
* when displaying help messages. When there is a better
* conceptual grouping of arguments than this default one,
* appropriate groups can be created using the addArgumentGroup() method
*
* This class inherited from [[ArgumentContainer]]
**/
'use strict';
var util = require('util');
var ActionContainer = require('../action_container');
/**
* new ArgumentGroup(container, options)
* - container (object): main container
* - options (object): hash of group options
*
* #### options
* - **prefixChars** group name prefix
* - **argumentDefault** default argument value
* - **title** group title
* - **description** group description
*
**/
var ArgumentGroup = module.exports = function ArgumentGroup(container, options) {
options = options || {};
// add any missing keyword arguments by checking the container
options.conflictHandler = (options.conflictHandler || container.conflictHandler);
options.prefixChars = (options.prefixChars || container.prefixChars);
options.argumentDefault = (options.argumentDefault || container.argumentDefault);
ActionContainer.call(this, options);
// group attributes
this.title = options.title;
this._groupActions = [];
// share most attributes with the container
this._container = container;
this._registries = container._registries;
this._actions = container._actions;
this._optionStringActions = container._optionStringActions;
this._defaults = container._defaults;
this._hasNegativeNumberOptionals = container._hasNegativeNumberOptionals;
this._mutuallyExclusiveGroups = container._mutuallyExclusiveGroups;
};
util.inherits(ArgumentGroup, ActionContainer);
ArgumentGroup.prototype._addAction = function (action) {
// Parent add action
action = ActionContainer.prototype._addAction.call(this, action);
this._groupActions.push(action);
return action;
};
ArgumentGroup.prototype._removeAction = function (action) {
// Parent remove action
ActionContainer.prototype._removeAction.call(this, action);
var actionIndex = this._groupActions.indexOf(action);
if (actionIndex >= 0) {
this._groupActions.splice(actionIndex, 1);
}
};
/**
* class ArgumentParser
*
* Object for parsing command line strings into js objects.
*
* Inherited from [[ActionContainer]]
**/
'use strict';
var util = require('util');
var format = require('util').format;
var Path = require('path');
var sprintf = require('sprintf-js').sprintf;
// Constants
var c = require('./const');
var $$ = require('./utils');
var ActionContainer = require('./action_container');
// Errors
var argumentErrorHelper = require('./argument/error');
var HelpFormatter = require('./help/formatter');
var Namespace = require('./namespace');
/**
* new ArgumentParser(options)
*
* Create a new ArgumentParser object.
*
* ##### Options:
* - `prog` The name of the program (default: Path.basename(process.argv[1]))
* - `usage` A usage message (default: auto-generated from arguments)
* - `description` A description of what the program does
* - `epilog` Text following the argument descriptions
* - `parents` Parsers whose arguments should be copied into this one
* - `formatterClass` HelpFormatter class for printing help messages
* - `prefixChars` Characters that prefix optional arguments
* - `fromfilePrefixChars` Characters that prefix files containing additional arguments
* - `argumentDefault` The default value for all arguments
* - `addHelp` Add a -h/-help option
* - `conflictHandler` Specifies how to handle conflicting argument names
* - `debug` Enable debug mode. Argument errors throw exception in
* debug mode and process.exit in normal. Used for development and
* testing (default: false)
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#argumentparser-objects
**/
function ArgumentParser(options) {
if (!(this instanceof ArgumentParser)) {
return new ArgumentParser(options);
}
var self = this;
options = options || {};
options.description = (options.description || null);
options.argumentDefault = (options.argumentDefault || null);
options.prefixChars = (options.prefixChars || '-');
options.conflictHandler = (options.conflictHandler || 'error');
ActionContainer.call(this, options);
options.addHelp = typeof options.addHelp === 'undefined' || !!options.addHelp;
options.parents = options.parents || [];
// default program name
options.prog = (options.prog || Path.basename(process.argv[1]));
this.prog = options.prog;
this.usage = options.usage;
this.epilog = options.epilog;
this.version = options.version;
this.debug = (options.debug === true);
this.formatterClass = (options.formatterClass || HelpFormatter);
this.fromfilePrefixChars = options.fromfilePrefixChars || null;
this._positionals = this.addArgumentGroup({ title: 'Positional arguments' });
this._optionals = this.addArgumentGroup({ title: 'Optional arguments' });
this._subparsers = null;
// register types
function FUNCTION_IDENTITY(o) {
return o;
}
this.register('type', 'auto', FUNCTION_IDENTITY);
this.register('type', null, FUNCTION_IDENTITY);
this.register('type', 'int', function (x) {
var result = parseInt(x, 10);
if (isNaN(result)) {
throw new Error(x + ' is not a valid integer.');
}
return result;
});
this.register('type', 'float', function (x) {
var result = parseFloat(x);
if (isNaN(result)) {
throw new Error(x + ' is not a valid float.');
}
return result;
});
this.register('type', 'string', function (x) {
return '' + x;
});
// add help and version arguments if necessary
var defaultPrefix = (this.prefixChars.indexOf('-') > -1) ? '-' : this.prefixChars[0];
if (options.addHelp) {
this.addArgument(
[ defaultPrefix + 'h', defaultPrefix + defaultPrefix + 'help' ],
{
action: 'help',
defaultValue: c.SUPPRESS,
help: 'Show this help message and exit.'
}
);
}
if (typeof this.version !== 'undefined') {
this.addArgument(
[ defaultPrefix + 'v', defaultPrefix + defaultPrefix + 'version' ],
{
action: 'version',
version: this.version,
defaultValue: c.SUPPRESS,
help: "Show program's version number and exit."
}
);
}
// add parent arguments and defaults
options.parents.forEach(function (parent) {
self._addContainerActions(parent);
if (typeof parent._defaults !== 'undefined') {
for (var defaultKey in parent._defaults) {
if (parent._defaults.hasOwnProperty(defaultKey)) {
self._defaults[defaultKey] = parent._defaults[defaultKey];
}
}
}
});
}
util.inherits(ArgumentParser, ActionContainer);
/**
* ArgumentParser#addSubparsers(options) -> [[ActionSubparsers]]
* - options (object): hash of options see [[ActionSubparsers.new]]
*
* See also [subcommands][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#sub-commands
**/
ArgumentParser.prototype.addSubparsers = function (options) {
if (this._subparsers) {
this.error('Cannot have multiple subparser arguments.');
}
options = options || {};
options.debug = (this.debug === true);
options.optionStrings = [];
options.parserClass = (options.parserClass || ArgumentParser);
if (!!options.title || !!options.description) {
this._subparsers = this.addArgumentGroup({
title: (options.title || 'subcommands'),
description: options.description
});
delete options.title;
delete options.description;
} else {
this._subparsers = this._positionals;
}
// prog defaults to the usage message of this parser, skipping
// optional arguments and with no "usage:" prefix
if (!options.prog) {
var formatter = this._getFormatter();
var positionals = this._getPositionalActions();
var groups = this._mutuallyExclusiveGroups;
formatter.addUsage(this.usage, positionals, groups, '');
options.prog = formatter.formatHelp().trim();
}
// create the parsers action and add it to the positionals list
var ParsersClass = this._popActionClass(options, 'parsers');
var action = new ParsersClass(options);
this._subparsers._addAction(action);
// return the created parsers action
return action;
};
ArgumentParser.prototype._addAction = function (action) {
if (action.isOptional()) {
this._optionals._addAction(action);
} else {
this._positionals._addAction(action);
}
return action;
};
ArgumentParser.prototype._getOptionalActions = function () {
return this._actions.filter(function (action) {
return action.isOptional();
});
};
ArgumentParser.prototype._getPositionalActions = function () {
return this._actions.filter(function (action) {
return action.isPositional();
});
};
/**
* ArgumentParser#parseArgs(args, namespace) -> Namespace|Object
* - args (array): input elements
* - namespace (Namespace|Object): result object
*
* Parsed args and throws error if some arguments are not recognized
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#the-parse-args-method
**/
ArgumentParser.prototype.parseArgs = function (args, namespace) {
var argv;
var result = this.parseKnownArgs(args, namespace);
args = result[0];
argv = result[1];
if (argv && argv.length > 0) {
this.error(
format('Unrecognized arguments: %s.', argv.join(' '))
);
}
return args;
};
/**
* ArgumentParser#parseKnownArgs(args, namespace) -> array
* - args (array): input options
* - namespace (Namespace|Object): result object
*
* Parse known arguments and return tuple of result object
* and unknown args
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#partial-parsing
**/
ArgumentParser.prototype.parseKnownArgs = function (args, namespace) {
var self = this;
// args default to the system args
args = args || process.argv.slice(2);
// default Namespace built from parser defaults
namespace = namespace || new Namespace();
self._actions.forEach(function (action) {
if (action.dest !== c.SUPPRESS) {
if (!$$.has(namespace, action.dest)) {
if (action.defaultValue !== c.SUPPRESS) {
var defaultValue = action.defaultValue;
if (typeof action.defaultValue === 'string') {
defaultValue = self._getValue(action, defaultValue);
}
namespace[action.dest] = defaultValue;
}
}
}
});
Object.keys(self._defaults).forEach(function (dest) {
namespace[dest] = self._defaults[dest];
});
// parse the arguments and exit if there are any errors
try {
var res = this._parseKnownArgs(args, namespace);
namespace = res[0];
args = res[1];
if ($$.has(namespace, c._UNRECOGNIZED_ARGS_ATTR)) {
args = $$.arrayUnion(args, namespace[c._UNRECOGNIZED_ARGS_ATTR]);
delete namespace[c._UNRECOGNIZED_ARGS_ATTR];
}
return [ namespace, args ];
} catch (e) {
this.error(e);
}
};
ArgumentParser.prototype._parseKnownArgs = function (argStrings, namespace) {
var self = this;
var extras = [];
// replace arg strings that are file references
if (this.fromfilePrefixChars !== null) {
argStrings = this._readArgsFromFiles(argStrings);
}
// map all mutually exclusive arguments to the other arguments
// they can't occur with
// Python has 'conflicts = action_conflicts.setdefault(mutex_action, [])'
// though I can't conceive of a way in which an action could be a member
// of two different mutually exclusive groups.
function actionHash(action) {
// some sort of hashable key for this action
// action itself cannot be a key in actionConflicts
// I think getName() (join of optionStrings) is unique enough
return action.getName();
}
var conflicts, key;
var actionConflicts = {};
this._mutuallyExclusiveGroups.forEach(function (mutexGroup) {
mutexGroup._groupActions.forEach(function (mutexAction, i, groupActions) {
key = actionHash(mutexAction);
if (!$$.has(actionConflicts, key)) {
actionConflicts[key] = [];
}
conflicts = actionConflicts[key];
conflicts.push.apply(conflicts, groupActions.slice(0, i));
conflicts.push.apply(conflicts, groupActions.slice(i + 1));
});
});
// find all option indices, and determine the arg_string_pattern
// which has an 'O' if there is an option at an index,
// an 'A' if there is an argument, or a '-' if there is a '--'
var optionStringIndices = {};
var argStringPatternParts = [];
argStrings.forEach(function (argString, argStringIndex) {
if (argString === '--') {
argStringPatternParts.push('-');
while (argStringIndex < argStrings.length) {
argStringPatternParts.push('A');
argStringIndex++;
}
} else {
// otherwise, add the arg to the arg strings
// and note the index if it was an option
var pattern;
var optionTuple = self._parseOptional(argString);
if (!optionTuple) {
pattern = 'A';
} else {
optionStringIndices[argStringIndex] = optionTuple;
pattern = 'O';
}
argStringPatternParts.push(pattern);
}
});
var argStringsPattern = argStringPatternParts.join('');
var seenActions = [];
var seenNonDefaultActions = [];
function takeAction(action, argumentStrings, optionString) {
seenActions.push(action);
var argumentValues = self._getValues(action, argumentStrings);
// error if this argument is not allowed with other previously
// seen arguments, assuming that actions that use the default
// value don't really count as "present"
if (argumentValues !== action.defaultValue) {
seenNonDefaultActions.push(action);
if (actionConflicts[actionHash(action)]) {
actionConflicts[actionHash(action)].forEach(function (actionConflict) {
if (seenNonDefaultActions.indexOf(actionConflict) >= 0) {
throw argumentErrorHelper(
action,
format('Not allowed with argument "%s".', actionConflict.getName())
);
}
});
}
}
if (argumentValues !== c.SUPPRESS) {
action.call(self, namespace, argumentValues, optionString);
}
}
function consumeOptional(startIndex) {
// get the optional identified at this index
var optionTuple = optionStringIndices[startIndex];
var action = optionTuple[0];
var optionString = optionTuple[1];
var explicitArg = optionTuple[2];
// identify additional optionals in the same arg string
// (e.g. -xyz is the same as -x -y -z if no args are required)
var actionTuples = [];
var args, argCount, start, stop;
for (;;) {
if (!action) {
extras.push(argStrings[startIndex]);
return startIndex + 1;
}
if (explicitArg) {
argCount = self._matchArgument(action, 'A');
// if the action is a single-dash option and takes no
// arguments, try to parse more single-dash options out
// of the tail of the option string
var chars = self.prefixChars;
if (argCount === 0 && chars.indexOf(optionString[1]) < 0) {
actionTuples.push([ action, [], optionString ]);
optionString = optionString[0] + explicitArg[0];
var newExplicitArg = explicitArg.slice(1) || null;
var optionalsMap = self._optionStringActions;
if (Object.keys(optionalsMap).indexOf(optionString) >= 0) {
action = optionalsMap[optionString];
explicitArg = newExplicitArg;
} else {
throw argumentErrorHelper(action, sprintf('ignored explicit argument %r', explicitArg));
}
} else if (argCount === 1) {
// if the action expect exactly one argument, we've
// successfully matched the option; exit the loop
stop = startIndex + 1;
args = [ explicitArg ];
actionTuples.push([ action, args, optionString ]);
break;
} else {
// error if a double-dash option did not use the
// explicit argument
throw argumentErrorHelper(action, sprintf('ignored explicit argument %r', explicitArg));
}
} else {
// if there is no explicit argument, try to match the
// optional's string arguments with the following strings
// if successful, exit the loop
start = startIndex + 1;
var selectedPatterns = argStringsPattern.substr(start);
argCount = self._matchArgument(action, selectedPatterns);
stop = start + argCount;
args = argStrings.slice(start, stop);
actionTuples.push([ action, args, optionString ]);
break;
}
}
// add the Optional to the list and return the index at which
// the Optional's string args stopped
if (actionTuples.length < 1) {
throw new Error('length should be > 0');
}
for (var i = 0; i < actionTuples.length; i++) {
takeAction.apply(self, actionTuples[i]);
}
return stop;
}
// the list of Positionals left to be parsed; this is modified
// by consume_positionals()
var positionals = self._getPositionalActions();
function consumePositionals(startIndex) {
// match as many Positionals as possible
var selectedPattern = argStringsPattern.substr(startIndex);
var argCounts = self._matchArgumentsPartial(positionals, selectedPattern);
// slice off the appropriate arg strings for each Positional
// and add the Positional and its args to the list
for (var i = 0; i < positionals.length; i++) {
var action = positionals[i];
var argCount = argCounts[i];
if (typeof argCount === 'undefined') {
continue;
}
var args = argStrings.slice(startIndex, startIndex + argCount);
startIndex += argCount;
takeAction(action, args);
}
// slice off the Positionals that we just parsed and return the
// index at which the Positionals' string args stopped
positionals = positionals.slice(argCounts.length);
return startIndex;
}
// consume Positionals and Optionals alternately, until we have
// passed the last option string
var startIndex = 0;
var position;
var maxOptionStringIndex = -1;
Object.keys(optionStringIndices).forEach(function (position) {
maxOptionStringIndex = Math.max(maxOptionStringIndex, parseInt(position, 10));
});
var positionalsEndIndex, nextOptionStringIndex;
while (startIndex <= maxOptionStringIndex) {
// consume any Positionals preceding the next option
nextOptionStringIndex = null;
for (position in optionStringIndices) {
if (!optionStringIndices.hasOwnProperty(position)) { continue; }
position = parseInt(position, 10);
if (position >= startIndex) {
if (nextOptionStringIndex !== null) {
nextOptionStringIndex = Math.min(nextOptionStringIndex, position);
} else {
nextOptionStringIndex = position;
}
}
}
if (startIndex !== nextOptionStringIndex) {
positionalsEndIndex = consumePositionals(startIndex);
// only try to parse the next optional if we didn't consume
// the option string during the positionals parsing
if (positionalsEndIndex > startIndex) {
startIndex = positionalsEndIndex;
continue;
} else {
startIndex = positionalsEndIndex;
}
}
// if we consumed all the positionals we could and we're not
// at the index of an option string, there were extra arguments
if (!optionStringIndices[startIndex]) {
var strings = argStrings.slice(startIndex, nextOptionStringIndex);
extras = extras.concat(strings);
startIndex = nextOptionStringIndex;
}
// consume the next optional and any arguments for it
startIndex = consumeOptional(startIndex);
}
// consume any positionals following the last Optional
var stopIndex = consumePositionals(startIndex);
// if we didn't consume all the argument strings, there were extras
extras = extras.concat(argStrings.slice(stopIndex));
// if we didn't use all the Positional objects, there were too few
// arg strings supplied.
if (positionals.length > 0) {
self.error('too few arguments');
}
// make sure all required actions were present
self._actions.forEach(function (action) {
if (action.required) {
if (seenActions.indexOf(action) < 0) {
self.error(format('Argument "%s" is required', action.getName()));
}
}
});
// make sure all required groups have one option present
var actionUsed = false;
self._mutuallyExclusiveGroups.forEach(function (group) {
if (group.required) {
actionUsed = group._groupActions.some(function (action) {
return seenNonDefaultActions.indexOf(action) !== -1;
});
// if no actions were used, report the error
if (!actionUsed) {
var names = [];
group._groupActions.forEach(function (action) {
if (action.help !== c.SUPPRESS) {
names.push(action.getName());
}
});
names = names.join(' ');
var msg = 'one of the arguments ' + names + ' is required';
self.error(msg);
}
}
});
// return the updated namespace and the extra arguments
return [ namespace, extras ];
};
ArgumentParser.prototype._readArgsFromFiles = function (argStrings) {
// expand arguments referencing files
var self = this;
var fs = require('fs');
var newArgStrings = [];
argStrings.forEach(function (argString) {
if (self.fromfilePrefixChars.indexOf(argString[0]) < 0) {
// for regular arguments, just add them back into the list
newArgStrings.push(argString);
} else {
// replace arguments referencing files with the file content
try {
var argstrs = [];
var filename = argString.slice(1);
var content = fs.readFileSync(filename, 'utf8');
content = content.trim().split('\n');
content.forEach(function (argLine) {
self.convertArgLineToArgs(argLine).forEach(function (arg) {
argstrs.push(arg);
});
argstrs = self._readArgsFromFiles(argstrs);
});
newArgStrings.push.apply(newArgStrings, argstrs);
} catch (error) {
return self.error(error.message);
}
}
});
return newArgStrings;
};
ArgumentParser.prototype.convertArgLineToArgs = function (argLine) {
return [ argLine ];
};
ArgumentParser.prototype._matchArgument = function (action, regexpArgStrings) {
// match the pattern for this action to the arg strings
var regexpNargs = new RegExp('^' + this._getNargsPattern(action));
var matches = regexpArgStrings.match(regexpNargs);
var message;
// throw an exception if we weren't able to find a match
if (!matches) {
switch (action.nargs) {
/*eslint-disable no-undefined*/
case undefined:
case null:
message = 'Expected one argument.';
break;
case c.OPTIONAL:
message = 'Expected at most one argument.';
break;
case c.ONE_OR_MORE:
message = 'Expected at least one argument.';
break;
default:
message = 'Expected %s argument(s)';
}
throw argumentErrorHelper(
action,
format(message, action.nargs)
);
}
// return the number of arguments matched
return matches[1].length;
};
ArgumentParser.prototype._matchArgumentsPartial = function (actions, regexpArgStrings) {
// progressively shorten the actions list by slicing off the
// final actions until we find a match
var self = this;
var result = [];
var actionSlice, pattern, matches;
var i, j;
function getLength(string) {
return string.length;
}
for (i = actions.length; i > 0; i--) {
pattern = '';
actionSlice = actions.slice(0, i);
for (j = 0; j < actionSlice.length; j++) {
pattern += self._getNargsPattern(actionSlice[j]);
}
pattern = new RegExp('^' + pattern);
matches = regexpArgStrings.match(pattern);
if (matches && matches.length > 0) {
// need only groups
matches = matches.splice(1);
result = result.concat(matches.map(getLength));
break;
}
}
// return the list of arg string counts
return result;
};
ArgumentParser.prototype._parseOptional = function (argString) {
var action, optionString, argExplicit, optionTuples;
// if it's an empty string, it was meant to be a positional
if (!argString) {
return null;
}
// if it doesn't start with a prefix, it was meant to be positional
if (this.prefixChars.indexOf(argString[0]) < 0) {
return null;
}
// if the option string is present in the parser, return the action
if (this._optionStringActions[argString]) {
return [ this._optionStringActions[argString], argString, null ];
}
// if it's just a single character, it was meant to be positional
if (argString.length === 1) {
return null;
}
// if the option string before the "=" is present, return the action
if (argString.indexOf('=') >= 0) {
optionString = argString.split('=', 1)[0];
argExplicit = argString.slice(optionString.length + 1);
if (this._optionStringActions[optionString]) {
action = this._optionStringActions[optionString];
return [ action, optionString, argExplicit ];
}
}
// search through all possible prefixes of the option string
// and all actions in the parser for possible interpretations
optionTuples = this._getOptionTuples(argString);
// if multiple actions match, the option string was ambiguous
if (optionTuples.length > 1) {
var optionStrings = optionTuples.map(function (optionTuple) {
return optionTuple[1];
});
this.error(format(
'Ambiguous option: "%s" could match %s.',
argString, optionStrings.join(', ')
));
// if exactly one action matched, this segmentation is good,
// so return the parsed action
} else if (optionTuples.length === 1) {
return optionTuples[0];
}
// if it was not found as an option, but it looks like a negative
// number, it was meant to be positional
// unless there are negative-number-like options
if (argString.match(this._regexpNegativeNumber)) {
if (!this._hasNegativeNumberOptionals.some(Boolean)) {
return null;
}
}
// if it contains a space, it was meant to be a positional
if (argString.search(' ') >= 0) {
return null;
}
// it was meant to be an optional but there is no such option
// in this parser (though it might be a valid option in a subparser)
return [ null, argString, null ];
};
ArgumentParser.prototype._getOptionTuples = function (optionString) {
var result = [];
var chars = this.prefixChars;
var optionPrefix;
var argExplicit;
var action;
var actionOptionString;
// option strings starting with two prefix characters are only split at
// the '='
if (chars.indexOf(optionString[0]) >= 0 && chars.indexOf(optionString[1]) >= 0) {
if (optionString.indexOf('=') >= 0) {
var optionStringSplit = optionString.split('=', 1);
optionPrefix = optionStringSplit[0];
argExplicit = optionStringSplit[1];
} else {
optionPrefix = optionString;
argExplicit = null;
}
for (actionOptionString in this._optionStringActions) {
if (actionOptionString.substr(0, optionPrefix.length) === optionPrefix) {
action = this._optionStringActions[actionOptionString];
result.push([ action, actionOptionString, argExplicit ]);
}
}
// single character options can be concatenated with their arguments
// but multiple character options always have to have their argument
// separate
} else if (chars.indexOf(optionString[0]) >= 0 && chars.indexOf(optionString[1]) < 0) {
optionPrefix = optionString;
argExplicit = null;
var optionPrefixShort = optionString.substr(0, 2);
var argExplicitShort = optionString.substr(2);
for (actionOptionString in this._optionStringActions) {
if (!$$.has(this._optionStringActions, actionOptionString)) continue;
action = this._optionStringActions[actionOptionString];
if (actionOptionString === optionPrefixShort) {
result.push([ action, actionOptionString, argExplicitShort ]);
} else if (actionOptionString.substr(0, optionPrefix.length) === optionPrefix) {
result.push([ action, actionOptionString, argExplicit ]);
}
}
// shouldn't ever get here
} else {
throw new Error(format('Unexpected option string: %s.', optionString));
}
// return the collected option tuples
return result;
};
ArgumentParser.prototype._getNargsPattern = function (action) {
// in all examples below, we have to allow for '--' args
// which are represented as '-' in the pattern
var regexpNargs;
switch (action.nargs) {
// the default (null) is assumed to be a single argument
case undefined:
case null:
regexpNargs = '(-*A-*)';
break;
// allow zero or more arguments
case c.OPTIONAL:
regexpNargs = '(-*A?-*)';
break;
// allow zero or more arguments
case c.ZERO_OR_MORE:
regexpNargs = '(-*[A-]*)';
break;
// allow one or more arguments
case c.ONE_OR_MORE:
regexpNargs = '(-*A[A-]*)';
break;
// allow any number of options or arguments
case c.REMAINDER:
regexpNargs = '([-AO]*)';
break;
// allow one argument followed by any number of options or arguments
case c.PARSER:
regexpNargs = '(-*A[-AO]*)';
break;
// all others should be integers
default:
regexpNargs = '(-*' + $$.repeat('-*A', action.nargs) + '-*)';
}
// if this is an optional action, -- is not allowed
if (action.isOptional()) {
regexpNargs = regexpNargs.replace(/-\*/g, '');
regexpNargs = regexpNargs.replace(/-/g, '');
}
// return the pattern
return regexpNargs;
};
//
// Value conversion methods
//
ArgumentParser.prototype._getValues = function (action, argStrings) {
var self = this;
// for everything but PARSER args, strip out '--'
if (action.nargs !== c.PARSER && action.nargs !== c.REMAINDER) {
argStrings = argStrings.filter(function (arrayElement) {
return arrayElement !== '--';
});
}
var value, argString;
// optional argument produces a default when not present
if (argStrings.length === 0 && action.nargs === c.OPTIONAL) {
value = (action.isOptional()) ? action.constant : action.defaultValue;
if (typeof (value) === 'string') {
value = this._getValue(action, value);
this._checkValue(action, value);
}
// when nargs='*' on a positional, if there were no command-line
// args, use the default if it is anything other than None
} else if (argStrings.length === 0 && action.nargs === c.ZERO_OR_MORE &&
action.optionStrings.length === 0) {
value = (action.defaultValue || argStrings);
this._checkValue(action, value);
// single argument or optional argument produces a single value
} else if (argStrings.length === 1 &&
(!action.nargs || action.nargs === c.OPTIONAL)) {
argString = argStrings[0];
value = this._getValue(action, argString);
this._checkValue(action, value);
// REMAINDER arguments convert all values, checking none
} else if (action.nargs === c.REMAINDER) {
value = argStrings.map(function (v) {
return self._getValue(action, v);
});
// PARSER arguments convert all values, but check only the first
} else if (action.nargs === c.PARSER) {
value = argStrings.map(function (v) {
return self._getValue(action, v);
});
this._checkValue(action, value[0]);
// all other types of nargs produce a list
} else {
value = argStrings.map(function (v) {
return self._getValue(action, v);
});
value.forEach(function (v) {
self._checkValue(action, v);
});
}
// return the converted value
return value;
};
ArgumentParser.prototype._getValue = function (action, argString) {
var result;
var typeFunction = this._registryGet('type', action.type, action.type);
if (typeof typeFunction !== 'function') {
var message = format('%s is not callable', typeFunction);
throw argumentErrorHelper(action, message);
}
// convert the value to the appropriate type
try {
result = typeFunction(argString);
// ArgumentTypeErrors indicate errors
// If action.type is not a registered string, it is a function
// Try to deduce its name for inclusion in the error message
// Failing that, include the error message it raised.
} catch (e) {
var name = null;
if (typeof action.type === 'string') {
name = action.type;
} else {
name = action.type.name || action.type.displayName || '<function>';
}
var msg = format('Invalid %s value: %s', name, argString);
if (name === '<function>') { msg += '\n' + e.message; }
throw argumentErrorHelper(action, msg);
}
// return the converted value
return result;
};
ArgumentParser.prototype._checkValue = function (action, value) {
// converted value must be one of the choices (if specified)
var choices = action.choices;
if (choices) {
// choise for argument can by array or string
if ((typeof choices === 'string' || Array.isArray(choices)) &&
choices.indexOf(value) !== -1) {
return;
}
// choise for subparsers can by only hash
if (typeof choices === 'object' && !Array.isArray(choices) && choices[value]) {
return;
}
if (typeof choices === 'string') {
choices = choices.split('').join(', ');
} else if (Array.isArray(choices)) {
choices = choices.join(', ');
} else {
choices = Object.keys(choices).join(', ');
}
var message = format('Invalid choice: %s (choose from [%s])', value, choices);
throw argumentErrorHelper(action, message);
}
};
//
// Help formatting methods
//
/**
* ArgumentParser#formatUsage -> string
*
* Return usage string
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#printing-help
**/
ArgumentParser.prototype.formatUsage = function () {
var formatter = this._getFormatter();
formatter.addUsage(this.usage, this._actions, this._mutuallyExclusiveGroups);
return formatter.formatHelp();
};
/**
* ArgumentParser#formatHelp -> string
*
* Return help
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#printing-help
**/
ArgumentParser.prototype.formatHelp = function () {
var formatter = this._getFormatter();
// usage
formatter.addUsage(this.usage, this._actions, this._mutuallyExclusiveGroups);
// description
formatter.addText(this.description);
// positionals, optionals and user-defined groups
this._actionGroups.forEach(function (actionGroup) {
formatter.startSection(actionGroup.title);
formatter.addText(actionGroup.description);
formatter.addArguments(actionGroup._groupActions);
formatter.endSection();
});
// epilog
formatter.addText(this.epilog);
// determine help from format above
return formatter.formatHelp();
};
ArgumentParser.prototype._getFormatter = function () {
var FormatterClass = this.formatterClass;
var formatter = new FormatterClass({ prog: this.prog });
return formatter;
};
//
// Print functions
//
/**
* ArgumentParser#printUsage() -> Void
*
* Print usage
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#printing-help
**/
ArgumentParser.prototype.printUsage = function () {
this._printMessage(this.formatUsage());
};
/**
* ArgumentParser#printHelp() -> Void
*
* Print help
*
* See also [original guide][1]
*
* [1]:http://docs.python.org/dev/library/argparse.html#printing-help
**/
ArgumentParser.prototype.printHelp = function () {
this._printMessage(this.formatHelp());
};
ArgumentParser.prototype._printMessage = function (message, stream) {
if (!stream) {
stream = process.stdout;
}
if (message) {
stream.write('' + message);
}
};
//
// Exit functions
//
/**
* ArgumentParser#exit(status=0, message) -> Void
* - status (int): exit status
* - message (string): message
*
* Print message in stderr/stdout and exit program
**/
ArgumentParser.prototype.exit = function (status, message) {
if (message) {
if (status === 0) {
this._printMessage(message);
} else {
this._printMessage(message, process.stderr);
}
}
process.exit(status);
};
/**
* ArgumentParser#error(message) -> Void
* - err (Error|string): message
*
* Error method Prints a usage message incorporating the message to stderr and
* exits. If you override this in a subclass,
* it should not return -- it should
* either exit or throw an exception.
*
**/
ArgumentParser.prototype.error = function (err) {
var message;
if (err instanceof Error) {
if (this.debug === true) {
throw err;
}
message = err.message;
} else {
message = err;
}
var msg = format('%s: error: %s', this.prog, message) + c.EOL;
if (this.debug === true) {
throw new Error(msg);
}
this.printUsage(process.stderr);
return this.exit(2, msg);
};
module.exports = ArgumentParser;
//
// Constants
//
'use strict';
module.exports.EOL = '\n';
module.exports.SUPPRESS = '==SUPPRESS==';
module.exports.OPTIONAL = '?';
module.exports.ZERO_OR_MORE = '*';
module.exports.ONE_OR_MORE = '+';
module.exports.PARSER = 'A...';
module.exports.REMAINDER = '...';
module.exports._UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment