[Pkg-javascript-commits] [node-argparse] 01/01: Initial import of argparse version 0.1.15
matthew pideil
mpideil-guest at moszumanska.debian.org
Sun Apr 13 14:55:31 UTC 2014
This is an automated email from the git hooks/post-receive script.
mpideil-guest pushed a commit to branch master
in repository node-argparse.
commit 3a374c5de09babc91ae03624ebbdb7cc116ac640
Author: Matthew Pideil <matthewp_debian at teledetection.fr>
Date: Sun Apr 13 14:54:24 2014 +0000
Initial import of argparse version 0.1.15
---
.gitignore | 3 +
.jshintignore | 4 +
.jshintrc | 73 +++
.ndocrc | 17 +
.npmignore | 5 +
.travis.yml | 6 +
HISTORY.md | 109 ++++
LICENSE | 21 +
Makefile | 104 ++++
README.md | 239 +++++++++
examples/arguments.js | 36 ++
examples/choice.js | 22 +
examples/constants.js | 59 +++
examples/help.js | 13 +
examples/nargs.js | 33 ++
examples/parents.js | 28 +
examples/prefix_chars.js | 23 +
examples/sub_commands.js | 49 ++
examples/sum.js | 35 ++
examples/testformatters.js | 270 ++++++++++
index.js | 1 +
lib/action.js | 146 ++++++
lib/action/append.js | 55 ++
lib/action/append/constant.js | 47 ++
lib/action/count.js | 40 ++
lib/action/help.js | 48 ++
lib/action/store.js | 50 ++
lib/action/store/constant.js | 43 ++
lib/action/store/false.js | 27 +
lib/action/store/true.js | 26 +
lib/action/subparsers.js | 148 ++++++
lib/action/version.js | 50 ++
lib/action_container.js | 481 +++++++++++++++++
lib/argparse.js | 14 +
lib/argument/error.js | 50 ++
lib/argument/exclusive.js | 54 ++
lib/argument/group.js | 75 +++
lib/argument_parser.js | 1165 +++++++++++++++++++++++++++++++++++++++++
lib/const.js | 18 +
lib/help/added_formatters.js | 88 ++++
lib/help/formatter.js | 803 ++++++++++++++++++++++++++++
lib/namespace.js | 77 +++
package.json | 26 +
test/base.js | 247 +++++++++
test/childgroups.js | 60 +++
test/choices.js | 70 +++
test/conflict.js | 97 ++++
test/constant.js | 59 +++
test/fixtures/hello | 2 +
test/fixtures/invalid | 2 +
test/fixtures/recursive | 3 +
test/formatters.js | 259 +++++++++
test/fromfile.js | 77 +++
test/group.js | 183 +++++++
test/mocha.opts | 1 +
test/nargs.js | 860 ++++++++++++++++++++++++++++++
test/optionals.js | 825 +++++++++++++++++++++++++++++
test/parents.js | 44 ++
test/positionals.js | 93 ++++
test/prefix.js | 223 ++++++++
test/sub_commands.js | 86 +++
test/suppress.js | 78 +++
test/user_defined_type.js | 119 +++++
63 files changed, 8069 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..766c968
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/tmp/
+/node_modules/
+/doc/
diff --git a/.jshintignore b/.jshintignore
new file mode 100644
index 0000000..9c80d0b
--- /dev/null
+++ b/.jshintignore
@@ -0,0 +1,4 @@
+.git/
+doc/
+node_modules/
+tmp/
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..7f1c6ef
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,73 @@
+{
+ // Enforcing Options /////////////////////////////////////////////////////////
+
+ "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
+ "curly" : true, // Require {} for every new block or scope.
+ "eqeqeq" : true, // Require triple equals i.e. `===`.
+ "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : true, // Prohibit hariable use before definition.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "noempty" : true, // Prohibit use of empty blocks.
+ "nonew" : true, // Prohibit use of constructors for side-effects.
+ "plusplus" : false, // Prohibit use of `++` & `--`.
+ "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+ "unused" : true, // Warns when you define and never use your variables
+ "strict" : true, // Require `use strict` pragma in every file.
+ "trailing" : true, // Prohibit trailing whitespaces.
+
+ // Relaxing Options //////////////////////////////////////////////////////////
+
+ "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "eqnull" : false, // Tolerate use of `== null`.
+ //"es5" : true, // Allow ECMAScript 5 syntax.
+ "esnext" : false, // Allow ES.next specific features such as const and let
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : false, // Tolerate `ExpressionStatement` as Programs.
+ "funcscope" : false, // Tolerate declaring variables inside of control structures while accessing them later
+ "globalstrict" : true, // Allow global "use strict" (also enables 'strict').
+ "iterator" : false, // Allow usage of __iterator__ property.
+ "lastsemic" : false, // Tolerate semicolon omited for the last statement.
+ "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "laxcomma" : true, // This option suppresses warnings about comma-first coding style
+ "loopfunc" : false, // Allow functions to be defined within loops.
+ "multistr" : false, // Tolerate multi-line strings.
+ "onecase" : false, // Tolerate swithes with only one case.
+ "proto" : false, // Allow usage of __proto__ property.
+ "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
+ "scripturl" : true, // Tolerate script-targeted URLs.
+ "smarttabs" : false, // Allow mixed tabs and spaces when the latter are used for alignmnent only.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "supernew" : true, // Tolerate `new function () { ... };` and `new Object;`.
+
+ // Environments //////////////////////////////////////////////////////////////
+
+ "browser" : false, // Defines globals exposed by modern browsers
+ "couch" : false, // Defines globals exposed by CouchDB
+ "devel" : false, // Allow developments statements e.g. `console.log();`.
+ "dojo" : false, // Defines globals exposed by the Dojo Toolkit
+ "jquery" : false, // Defines globals exposed by the jQuery
+ "mootools" : false, // Defines globals exposed by the MooTools
+ "node" : true, // Defines globals exposed when running under Node.JS
+ "nonstandard" : false, // Defines non-standard but widely adopted globals such as escape and unescape
+ "prototypejs" : false, // Defines globals exposed by the Prototype
+ "rhino" : false, // Defines globals exposed when running under Rhino
+ "wsh" : false, // Defines globals exposed when running under WSH
+
+ // Legacy ////////////////////////////////////////////////////////////////////
+
+ "nomen" : false, // Prohibit use of initial or trailing underbars in names.
+ "onevar" : false, // Allow only one `var` statement per function.
+ "passfail" : false, // Stop on first error.
+ "white" : false, // Check against strict whitespace and indentation rules.
+
+ // Undocumented //////////////////////////////////////////////////////////////
+
+ "maxerr" : 100, // Maximum error before stopping.
+ "indent" : 2 // Specify indentation spacing
+}
diff --git a/.ndocrc b/.ndocrc
new file mode 100644
index 0000000..996780f
--- /dev/null
+++ b/.ndocrc
@@ -0,0 +1,17 @@
+#
+# Common nodeca config
+################################################################################
+
+--index "./README.md"
+--package "./package.json"
+--gh-ribbon "{package.homepage}"
+--output "doc"
+--render "html"
+--broken-links "throw"
+
+
+#
+# Paths with sources
+################################################################################
+
+lib
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..4d7c43c
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,5 @@
+/doc/
+/test/
+/.*
+/Makefile
+
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..24fecec
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
+before_script: "make dev-deps"
+script: "make test"
diff --git a/HISTORY.md b/HISTORY.md
new file mode 100644
index 0000000..102f82b
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,109 @@
+0.1.15 / 2013-05-13
+-------------------
+
+* Fixed #55, @trebor89
+
+
+0.1.14 / 2013-05-12
+-------------------
+
+* Fixed #62, @maxtaco
+
+
+0.1.13 / 2013-04-08
+-------------------
+
+* Added `.npmignore` to reduce package size
+
+
+0.1.12 / 2013-02-10
+-------------------
+
+* Fixed conflictHandler (#46), @hpaulj
+
+
+0.1.11 / 2013-02-07
+-------------------
+
+* Multiple bugfixes, @hpaulj
+* Added 70+ tests (ported from python), @hpaulj
+* Added conflictHandler, @applepicke
+* Added fromfilePrefixChar, @hpaulj
+
+
+0.1.10 / 2012-12-30
+-------------------
+
+* Added [mutual exclusion](http://docs.python.org/dev/library/argparse.html#mutual-exclusion)
+ support, thanks to @hpaulj
+* Fixed options check for `storeConst` & `appendConst` actions, thanks to @hpaulj
+
+
+0.1.9 / 2012-12-27
+------------------
+
+* Fixed option dest interferens with other options (issue #23), thanks to @hpaulj
+* Fixed default value behavior with `*` positionals, thanks to @hpaulj
+* Improve `getDefault()` behavior, thanks to @hpaulj
+* Imrove negative argument parsing, thanks to @hpaulj
+
+
+0.1.8 / 2012-12-01
+------------------
+
+* Fixed parser parents (issue #19), thanks to @hpaulj
+* Fixed negative argument parse (issue #20), thanks to @hpaulj
+
+
+0.1.7 / 2012-10-14
+------------------
+
+* Fixed 'choices' argument parse (issue #16)
+* Fixed stderr output (issue #15)
+
+
+0.1.6 / 2012-09-09
+------------------
+
+* Fixed check for conflict of options (thanks to @tomxtobin)
+
+
+0.1.5 / 2012-09-03
+------------------
+
+* Fix parser #setDefaults method (thanks to @tomxtobin)
+
+
+0.1.4 / 2012-07-30
+------------------
+
+* Fixed pseudo-argument support (thanks to @CGamesPlay)
+* Fixed addHelp default (should be true), if not set (thanks to @benblank)
+
+
+0.1.3 / 2012-06-27
+------------------
+
+* Fixed formatter api name: Formatter -> HelpFormatter
+
+
+0.1.2 / 2012-05-29
+------------------
+
+* Added basic tests
+* Removed excess whitespace in help
+* Fixed error reporting, when parcer with subcommands
+ called with empty arguments
+
+
+0.1.1 / 2012-05-23
+------------------
+
+* Fixed line wrapping in help formatter
+* Added better error reporting on invalid arguments
+
+
+0.1.0 / 2012-05-16
+------------------
+
+* First release.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..1afdae5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+(The MIT License)
+
+Copyright (C) 2012 by Vitaly Puzrin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..69f36d7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,104 @@
+PATH := ./node_modules/.bin:${PATH}
+
+NPM_PACKAGE := $(shell node -e 'process.stdout.write(require("./package.json").name)')
+NPM_VERSION := $(shell node -e 'process.stdout.write(require("./package.json").version)')
+
+TMP_PATH := /tmp/${NPM_PACKAGE}-$(shell date +%s)
+
+REMOTE_NAME ?= origin
+REMOTE_REPO ?= $(shell git config --get remote.${REMOTE_NAME}.url)
+
+CURR_HEAD := $(firstword $(shell git show-ref --hash HEAD | cut --bytes=-6) master)
+GITHUB_PROJ := nodeca/${NPM_PACKAGE}
+
+
+help:
+ echo "make help - Print this help"
+ echo "make lint - Lint sources with JSHint"
+ echo "make test - Lint sources and run all tests"
+ echo "make doc - Build API docs"
+ echo "make dev-deps - Install developer dependencies"
+ echo "make gh-pages - Build and push API docs into gh-pages branch"
+ echo "make publish - Set new version tag and publish npm package"
+ echo "make todo - Find and list all TODOs"
+
+
+lint:
+ if test ! `which jshint` ; then \
+ echo "You need 'jshint' installed in order to run lint." >&2 ; \
+ echo " $ make dev-deps" >&2 ; \
+ exit 128 ; \
+ fi
+ jshint . --show-non-errors
+
+
+test: lint
+ @if test ! `which mocha` ; then \
+ echo "You need 'mocha' installed in order to run tests." >&2 ; \
+ echo " $ make dev-deps" >&2 ; \
+ exit 128 ; \
+ fi
+ NODE_ENV=test mocha
+
+
+doc:
+ @if test ! `which ndoc` ; then \
+ echo "You need 'ndoc' installed in order to generate docs." >&2 ; \
+ echo " $ npm install -g ndoc" >&2 ; \
+ exit 128 ; \
+ fi
+ rm -rf ./doc
+ ndoc --link-format "{package.homepage}/blob/${CURR_HEAD}/{file}#L{line}"
+
+
+dev-deps:
+ @if test ! `which npm` ; then \
+ echo "You need 'npm' installed." >&2 ; \
+ echo " See: http://npmjs.org/" >&2 ; \
+ exit 128 ; \
+ fi
+ npm install -g jshint
+ npm install
+
+
+gh-pages:
+ @if test -z ${REMOTE_REPO} ; then \
+ echo 'Remote repo URL not found' >&2 ; \
+ exit 128 ; \
+ fi
+ $(MAKE) doc && \
+ cp -r ./doc ${TMP_PATH} && \
+ touch ${TMP_PATH}/.nojekyll
+ cd ${TMP_PATH} && \
+ git init && \
+ git add . && \
+ git commit -q -m 'Recreated docs'
+ cd ${TMP_PATH} && \
+ git remote add remote ${REMOTE_REPO} && \
+ git push --force remote +master:gh-pages
+ rm -rf ${TMP_PATH}
+
+
+publish:
+ @if test 0 -ne `git status --porcelain | wc -l` ; then \
+ echo "Unclean working tree. Commit or stash changes first." >&2 ; \
+ exit 128 ; \
+ fi
+ @if test 0 -ne `git fetch ; git status | grep '^# Your branch' | wc -l` ; then \
+ echo "Local/Remote history differs. Please push/pull changes." >&2 ; \
+ exit 128 ; \
+ fi
+ @if test 0 -ne `git tag -l ${NPM_VERSION} | wc -l` ; then \
+ echo "Tag ${NPM_VERSION} exists. Update package.json" >&2 ; \
+ exit 128 ; \
+ fi
+ git tag ${NPM_VERSION} && git push origin ${NPM_VERSION}
+ npm publish https://github.com/${GITHUB_PROJ}/tarball/${NPM_VERSION}
+
+
+todo:
+ grep 'TODO' -n -r ./lib 2>/dev/null || test true
+
+
+.PHONY: publish lint test doc dev-deps gh-pages todo
+.SILENT: help lint test doc todo
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f20e0c1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,239 @@
+argparse
+========
+
+[![Build Status](https://secure.travis-ci.org/nodeca/argparse.png?branch=master)](http://travis-ci.org/nodeca/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.** Method names changed to camelCase. See [generated docs](http://nodeca.github.com/argparse/).
+
+
+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'
+ }
+);
+var args = parser.parseArgs();
+console.dir(args);
+```
+
+Display help:
+
+```
+$ ./test.js -h
+usage: example.js [-h] [-v] [-f FOO] [-b BAR]
+
+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
+```
+
+Parse arguments:
+
+```
+$ ./test.js -f=3 --bar=4
+{ foo: '3', bar: '4' }
+```
+
+More [examples](https://github.com/nodeca/argparse/tree/master/examples).
+
+
+ArgumentParser objects
+======================
+
+```
+new ArgumentParser({paramters 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: None)
+- ```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: sys.argv[0])
+- ```usage``` - The string describing the program usage (default: generated)
+- ```conflictHandler``` - Usually unnecessary, defines strategy for resolving conflicting optionals.
+
+**Not supportied 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([names or flags], {options})
+```
+
+Defines how a single command-line argument should be parsed.
+
+- ```name or flags``` - Either a name or a list of option strings, e.g. foo or -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.
+
+
diff --git a/examples/arguments.js b/examples/arguments.js
new file mode 100755
index 0000000..5b090fa
--- /dev/null
+++ b/examples/arguments.js
@@ -0,0 +1,36 @@
+#!/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: arguments'
+});
+parser.addArgument(
+ [ '-f', '--foo' ],
+ {
+ help: 'foo bar'
+ }
+);
+parser.addArgument(
+ [ '-b', '--bar' ],
+ {
+ help: 'bar foo'
+ }
+);
+
+
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs('-f 1 -b2'.split(' '));
+console.dir(args);
+console.log('-----------');
+args = parser.parseArgs('-f=3 --bar=4'.split(' '));
+console.dir(args);
+console.log('-----------');
+args = parser.parseArgs('--foo 5 --bar 6'.split(' '));
+console.dir(args);
+console.log('-----------');
diff --git a/examples/choice.js b/examples/choice.js
new file mode 100755
index 0000000..2616fa4
--- /dev/null
+++ b/examples/choice.js
@@ -0,0 +1,22 @@
+#!/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: choice'
+});
+
+parser.addArgument(['foo'], {choices: 'abc'});
+
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs(['c']);
+console.dir(args);
+console.log('-----------');
+parser.parseArgs(['X']);
+console.dir(args);
+
diff --git a/examples/constants.js b/examples/constants.js
new file mode 100755
index 0000000..172a4f3
--- /dev/null
+++ b/examples/constants.js
@@ -0,0 +1,59 @@
+#!/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: constant'
+});
+
+parser.addArgument(
+ [ '-a'],
+ {
+ action: 'storeConst',
+ dest: 'answer',
+ help: 'store constant',
+ constant: 42
+ }
+);
+parser.addArgument(
+ [ '--str' ],
+ {
+ action: 'appendConst',
+ dest: 'types',
+ help: 'append constant "str" to types',
+ constant: 'str'
+ }
+);
+parser.addArgument(
+ [ '--int' ],
+ {
+ action: 'appendConst',
+ dest: 'types',
+ help: 'append constant "int" to types',
+ constant: 'int'
+ }
+);
+
+parser.addArgument(
+ [ '--true' ],
+ {
+ action: 'storeTrue',
+ help: 'store true constant'
+ }
+);
+parser.addArgument(
+ [ '--false' ],
+ {
+ action: 'storeFalse',
+ help: 'store false constant'
+ }
+);
+
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs('-a --str --int --true'.split(' '));
+console.dir(args);
diff --git a/examples/help.js b/examples/help.js
new file mode 100755
index 0000000..7eb9555
--- /dev/null
+++ b/examples/help.js
@@ -0,0 +1,13 @@
+#!/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: help',
+ epilog: 'help epilog',
+ prog: 'help_example_prog',
+ usage: 'Usage %(prog)s <agrs>'
+});
+parser.printHelp();
diff --git a/examples/nargs.js b/examples/nargs.js
new file mode 100755
index 0000000..74f376b
--- /dev/null
+++ b/examples/nargs.js
@@ -0,0 +1,33 @@
+#!/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: nargs'
+});
+parser.addArgument(
+ [ '-f', '--foo' ],
+ {
+ help: 'foo bar',
+ nargs: 1
+ }
+);
+parser.addArgument(
+ [ '-b', '--bar' ],
+ {
+ help: 'bar foo',
+ nargs: '*'
+ }
+);
+
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs('--foo a --bar c d'.split(' '));
+console.dir(args);
+console.log('-----------');
+args = parser.parseArgs('--bar b c f --foo a'.split(' '));
+console.dir(args);
diff --git a/examples/parents.js b/examples/parents.js
new file mode 100755
index 0000000..dfe8968
--- /dev/null
+++ b/examples/parents.js
@@ -0,0 +1,28 @@
+#!/usr/bin/env node
+'use strict';
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+var args;
+var parent_parser = new ArgumentParser({ addHelp: false });
+// note addHelp:false to prevent duplication of the -h option
+parent_parser.addArgument(
+ ['--parent'],
+ { type: 'int', description: 'parent' }
+);
+
+var foo_parser = new ArgumentParser({
+ parents: [ parent_parser ],
+ description: 'child1'
+});
+foo_parser.addArgument(['foo']);
+args = foo_parser.parseArgs(['--parent', '2', 'XXX']);
+console.log(args);
+
+var bar_parser = new ArgumentParser({
+ parents: [ parent_parser ],
+ description: 'child2'
+});
+bar_parser.addArgument(['--bar']);
+args = bar_parser.parseArgs(['--bar', 'YYY']);
+console.log(args);
diff --git a/examples/prefix_chars.js b/examples/prefix_chars.js
new file mode 100755
index 0000000..430d5e1
--- /dev/null
+++ b/examples/prefix_chars.js
@@ -0,0 +1,23 @@
+#!/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: prefix_chars',
+ prefixChars: '-+'
+});
+parser.addArgument(['+f', '++foo']);
+parser.addArgument(['++bar'], {action: 'storeTrue'});
+
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs(['+f', '1']);
+console.dir(args);
+args = parser.parseArgs(['++bar']);
+console.dir(args);
+args = parser.parseArgs(['++foo', '2', '++bar']);
+console.dir(args);
diff --git a/examples/sub_commands.js b/examples/sub_commands.js
new file mode 100755
index 0000000..df9c494
--- /dev/null
+++ b/examples/sub_commands.js
@@ -0,0 +1,49 @@
+#!/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, help: 'c1 help'});
+bar.addArgument(
+ [ '-f', '--foo' ],
+ {
+ action: 'store',
+ help: 'foo3 bar3'
+ }
+);
+var bar = subparsers.addParser(
+ 'c2',
+ {aliases: ['co'], addHelp: true, help: 'c2 help'}
+);
+bar.addArgument(
+ [ '-b', '--bar' ],
+ {
+ action: 'store',
+ type: 'int',
+ help: 'foo3 bar3'
+ }
+);
+parser.printHelp();
+console.log('-----------');
+
+var args;
+args = parser.parseArgs('c1 -f 2'.split(' '));
+console.dir(args);
+console.log('-----------');
+args = parser.parseArgs('c2 -b 1'.split(' '));
+console.dir(args);
+console.log('-----------');
+args = parser.parseArgs('co -b 1'.split(' '));
+console.dir(args);
+console.log('-----------');
+parser.parseArgs(['c1', '-h']);
diff --git a/examples/sum.js b/examples/sum.js
new file mode 100755
index 0000000..4532800
--- /dev/null
+++ b/examples/sum.js
@@ -0,0 +1,35 @@
+#!/usr/bin/env node
+
+'use strict';
+
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+var parser = new ArgumentParser({ description: 'Process some integers.' });
+
+
+function sum(arr) {
+ return arr.reduce(function (a, b) {
+ return a + b;
+ }, 0);
+}
+function max(arr) {
+ return Math.max.apply(Math, arr);
+}
+
+
+parser.addArgument(['integers'], {
+ metavar: 'N',
+ type: 'int',
+ nargs: '+',
+ help: 'an integer for the accumulator'
+});
+parser.addArgument(['--sum'], {
+ dest: 'accumulate',
+ action: 'storeConst',
+ constant: sum,
+ defaultValue: max,
+ help: 'sum the integers (default: find the max)'
+});
+
+var args = parser.parseArgs('--sum 1 2 -1'.split(' '));
+console.log(args.accumulate(args.integers));
diff --git a/examples/testformatters.js b/examples/testformatters.js
new file mode 100644
index 0000000..afb4a2d
--- /dev/null
+++ b/examples/testformatters.js
@@ -0,0 +1,270 @@
+'use strict';
+
+var a, group, parser, helptext;
+
+var assert = require('assert');
+var _ = require('underscore');
+_.str = require('underscore.string');
+var print = function () {
+ return console.log.apply(console, arguments);
+ };
+// print = function () {};
+
+var argparse = require('argparse');
+
+print("TEST argparse.ArgumentDefaultsHelpFormatter");
+
+parser = new argparse.ArgumentParser({
+ debug: true,
+ formatterClass: argparse.ArgumentDefaultsHelpFormatter,
+ description: 'description'
+});
+
+parser.addArgument(['--foo'], {
+ help: 'foo help - oh and by the way, %(defaultValue)s'
+});
+
+parser.addArgument(['--bar'], {
+ action: 'storeTrue',
+ help: 'bar help'
+});
+
+parser.addArgument(['spam'], {
+ help: 'spam help'
+});
+
+parser.addArgument(['badger'], {
+ nargs: '?',
+ defaultValue: 'wooden',
+ help: 'badger help'
+});
+
+group = parser.addArgumentGroup({
+ title: 'title',
+ description: 'group description'
+});
+
+group.addArgument(['--baz'], {
+ type: 'int',
+ defaultValue: 42,
+ help: 'baz help'
+});
+
+helptext = parser.formatHelp();
+print(helptext);
+// test selected clips
+assert(helptext.match(/badger help \(default: wooden\)/));
+assert(helptext.match(/foo help - oh and by the way, null/));
+assert(helptext.match(/bar help \(default: false\)/));
+assert(helptext.match(/title:\n {2}group description/)); // test indent
+assert(helptext.match(/baz help \(default: 42\)/im));
+
+/*
+usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
+
+description
+
+positional arguments:
+ spam spam help
+ badger badger help (default: wooden)
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help - oh and by the way, null
+ --bar bar help (default: false)
+
+title:
+ group description
+
+ --baz BAZ baz help (default: 42)
+*/
+
+print("TEST argparse.RawDescriptionHelpFormatter");
+
+parser = new argparse.ArgumentParser({
+ debug: true,
+ prog: 'PROG',
+ formatterClass: argparse.RawDescriptionHelpFormatter,
+ description: 'Keep the formatting\n' +
+ ' exactly as it is written\n' +
+ '\n' +
+ 'here\n'
+});
+
+a = parser.addArgument(['--foo'], {
+ help: ' foo help should not\n' +
+ ' retain this odd formatting'
+});
+
+parser.addArgument(['spam'], {
+ 'help': 'spam help'
+});
+
+group = parser.addArgumentGroup({
+ title: 'title',
+ description: ' This text\n' +
+ ' should be indented\n' +
+ ' exactly like it is here\n'
+});
+
+group.addArgument(['--bar'], {
+ help: 'bar help'
+});
+
+helptext = parser.formatHelp();
+print(helptext);
+// test selected clips
+assert(helptext.match(parser.description));
+assert.equal(helptext.match(a.help), null);
+assert(helptext.match(/foo help should not retain this odd formatting/));
+
+/*
+class TestHelpRawDescription(HelpTestCase):
+ """Test the RawTextHelpFormatter"""
+....
+
+usage: PROG [-h] [--foo FOO] [--bar BAR] spam
+
+Keep the formatting
+ exactly as it is written
+
+here
+
+positional arguments:
+ spam spam help
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help should not retain this odd formatting
+
+title:
+ This text
+ should be indented
+ exactly like it is here
+
+ --bar BAR bar help
+*/
+
+
+print("TEST argparse.RawTextHelpFormatter");
+
+parser = new argparse.ArgumentParser({
+ debug: true,
+ prog: 'PROG',
+ formatterClass: argparse.RawTextHelpFormatter,
+ description: 'Keep the formatting\n' +
+ ' exactly as it is written\n' +
+ '\n' +
+ 'here\n'
+});
+
+parser.addArgument(['--baz'], {
+ help: ' baz help should also\n' +
+ 'appear as given here'
+});
+
+a = parser.addArgument(['--foo'], {
+ help: ' foo help should also\n' +
+ 'appear as given here'
+});
+
+parser.addArgument(['spam'], {
+ 'help': 'spam help'
+});
+
+group = parser.addArgumentGroup({
+ title: 'title',
+ description: ' This text\n' +
+ ' should be indented\n' +
+ ' exactly like it is here\n'
+});
+
+group.addArgument(['--bar'], {
+ help: 'bar help'
+});
+
+helptext = parser.formatHelp();
+print(helptext);
+// test selected clips
+assert(helptext.match(parser.description));
+assert(helptext.match(/( {14})appear as given here/gm));
+
+/*
+class TestHelpRawText(HelpTestCase):
+ """Test the RawTextHelpFormatter"""
+
+usage: PROG [-h] [--foo FOO] [--bar BAR] spam
+
+Keep the formatting
+ exactly as it is written
+
+here
+
+positional arguments:
+ spam spam help
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help should also
+ appear as given here
+
+title:
+ This text
+ should be indented
+ exactly like it is here
+
+ --bar BAR bar help
+*/
+
+
+print("TEST metavar as a tuple");
+
+parser = new argparse.ArgumentParser({
+ prog: 'PROG'
+});
+
+parser.addArgument(['-w'], {
+ help: 'w',
+ nargs: '+',
+ metavar: ['W1', 'W2']
+});
+
+parser.addArgument(['-x'], {
+ help: 'x',
+ nargs: '*',
+ metavar: ['X1', 'X2']
+});
+
+parser.addArgument(['-y'], {
+ help: 'y',
+ nargs: 3,
+ metavar: ['Y1', 'Y2', 'Y3']
+});
+
+parser.addArgument(['-z'], {
+ help: 'z',
+ nargs: '?',
+ metavar: ['Z1']
+});
+
+helptext = parser.formatHelp();
+print(helptext);
+var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';
+ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
+// print(ustring)
+assert(helptext.match(new RegExp(ustring)));
+
+/*
+class TestHelpTupleMetavar(HelpTestCase):
+ """Test specifying metavar as a tuple"""
+
+usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]
+
+optional arguments:
+ -h, --help show this help message and exit
+ -w W1 [W2 ...] w
+ -x [X1 [X2 ...]] x
+ -y Y1 Y2 Y3 y
+ -z [Z1] z
+*/
+
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..3b6eea0
--- /dev/null
+++ b/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/argparse');
diff --git a/lib/action.js b/lib/action.js
new file mode 100644
index 0000000..6f7e9a5
--- /dev/null
+++ b/lib/action.js
@@ -0,0 +1,146 @@
+/**
+ * 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.
+ *
+ * #####Alowed 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 $$ = 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 = options.nargs !== undefined ? options.nargs : null;
+ this.constant = options.constant !== undefined ? options.constant : null;
+ this.defaultValue = options.defaultValue;
+ this.type = options.type !== undefined ? options.type : null;
+ this.choices = options.choices !== undefined ? options.choices : null;
+ this.required = options.required !== undefined ? options.required: false;
+ this.help = options.help !== undefined ? options.help : null;
+ this.metavar = options.metavar !== undefined ? options.metavar : null;
+
+ if (!(this.optionStrings instanceof Array)) {
+ throw new Error('optionStrings should be an array');
+ }
+ if (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 !== $$.SUPPRESS) {
+ return this.metavar;
+ } else if (this.dest !== undefined && this.dest !== $$.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
+};
diff --git a/lib/action/append.js b/lib/action/append.js
new file mode 100644
index 0000000..48c6dbe
--- /dev/null
+++ b/lib/action/append.js
@@ -0,0 +1,55 @@
+/*: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 $$ = 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 !== $$.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 = [].concat(namespace[this.dest] || []); // or _.clone
+ items.push(values);
+ namespace.set(this.dest, items);
+};
+
+
diff --git a/lib/action/append/constant.js b/lib/action/append/constant.js
new file mode 100644
index 0000000..90747ab
--- /dev/null
+++ b/lib/action/append/constant.js
@@ -0,0 +1,47 @@
+/*: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 (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);
+};
diff --git a/lib/action/count.js b/lib/action/count.js
new file mode 100644
index 0000000..d6a5899
--- /dev/null
+++ b/lib/action/count.js
@@ -0,0 +1,40 @@
+/*: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);
+};
diff --git a/lib/action/help.js b/lib/action/help.js
new file mode 100644
index 0000000..7f7b4e2
--- /dev/null
+++ b/lib/action/help.js
@@ -0,0 +1,48 @@
+/*: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 $$ = 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 = $$.SUPPRESS;
+ }
+ options.dest = (options.dest !== null ? options.dest: $$.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();
+};
diff --git a/lib/action/store.js b/lib/action/store.js
new file mode 100644
index 0000000..8ebc974
--- /dev/null
+++ b/lib/action/store.js
@@ -0,0 +1,50 @@
+/*: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 $$ = 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 (this.constant !== undefined && this.nargs !== $$.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);
+};
diff --git a/lib/action/store/constant.js b/lib/action/store/constant.js
new file mode 100644
index 0000000..8410fcf
--- /dev/null
+++ b/lib/action/store/constant.js
@@ -0,0 +1,43 @@
+/*: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 (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);
+};
diff --git a/lib/action/store/false.js b/lib/action/store/false.js
new file mode 100644
index 0000000..66417bf
--- /dev/null
+++ b/lib/action/store/false.js
@@ -0,0 +1,27 @@
+/*: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);
diff --git a/lib/action/store/true.js b/lib/action/store/true.js
new file mode 100644
index 0000000..43ec708
--- /dev/null
+++ b/lib/action/store/true.js
@@ -0,0 +1,26 @@
+/*: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);
diff --git a/lib/action/subparsers.js b/lib/action/subparsers.js
new file mode 100644
index 0000000..adecf65
--- /dev/null
+++ b/lib/action/subparsers.js
@@ -0,0 +1,148 @@
+/** 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 _ = require('underscore');
+
+
+var Action = require('../action');
+
+// Constants
+var $$ = require('../const');
+
+// Errors
+var argumentErrorHelper = require('../argument/error');
+
+
+/*:nodoc:*
+ * new ChoicesPseudoAction(name, help)
+ *
+ * Create pseudo action for correct help text
+ *
+ **/
+var ChoicesPseudoAction = function (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]]
+ *
+ **/
+var ActionSubparsers = module.exports = function ActionSubparsers(options) {
+ options = options || {};
+ options.dest = options.dest || $$.SUPPRESS;
+ options.nargs = $$.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 || _.isString(options.help)) {
+ 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 !== $$.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,
+ _.keys(this._nameParserMap).join(', ')
+ ));
+ }
+
+ // parse all the remaining options into the namespace
+ parser.parseArgs(argStrings, namespace);
+};
+
+
diff --git a/lib/action/version.js b/lib/action/version.js
new file mode 100644
index 0000000..a17877c
--- /dev/null
+++ b/lib/action/version.js
@@ -0,0 +1,50 @@
+/*: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 $$ = 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: $$.SUPPRESS);
+ options.dest = (options.dest || $$.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());
+};
+
+
+
diff --git a/lib/action_container.js b/lib/action_container.js
new file mode 100644
index 0000000..dbe3e3c
--- /dev/null
+++ b/lib/action_container.js
@@ -0,0 +1,481 @@
+/** internal
+ * class ActionContainer
+ *
+ * Action container. Parent for [[ArgumentParser]] and [[ArgumentGroup]]
+ **/
+
+'use strict';
+
+var format = require('util').format;
+var _ = require('underscore');
+
+_.str = require('underscore.string');
+
+// Constants
+var $$ = require('./const');
+
+//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 (3 > arguments.length) {
+ 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) {
+ 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 (action.dest in options) {
+ 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 (Array): 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, ...})
+ **/
+ActionContainer.prototype.addArgument = function (args, options) {
+ args = args;
+ options = options || {};
+
+ if (!_.isArray(args)) {
+ throw new TypeError('addArgument first argument should be an array');
+ }
+ if (!_.isObject(options) || _.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 (_.isUndefined(options.defaultValue)) {
+ var dest = options.dest;
+ if (_.has(this._defaults, dest)) {
+ options.defaultValue = this._defaults[dest];
+ } else if (!_.isUndefined(this.argumentDefault)) {
+ options.defaultValue = this.argumentDefault;
+ }
+ }
+
+ // create the action object, and add it to the parser
+ var ActionClass = this._popActionClass(options);
+ if (! _.isFunction(ActionClass)) {
+ 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 (!_.isFunction(typeFunction)) {
+ 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 (!_.any(self._hasNegativeNumberOptionals)) {
+ 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 (_.isArray(dest)) {
+ dest = _.first(dest);
+ }
+ // 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 !== $$.OPTIONAL && options.nargs !== $$.ZERO_OR_MORE) {
+ options.required = true;
+ }
+ if (options.nargs === $$.ZERO_OR_MORE && 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 = _.str.strip(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" + _.str.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 = _.map(conflOptionals, 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);
+ }
+ });
+};
diff --git a/lib/argparse.js b/lib/argparse.js
new file mode 100644
index 0000000..f2a2c51
--- /dev/null
+++ b/lib/argparse.js
@@ -0,0 +1,14 @@
+'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;
diff --git a/lib/argument/error.js b/lib/argument/error.js
new file mode 100644
index 0000000..c8a02a0
--- /dev/null
+++ b/lib/argument/error.js
@@ -0,0 +1,50 @@
+'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;
+};
diff --git a/lib/argument/exclusive.js b/lib/argument/exclusive.js
new file mode 100644
index 0000000..8287e00
--- /dev/null
+++ b/lib/argument/exclusive.js
@@ -0,0 +1,54 @@
+/** 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);
+};
+
diff --git a/lib/argument/group.js b/lib/argument/group.js
new file mode 100644
index 0000000..58b271f
--- /dev/null
+++ b/lib/argument/group.js
@@ -0,0 +1,75 @@
+/** 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);
+ }
+};
+
diff --git a/lib/argument_parser.js b/lib/argument_parser.js
new file mode 100644
index 0000000..97cf098
--- /dev/null
+++ b/lib/argument_parser.js
@@ -0,0 +1,1165 @@
+/**
+ * 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 _ = require('underscore');
+_.str = require('underscore.string');
+
+// Constants
+var $$ = require('./const');
+
+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: sys.argv[0])
+ * - `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
+ **/
+var ArgumentParser = module.exports = function 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 = (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
+ var FUNCTION_IDENTITY = function (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: $$.SUPPRESS,
+ help: 'Show this help message and exit.'
+ }
+ );
+ }
+ if (this.version !== undefined) {
+ this.addArgument(
+ [defaultPrefix + 'v', defaultPrefix + defaultPrefix + 'version'],
+ {
+ action: 'version',
+ version: this.version,
+ defaultValue: $$.SUPPRESS,
+ help: "Show program's version number and exit."
+ }
+ );
+ }
+
+ // add parent arguments and defaults
+ options.parents.forEach(function (parent) {
+ self._addContainerActions(parent);
+ if (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 = _.str.strip(formatter.formatHelp());
+ }
+
+ // 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 !== $$.SUPPRESS) {
+ if (!_.has(namespace, action.dest)) {
+ if (action.defaultValue !== $$.SUPPRESS) {
+ var defaultValue = action.defaultValue;
+ if (_.isString(action.defaultValue)) {
+ defaultValue = self._getValue(action, defaultValue);
+ }
+ namespace[action.dest] = defaultValue;
+ }
+ }
+ }
+ });
+
+ _.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, $$._UNRECOGNIZED_ARGS_ATTR)) {
+ args = _.union(args, namespace[$$._UNRECOGNIZED_ARGS_ATTR]);
+ delete namespace[$$._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++;
+ }
+ }
+ // otherwise, add the arg to the arg strings
+ // and note the index if it was an option
+ else {
+ 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 !== $$.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;
+
+ while (true) {
+ 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 (_.keys(optionalsMap).indexOf(optionString) >= 0) {
+ action = optionalsMap[optionString];
+ explicitArg = newExplicitArg;
+ }
+ else {
+ var msg = 'ignored explicit argument %r';
+ throw argumentErrorHelper(action, msg);
+ }
+ }
+ // if the action expect exactly one argument, we've
+ // successfully matched the option; exit the loop
+ else if (argCount === 1) {
+ stop = startIndex + 1;
+ args = [explicitArg];
+ actionTuples.push([action, args, optionString]);
+ break;
+ }
+ // error if a double-dash option did not use the
+ // explicit argument
+ else {
+ var message = 'ignored explicit argument %r';
+ throw argumentErrorHelper(action, _.str.sprintf(message, explicitArg));
+ }
+ }
+ // if there is no explicit argument, try to match the
+ // optional's string arguments with the following strings
+ // if successful, exit the loop
+ else {
+
+ 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
+ _.zip(positionals, argCounts).forEach(function (item) {
+ var action = item[0];
+ var argCount = item[1];
+ if (argCount === undefined) {
+ return;
+ }
+ 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;
+ if (!!optionStringIndices) {
+ for (position in optionStringIndices) {
+ 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) {
+ 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(_.rest(argStrings, 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 (_.indexOf(seenActions, 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 = _.any(group._groupActions, function (action) {
+ return _.contains(seenNonDefaultActions, action);
+ });
+
+ // if no actions were used, report the error
+ if (!actionUsed) {
+ var names = [];
+ group._groupActions.forEach(function (action) {
+ if (action.help !== $$.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 _this = this;
+ var fs = require('fs');
+ var newArgStrings = [];
+ argStrings.forEach(function (argString) {
+ if (_this.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) {
+ _this.convertArgLineToArgs(argLine).forEach(function (arg) {
+ argstrs.push(arg);
+ });
+ argstrs = _this._readArgsFromFiles(argstrs);
+ });
+ newArgStrings.push.apply(newArgStrings, argstrs);
+ } catch (error) {
+ return _this.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) {
+ case undefined:
+ case null:
+ message = 'Expected one argument.';
+ break;
+ case $$.OPTIONAL:
+ message = 'Expected at most one argument.';
+ break;
+ case $$.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;
+
+ var getLength = function (string) {
+ return string.length;
+ };
+
+ for (i = actions.length; i > 0; i -= 1) {
+ pattern = '';
+ actionSlice = actions.slice(0, i);
+ for (j in actionSlice) {
+ 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) {
+ var argStringSplit = argString.split('=');
+ optionString = argStringSplit[0];
+ argExplicit = argStringSplit[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 (!_.any(this._hasNegativeNumberOptionals)) {
+ 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) {
+ 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 $$.OPTIONAL:
+ regexpNargs = '(-*A?-*)';
+ break;
+ // allow zero or more arguments
+ case $$.ZERO_OR_MORE:
+ regexpNargs = '(-*[A-]*)';
+ break;
+ // allow one or more arguments
+ case $$.ONE_OR_MORE:
+ regexpNargs = '(-*A[A-]*)';
+ break;
+ // allow any number of options or arguments
+ case $$.REMAINDER:
+ regexpNargs = '([-AO]*)';
+ break;
+ // allow one argument followed by any number of options or arguments
+ case $$.PARSER:
+ regexpNargs = '(-*A[-AO]*)';
+ break;
+ // all others should be integers
+ default:
+ regexpNargs = '(-*' + _.str.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 !== $$.PARSER && action.nargs !== $$.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 === $$.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 === $$.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 === $$.OPTIONAL)) {
+
+ argString = argStrings[0];
+ value = this._getValue(action, argString);
+ this._checkValue(action, value);
+
+ // REMAINDER arguments convert all values, checking none
+ } else if (action.nargs === $$.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 === $$.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 (!_.isFunction(typeFunction)) {
+ 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 (_.isString(action.type)) {
+ 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 ((_.isString(choices) || _.isArray(choices)) &&
+ choices.indexOf(value) !== -1) {
+ return;
+ }
+ // choise for subparsers can by only hash
+ if (_.isObject(choices) && !_.isArray(choices) && choices[value]) {
+ return;
+ }
+
+ if (_.isString(choices)) {
+ choices = choices.split('').join(', ');
+ }
+ else if (_.isArray(choices)) {
+ choices = choices.join(', ');
+ }
+ else {
+ choices = _.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) + $$.EOL;
+
+ if (this.debug === true) {
+ throw new Error(msg);
+ }
+
+ this.printUsage(process.stderr);
+
+ return this.exit(2, msg);
+};
diff --git a/lib/const.js b/lib/const.js
new file mode 100644
index 0000000..de831ba
--- /dev/null
+++ b/lib/const.js
@@ -0,0 +1,18 @@
+//
+// Constants
+//
+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';
diff --git a/lib/help/added_formatters.js b/lib/help/added_formatters.js
new file mode 100644
index 0000000..cd2f369
--- /dev/null
+++ b/lib/help/added_formatters.js
@@ -0,0 +1,88 @@
+'use strict';
+
+var util = require('util');
+var _ = require('underscore');
+_.str = require('underscore.string');
+
+// Constants
+var $$ = require('../const');
+
+var HelpFormatter = require('./formatter.js');
+
+/**
+ * new RawDescriptionHelpFormatter(options)
+ * new ArgumentParser({formatterClass: argparse.RawDescriptionHelpFormatter, ...})
+ *
+ * Help message formatter which adds default values to argument help.
+ *
+ * Only the name of this class is considered a public API. All the methods
+ * provided by the class are considered an implementation detail.
+ **/
+
+var ArgumentDefaultsHelpFormatter = function ArgumentDefaultsHelpFormatter(options) {
+ HelpFormatter.call(this, options);
+};
+
+util.inherits(ArgumentDefaultsHelpFormatter, HelpFormatter);
+
+ArgumentDefaultsHelpFormatter.prototype._getHelpString = function (action) {
+ var help = action.help;
+ if (action.help.indexOf('%(defaultValue)s') === -1) {
+ if (action.defaultValue !== $$.SUPPRESS) {
+ var defaulting_nargs = [$$.OPTIONAL, $$.ZERO_OR_MORE];
+ if (action.isOptional() || (defaulting_nargs.indexOf(action.nargs) >= 0)) {
+ help += ' (default: %(defaultValue)s)';
+ }
+ }
+ }
+ return help;
+};
+
+module.exports.ArgumentDefaultsHelpFormatter = ArgumentDefaultsHelpFormatter;
+
+/**
+ * new RawDescriptionHelpFormatter(options)
+ * new ArgumentParser({formatterClass: argparse.RawDescriptionHelpFormatter, ...})
+ *
+ * Help message formatter which retains any formatting in descriptions.
+ *
+ * Only the name of this class is considered a public API. All the methods
+ * provided by the class are considered an implementation detail.
+ **/
+
+var RawDescriptionHelpFormatter = function RawDescriptionHelpFormatter(options) {
+ HelpFormatter.call(this, options);
+};
+
+util.inherits(RawDescriptionHelpFormatter, HelpFormatter);
+
+RawDescriptionHelpFormatter.prototype._fillText = function (text, width, indent) {
+ var lines = text.split('\n');
+ lines = lines.map(function (line) {
+ return _.str.rtrim(indent + line);
+ });
+ return lines.join('\n');
+};
+module.exports.RawDescriptionHelpFormatter = RawDescriptionHelpFormatter;
+
+/**
+ * new RawTextHelpFormatter(options)
+ * new ArgumentParser({formatterClass: argparse.RawTextHelpFormatter, ...})
+ *
+ * Help message formatter which retains formatting of all help text.
+ *
+ * Only the name of this class is considered a public API. All the methods
+ * provided by the class are considered an implementation detail.
+ **/
+
+var RawTextHelpFormatter = function RawTextHelpFormatter(options) {
+ RawDescriptionHelpFormatter.call(this, options);
+};
+
+util.inherits(RawTextHelpFormatter, RawDescriptionHelpFormatter);
+
+RawTextHelpFormatter.prototype._splitLines = function (text) {
+ return text.split('\n');
+};
+
+module.exports.RawTextHelpFormatter = RawTextHelpFormatter;
diff --git a/lib/help/formatter.js b/lib/help/formatter.js
new file mode 100644
index 0000000..541d918
--- /dev/null
+++ b/lib/help/formatter.js
@@ -0,0 +1,803 @@
+/**
+ * class HelpFormatter
+ *
+ * Formatter for generating usage messages and argument help strings. Only the
+ * name of this class is considered a public API. All the methods provided by
+ * the class are considered an implementation detail.
+ *
+ * Do not call in your code, use this class only for inherits your own forvatter
+ *
+ * ToDo add [additonal formatters][1]
+ *
+ * [1]:http://docs.python.org/dev/library/argparse.html#formatter-class
+ **/
+'use strict';
+
+var _ = require('underscore');
+_.str = require('underscore.string');
+
+// Constants
+var $$ = require('../const');
+
+
+/*:nodoc:* internal
+ * new Support(parent, heding)
+ * - parent (object): parent section
+ * - heading (string): header string
+ *
+ **/
+function Section(parent, heading) {
+ this._parent = parent;
+ this._heading = heading;
+ this._items = [];
+}
+
+/*:nodoc:* internal
+ * Section#addItem(callback) -> Void
+ * - callback (array): tuple with function and args
+ *
+ * Add function for single element
+ **/
+Section.prototype.addItem = function (callback) {
+ this._items.push(callback);
+};
+
+/*:nodoc:* internal
+ * Section#formatHelp(formatter) -> string
+ * - formatter (HelpFormatter): current formatter
+ *
+ * Form help section string
+ *
+ **/
+Section.prototype.formatHelp = function (formatter) {
+ var itemHelp, heading;
+
+ // format the indented section
+ if (!!this._parent) {
+ formatter._indent();
+ }
+
+ itemHelp = this._items.map(function (item) {
+ var obj, func, args;
+
+ obj = formatter;
+ func = item[0];
+ args = item[1];
+ return func.apply(obj, args);
+ });
+ itemHelp = formatter._joinParts(itemHelp);
+
+ if (!!this._parent) {
+ formatter._dedent();
+ }
+
+ // return nothing if the section was empty
+ if (!itemHelp) {
+ return '';
+ }
+
+ // add the heading if the section was non-empty
+ heading = '';
+ if (!!this._heading && this._heading !== $$.SUPPRESS) {
+ var currentIndent = formatter.currentIndent;
+ heading = _.str.repeat(' ', currentIndent) + this._heading + ':' + $$.EOL;
+ }
+
+ // join the section-initialize newline, the heading and the help
+ return formatter._joinParts([$$.EOL, heading, itemHelp, $$.EOL]);
+};
+
+/**
+ * new HelpFormatter(options)
+ *
+ * #### Options:
+ * - `prog`: program name
+ * - `indentIncriment`: indent step, default value 2
+ * - `maxHelpPosition`: max help position, default value = 24
+ * - `width`: line width
+ *
+ **/
+var HelpFormatter = module.exports = function HelpFormatter(options) {
+ options = options || {};
+
+ this._prog = options.prog;
+
+ this._maxHelpPosition = options.maxHelpPosition || 24;
+ this._width = (options.width || ((process.env.COLUMNS || 80) - 2));
+
+ this._currentIndent = 0;
+ this._indentIncriment = options.indentIncriment || 2;
+ this._level = 0;
+ this._actionMaxLength = 0;
+
+ this._rootSection = new Section(null);
+ this._currentSection = this._rootSection;
+
+ this._whitespaceMatcher = new RegExp('\\s+', 'g');
+ this._longBreakMatcher = new RegExp($$.EOL + $$.EOL + $$.EOL + '+', 'g');
+};
+
+HelpFormatter.prototype._indent = function () {
+ this._currentIndent += this._indentIncriment;
+ this._level += 1;
+};
+
+HelpFormatter.prototype._dedent = function () {
+ this._currentIndent -= this._indentIncriment;
+ this._level -= 1;
+ if (this._currentIndent < 0) {
+ throw new Error('Indent decreased below 0.');
+ }
+};
+
+HelpFormatter.prototype._addItem = function (func, args) {
+ this._currentSection.addItem([func, args]);
+};
+
+//
+// Message building methods
+//
+
+/**
+ * HelpFormatter#startSection(heading) -> Void
+ * - heading (string): header string
+ *
+ * Start new help section
+ *
+ * See alse [code example][1]
+ *
+ * ##### Example
+ *
+ * formatter.startSection(actionGroup.title);
+ * formatter.addText(actionGroup.description);
+ * formatter.addArguments(actionGroup._groupActions);
+ * formatter.endSection();
+ *
+ **/
+HelpFormatter.prototype.startSection = function (heading) {
+ this._indent();
+ var section = new Section(this._currentSection, heading);
+ var func = section.formatHelp.bind(section);
+ this._addItem(func, [this]);
+ this._currentSection = section;
+};
+
+/**
+ * HelpFormatter#endSection -> Void
+ *
+ * End help section
+ *
+ * ##### Example
+ *
+ * formatter.startSection(actionGroup.title);
+ * formatter.addText(actionGroup.description);
+ * formatter.addArguments(actionGroup._groupActions);
+ * formatter.endSection();
+ **/
+HelpFormatter.prototype.endSection = function () {
+ this._currentSection = this._currentSection._parent;
+ this._dedent();
+};
+
+/**
+ * HelpFormatter#addText(text) -> Void
+ * - text (string): plain text
+ *
+ * Add plain text into current section
+ *
+ * ##### Example
+ *
+ * formatter.startSection(actionGroup.title);
+ * formatter.addText(actionGroup.description);
+ * formatter.addArguments(actionGroup._groupActions);
+ * formatter.endSection();
+ *
+ **/
+HelpFormatter.prototype.addText = function (text) {
+ if (!!text && text !== $$.SUPPRESS) {
+ this._addItem(this._formatText, [text]);
+ }
+};
+
+/**
+ * HelpFormatter#addUsage(usage, actions, groups, prefix) -> Void
+ * - usage (string): usage text
+ * - actions (array): actions list
+ * - groups (array): groups list
+ * - prefix (string): usage prefix
+ *
+ * Add usage data into current section
+ *
+ * ##### Example
+ *
+ * formatter.addUsage(this.usage, this._actions, []);
+ * return formatter.formatHelp();
+ *
+ **/
+HelpFormatter.prototype.addUsage = function (usage, actions, groups, prefix) {
+ if (usage !== $$.SUPPRESS) {
+ this._addItem(this._formatUsage, [usage, actions, groups, prefix]);
+ }
+};
+
+/**
+ * HelpFormatter#addArgument(action) -> Void
+ * - action (object): action
+ *
+ * Add argument into current section
+ *
+ * Single variant of [[HelpFormatter#addArguments]]
+ **/
+HelpFormatter.prototype.addArgument = function (action) {
+ if (action.help !== $$.SUPPRESS) {
+ var self = this;
+
+ // find all invocations
+ var invocations = [this._formatActionInvocation(action)];
+ var invocationLength = invocations[0].length;
+
+ var actionLength;
+
+ if (!!action._getSubactions) {
+ this._indent();
+ action._getSubactions().forEach(function (subaction) {
+
+ var invocationNew = self._formatActionInvocation(subaction);
+ invocations.push(invocationNew);
+ invocationLength = Math.max(invocationLength, invocationNew.length);
+
+ });
+ this._dedent();
+ }
+
+ // update the maximum item length
+ actionLength = invocationLength + this._currentIndent;
+ this._actionMaxLength = Math.max(this._actionMaxLength, actionLength);
+
+ // add the item to the list
+ this._addItem(this._formatAction, [action]);
+ }
+};
+
+/**
+ * HelpFormatter#addArguments(actions) -> Void
+ * - actions (array): actions list
+ *
+ * Mass add arguments into current section
+ *
+ * ##### Example
+ *
+ * formatter.startSection(actionGroup.title);
+ * formatter.addText(actionGroup.description);
+ * formatter.addArguments(actionGroup._groupActions);
+ * formatter.endSection();
+ *
+ **/
+HelpFormatter.prototype.addArguments = function (actions) {
+ var self = this;
+ actions.forEach(function (action) {
+ self.addArgument(action);
+ });
+};
+
+//
+// Help-formatting methods
+//
+
+/**
+ * HelpFormatter#formatHelp -> string
+ *
+ * Format help
+ *
+ * ##### Example
+ *
+ * formatter.addText(this.epilog);
+ * return formatter.formatHelp();
+ *
+ **/
+HelpFormatter.prototype.formatHelp = function () {
+ var help = this._rootSection.formatHelp(this);
+ if (help) {
+ help = help.replace(this._longBreakMatcher, $$.EOL + $$.EOL);
+ help = _.str.strip(help, $$.EOL) + $$.EOL;
+ }
+ return help;
+};
+
+HelpFormatter.prototype._joinParts = function (partStrings) {
+ return partStrings.filter(function (part) {
+ return (!!part && part !== $$.SUPPRESS);
+ }).join('');
+};
+
+HelpFormatter.prototype._formatUsage = function (usage, actions, groups, prefix) {
+ if (!prefix && !_.isString(prefix)) {
+ prefix = 'usage: ';
+ }
+
+ actions = actions || [];
+ groups = groups || [];
+
+
+ // if usage is specified, use that
+ if (usage) {
+ usage = _.str.sprintf(usage, {prog: this._prog});
+
+ // if no optionals or positionals are available, usage is just prog
+ } else if (!usage && actions.length === 0) {
+ usage = this._prog;
+
+ // if optionals and positionals are available, calculate usage
+ } else if (!usage) {
+ var prog = this._prog;
+ var optionals = [];
+ var positionals = [];
+ var actionUsage;
+ var textWidth;
+
+ // split optionals from positionals
+ actions.forEach(function (action) {
+ if (action.isOptional()) {
+ optionals.push(action);
+ } else {
+ positionals.push(action);
+ }
+ });
+
+ // build full usage string
+ actionUsage = this._formatActionsUsage([].concat(optionals, positionals), groups);
+ usage = [prog, actionUsage].join(' ');
+
+ // wrap the usage parts if it's too long
+ textWidth = this._width - this._currentIndent;
+ if ((prefix.length + usage.length) > textWidth) {
+
+ // break usage into wrappable parts
+ var regexpPart = new RegExp('\\(.*?\\)+|\\[.*?\\]+|\\S+', 'g');
+ var optionalUsage = this._formatActionsUsage(optionals, groups);
+ var positionalUsage = this._formatActionsUsage(positionals, groups);
+
+
+ var optionalParts = optionalUsage.match(regexpPart);
+ var positionalParts = positionalUsage.match(regexpPart) || [];
+
+ if (optionalParts.join(' ') !== optionalUsage) {
+ throw new Error('assert "optionalParts.join(\' \') === optionalUsage"');
+ }
+ if (positionalParts.join(' ') !== positionalUsage) {
+ throw new Error('assert "positionalParts.join(\' \') === positionalUsage"');
+ }
+
+ // helper for wrapping lines
+ var _getLines = function (parts, indent, prefix) {
+ var lines = [];
+ var line = [];
+
+ var lineLength = !!prefix ? prefix.length - 1: indent.length - 1;
+
+ parts.forEach(function (part) {
+ if (lineLength + 1 + part.length > textWidth) {
+ lines.push(indent + line.join(' '));
+ line = [];
+ lineLength = indent.length - 1;
+ }
+ line.push(part);
+ lineLength += part.length + 1;
+ });
+
+ if (line) {
+ lines.push(indent + line.join(' '));
+ }
+ if (prefix) {
+ lines[0] = lines[0].substr(indent.length);
+ }
+ return lines;
+ };
+
+ var lines, indent, parts;
+ // if prog is short, follow it with optionals or positionals
+ if (prefix.length + prog.length <= 0.75 * textWidth) {
+ indent = _.str.repeat(' ', (prefix.length + prog.length + 1));
+ if (optionalParts) {
+ lines = [].concat(
+ _getLines([prog].concat(optionalParts), indent, prefix),
+ _getLines(positionalParts, indent)
+ );
+ } else if (positionalParts) {
+ lines = _getLines([prog].concat(positionalParts), indent, prefix);
+ } else {
+ lines = [prog];
+ }
+
+ // if prog is long, put it on its own line
+ } else {
+ indent = _.str.repeat(' ', prefix.length);
+ parts = optionalParts + positionalParts;
+ lines = _getLines(parts, indent);
+ if (lines.length > 1) {
+ lines = [].concat(
+ _getLines(optionalParts, indent),
+ _getLines(positionalParts, indent)
+ );
+ }
+ lines = [prog] + lines;
+ }
+ // join lines into usage
+ usage = lines.join($$.EOL);
+ }
+ }
+
+ // prefix with 'usage:'
+ return prefix + usage + $$.EOL + $$.EOL;
+};
+
+HelpFormatter.prototype._formatActionsUsage = function (actions, groups) {
+ // find group indices and identify actions in groups
+ var groupActions = [];
+ var inserts = [];
+ var self = this;
+
+ groups.forEach(function (group) {
+ var end;
+ var i;
+
+ var start = actions.indexOf(group._groupActions[0]);
+ if (start >= 0) {
+ end = start + group._groupActions.length;
+
+ //if (actions.slice(start, end) === group._groupActions) {
+ if (_.isEqual(actions.slice(start, end), group._groupActions)) {
+ group._groupActions.forEach(function (action) {
+ groupActions.push(action);
+ });
+
+ if (!group.required) {
+ if (!!inserts[start]) {
+ inserts[start] += ' [';
+ }
+ else {
+ inserts[start] = '[';
+ }
+ inserts[end] = ']';
+ } else {
+ if (!!inserts[start]) {
+ inserts[start] += ' (';
+ }
+ else {
+ inserts[start] = '(';
+ }
+ inserts[end] = ')';
+ }
+ for (i = start + 1; i < end; i += 1) {
+ inserts[i] = '|';
+ }
+ }
+ }
+ });
+
+ // collect all actions format strings
+ var parts = [];
+
+ actions.forEach(function (action, actionIndex) {
+ var part;
+ var optionString;
+ var argsDefault;
+ var argsString;
+
+ // suppressed arguments are marked with None
+ // remove | separators for suppressed arguments
+ if (action.help === $$.SUPPRESS) {
+ parts.push(null);
+ if (inserts[actionIndex] === '|') {
+ inserts.splice(actionIndex, actionIndex);
+ } else if (inserts[actionIndex + 1] === '|') {
+ inserts.splice(actionIndex + 1, actionIndex + 1);
+ }
+
+ // produce all arg strings
+ } else if (!action.isOptional()) {
+ part = self._formatArgs(action, action.dest);
+
+ // if it's in a group, strip the outer []
+ if (groupActions.indexOf(action) >= 0) {
+ if (part[0] === '[' && part[part.length - 1] === ']') {
+ part = part.slice(1, -1);
+ }
+ }
+ // add the action string to the list
+ parts.push(part);
+
+ // produce the first way to invoke the option in brackets
+ } else {
+ optionString = action.optionStrings[0];
+
+ // if the Optional doesn't take a value, format is: -s or --long
+ if (action.nargs === 0) {
+ part = '' + optionString;
+
+ // if the Optional takes a value, format is: -s ARGS or --long ARGS
+ } else {
+ argsDefault = action.dest.toUpperCase();
+ argsString = self._formatArgs(action, argsDefault);
+ part = optionString + ' ' + argsString;
+ }
+ // make it look optional if it's not required or in a group
+ if (!action.required && groupActions.indexOf(action) < 0) {
+ part = '[' + part + ']';
+ }
+ // add the action string to the list
+ parts.push(part);
+ }
+ });
+
+ // insert things at the necessary indices
+ for (var i = inserts.length - 1; i >= 0; --i) {
+ if (inserts[i] !== null) {
+ parts.splice(i, 0, inserts[i]);
+ }
+ }
+
+ // join all the action items with spaces
+ var text = parts.filter(function (part) {
+ return !!part;
+ }).join(' ');
+
+ // clean up separators for mutually exclusive groups
+ text = text.replace(/([\[(]) /g, '$1'); // remove spaces
+ text = text.replace(/ ([\])])/g, '$1');
+ text = text.replace(/\[ *\]/g, ''); // remove empty groups
+ text = text.replace(/\( *\)/g, '');
+ text = text.replace(/\(([^|]*)\)/g, '$1'); // remove () from single action groups
+
+ text = _.str.strip(text);
+
+ // return the text
+ return text;
+};
+
+HelpFormatter.prototype._formatText = function (text) {
+ text = _.str.sprintf(text, {prog: this._prog});
+ var textWidth = this._width - this._currentIndent;
+ var indentIncriment = _.str.repeat(' ', this._currentIndent);
+ return this._fillText(text, textWidth, indentIncriment) + $$.EOL + $$.EOL;
+};
+
+HelpFormatter.prototype._formatAction = function (action) {
+ var self = this;
+
+ var helpText;
+ var helpLines;
+ var parts;
+ var indentFirst;
+
+ // determine the required width and the entry label
+ var helpPosition = Math.min(this._actionMaxLength + 2, this._maxHelpPosition);
+ var helpWidth = this._width - helpPosition;
+ var actionWidth = helpPosition - this._currentIndent - 2;
+ var actionHeader = this._formatActionInvocation(action);
+
+ // no help; start on same line and add a final newline
+ if (!action.help) {
+ actionHeader = _.str.repeat(' ', this._currentIndent) + actionHeader + $$.EOL;
+
+ // short action name; start on the same line and pad two spaces
+ } else if (actionHeader.length <= actionWidth) {
+ actionHeader = _.str.repeat(' ', this._currentIndent) +
+ actionHeader +
+ ' ' +
+ _.str.repeat(' ', actionWidth - actionHeader.length);
+ indentFirst = 0;
+
+ // long action name; start on the next line
+ } else {
+ actionHeader = _.str.repeat(' ', this._currentIndent) + actionHeader + $$.EOL;
+ indentFirst = helpPosition;
+ }
+
+ // collect the pieces of the action help
+ parts = [actionHeader];
+
+ // if there was help for the action, add lines of help text
+ if (!!action.help) {
+ helpText = this._expandHelp(action);
+ helpLines = this._splitLines(helpText, helpWidth);
+ parts.push(_.str.repeat(' ', indentFirst) + helpLines[0] + $$.EOL);
+ helpLines.slice(1).forEach(function (line) {
+ parts.push(_.str.repeat(' ', helpPosition) + line + $$.EOL);
+ });
+
+ // or add a newline if the description doesn't end with one
+ } else if (actionHeader.charAt(actionHeader.length - 1) !== $$.EOL) {
+ parts.push($$.EOL);
+ }
+ // if there are any sub-actions, add their help as well
+ if (!!action._getSubactions) {
+ this._indent();
+ action._getSubactions().forEach(function (subaction) {
+ parts.push(self._formatAction(subaction));
+ });
+ this._dedent();
+ }
+ // return a single string
+ return this._joinParts(parts);
+};
+
+HelpFormatter.prototype._formatActionInvocation = function (action) {
+ if (!action.isOptional()) {
+ var format_func = this._metavarFormatter(action, action.dest);
+ var metavars = format_func(1);
+ return metavars[0];
+ } else {
+ var parts = [];
+ var argsDefault;
+ var argsString;
+
+ // if the Optional doesn't take a value, format is: -s, --long
+ if (action.nargs === 0) {
+ parts = parts.concat(action.optionStrings);
+
+ // if the Optional takes a value, format is: -s ARGS, --long ARGS
+ } else {
+ argsDefault = action.dest.toUpperCase();
+ argsString = this._formatArgs(action, argsDefault);
+ action.optionStrings.forEach(function (optionString) {
+ parts.push(optionString + ' ' + argsString);
+ });
+ }
+ return parts.join(', ');
+ }
+};
+
+HelpFormatter.prototype._metavarFormatter = function (action, metavarDefault) {
+ var result;
+
+ if (!!action.metavar || action.metavar === '') {
+ result = action.metavar;
+ } else if (!!action.choices) {
+ var choices = action.choices;
+
+ if (_.isString(choices)) {
+ choices = choices.split('').join(', ');
+ } else if (_.isArray(choices)) {
+ choices = choices.join(',');
+ }
+ else
+ {
+ choices = _.keys(choices).join(',');
+ }
+ result = '{' + choices + '}';
+ } else {
+ result = metavarDefault;
+ }
+
+ return function (size) {
+ if (Array.isArray(result)) {
+ return result;
+ } else {
+ var metavars = [];
+ for (var i = 0; i < size; i += 1) {
+ metavars.push(result);
+ }
+ return metavars;
+ }
+ };
+};
+
+HelpFormatter.prototype._formatArgs = function (action, metavarDefault) {
+ var result;
+ var metavars;
+
+ var buildMetavar = this._metavarFormatter(action, metavarDefault);
+
+ switch (action.nargs) {
+ case undefined:
+ case null:
+ metavars = buildMetavar(1);
+ result = '' + metavars[0];
+ break;
+ case $$.OPTIONAL:
+ metavars = buildMetavar(1);
+ result = '[' + metavars[0] + ']';
+ break;
+ case $$.ZERO_OR_MORE:
+ metavars = buildMetavar(2);
+ result = '[' + metavars[0] + ' [' + metavars[1] + ' ...]]';
+ break;
+ case $$.ONE_OR_MORE:
+ metavars = buildMetavar(2);
+ result = '' + metavars[0] + ' [' + metavars[1] + ' ...]';
+ break;
+ case $$.REMAINDER:
+ result = '...';
+ break;
+ case $$.PARSER:
+ metavars = buildMetavar(1);
+ result = metavars[0] + ' ...';
+ break;
+ default:
+ metavars = buildMetavar(action.nargs);
+ result = metavars.join(' ');
+ }
+ return result;
+};
+
+HelpFormatter.prototype._expandHelp = function (action) {
+ var actionProperty;
+ var actionValue;
+
+ var params = {prog: this._prog};
+
+ for (actionProperty in action) {
+ if (action.hasOwnProperty(actionProperty)) {
+ actionValue = action[actionProperty];
+
+ if (actionValue !== $$.SUPPRESS) {
+ params[actionProperty] = actionValue;
+ }
+ }
+ }
+
+ if (!!params.choices) {
+ if (_.isString(params.choices)) {
+ params.choices = params.choices.split('').join(', ');
+ }
+ else if (_.isArray(params.choices)) {
+ params.choices = params.choices.join(', ');
+ }
+ else {
+ params.choices = _.keys(params.choices).join(', ');
+ }
+ }
+
+ return _.str.sprintf(this._getHelpString(action), params);
+};
+
+HelpFormatter.prototype._splitLines = function (text, width) {
+ var lines = [];
+ var delimiters = [" ", ".", ",", "!", "?"];
+ var re = new RegExp('[' + delimiters.join('') + '][^' + delimiters.join('') + ']*$');
+
+ text = text.replace(/[\n\|\t]/g, ' ');
+
+ text = _.str.strip(text);
+ text = text.replace(this._whitespaceMatcher, ' ');
+
+ // Wraps the single paragraph in text (a string) so every line
+ // is at most width characters long.
+ text.split($$.EOL).forEach(function (line) {
+ if (width >= line.length) {
+ lines.push(line);
+ return;
+ }
+
+ var wrapStart = 0;
+ var wrapEnd = width;
+ var delimiterIndex = 0;
+ while (wrapEnd <= line.length) {
+ if (wrapEnd !== line.length && delimiters.indexOf(line[wrapEnd] < -1)) {
+ delimiterIndex = (re.exec(line.substring(wrapStart, wrapEnd)) || {}).index;
+ wrapEnd = wrapStart + delimiterIndex + 1;
+ }
+ lines.push(line.substring(wrapStart, wrapEnd));
+ wrapStart = wrapEnd;
+ wrapEnd += width;
+ }
+ if (wrapStart < line.length) {
+ lines.push(line.substring(wrapStart, wrapEnd));
+ }
+ });
+
+ return lines;
+};
+
+HelpFormatter.prototype._fillText = function (text, width, indent) {
+ var lines = this._splitLines(text, width);
+ lines = lines.map(function (line) {
+ return indent + line;
+ });
+ return lines.join($$.EOL);
+};
+
+HelpFormatter.prototype._getHelpString = function (action) {
+ return action.help;
+};
diff --git a/lib/namespace.js b/lib/namespace.js
new file mode 100644
index 0000000..3546f2d
--- /dev/null
+++ b/lib/namespace.js
@@ -0,0 +1,77 @@
+/**
+ * class Namespace
+ *
+ * Simple object for storing attributes. Implements equality by attribute names
+ * and values, and provides a simple string representation.
+ *
+ * See also [original guide][1]
+ *
+ * [1]:http://docs.python.org/dev/library/argparse.html#the-namespace-object
+ **/
+'use strict';
+
+var _ = require('underscore');
+
+/**
+ * new Namespace(options)
+ * - options(object): predefined propertis for result object
+ *
+ **/
+var Namespace = module.exports = function Namespace(options) {
+ _.extend(this, options);
+};
+
+/**
+ * Namespace#isset(key) -> Boolean
+ * - key (string|number): property name
+ *
+ * Tells whenever `namespace` contains given `key` or not.
+ **/
+Namespace.prototype.isset = function (key) {
+ return _.has(this, key);
+};
+
+/**
+ * Namespace#set(key, value) -> self
+ * -key (string|number|object): propery name
+ * -value (mixed): new property value
+ *
+ * Set the property named key with value.
+ * If key object then set all key properties to namespace object
+ **/
+Namespace.prototype.set = function (key, value) {
+ if (typeof (key) === 'object') {
+ _.extend(this, key);
+ } else {
+ this[key] = value;
+ }
+ return this;
+};
+
+/**
+ * Namespace#get(key, defaultValue) -> mixed
+ * - key (string|number): property name
+ * - defaultValue (mixed): default value
+ *
+ * Return the property key or defaulValue if not set
+ **/
+Namespace.prototype.get = function (key, defaultValue) {
+ return !this[key] ? defaultValue: this[key];
+};
+
+/**
+ * Namespace#unset(key, defaultValue) -> mixed
+ * - key (string|number): property name
+ * - defaultValue (mixed): default value
+ *
+ * Return data[key](and delete it) or defaultValue
+ **/
+Namespace.prototype.unset = function (key, defaultValue) {
+ var value = this[key];
+ if (value !== null) {
+ delete this[key];
+ return value;
+ } else {
+ return defaultValue;
+ }
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..8e03546
--- /dev/null
+++ b/package.json
@@ -0,0 +1,26 @@
+{
+ "name" : "argparse",
+ "description" : "Very powerful CLI arguments parser. Native port of argparse - python's options parsing library",
+ "version" : "0.1.15",
+ "keywords" : ["cli", "parser", "argparse", "option", "args"],
+ "homepage" : "https://github.com/nodeca/argparse",
+
+ "contributors" : ["Eugene Shkuropat", "Paul Jacobson"],
+
+ "bugs" : { "url": "https://github.com/nodeca/argparse/issues" },
+ "license" : { "type": "MIT", "url": "https://github.com/nodeca/argparse/blob/master/LICENSE" },
+ "repository" : { "type": "git", "url": "git://github.com/nodeca/argparse.git" },
+
+ "main" : "./index.js",
+
+ "scripts" : {
+ "test": "make test"
+ },
+
+ "dependencies" : {
+ "underscore" : "~1.4.3",
+ "underscore.string" : "~2.3.1"
+ },
+ "devDependencies" : { "mocha": "*" },
+ "engines" : { "node": ">= 0.6.0" }
+}
diff --git a/test/base.js b/test/base.js
new file mode 100644
index 0000000..4493c0c
--- /dev/null
+++ b/test/base.js
@@ -0,0 +1,247 @@
+/*global describe, it*/
+
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('base', function () {
+ var parser;
+ var args;
+
+ it("should parse argument in short form", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+
+ args = parser.parseArgs('-f 1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('-f=1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('-f1'.split(' '));
+ assert.equal(args.foo, 1);
+ });
+
+ it("should parse argument in long form", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+
+ args = parser.parseArgs('--foo 1'.split(' '));
+ assert.equal(args.foo, 1);
+ args = parser.parseArgs('--foo=1'.split(' '));
+ assert.equal(args.foo, 1);
+ });
+
+ it("should parse multiple arguments", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['--bar']);
+
+ args = parser.parseArgs('--foo 5 --bar 6'.split(' '));
+ assert.equal(args.foo, 5);
+ assert.equal(args.bar, 6);
+ });
+
+ it("should check argument type", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['--bar' ], { type: 'int' });
+
+ assert.throws(function () {
+ parser.parseArgs('--bar bar'.split(' '));
+ });
+ assert.doesNotThrow(function () {
+ parser.parseArgs('--bar 1'.split(' '));
+ });
+ });
+
+ it("should not drop down with empty args (without positional arguments)", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+
+ assert.doesNotThrow(function () { parser.parseArgs([]); });
+ });
+
+ it("should drop down with empty args (positional arguments)", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'baz']);
+
+ assert.throws(
+ function () {parser.parseArgs([]); },
+ /too few arguments/
+ );
+ });
+
+ it("should support pseudo-argument", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'bar' ], { nargs: '+' });
+
+ args = parser.parseArgs([ '-f', 'foo', '--', '-f', 'bar' ]);
+ assert.equal(args.foo, 'foo');
+ assert.equal(args.bar.length, 2);
+ });
+
+ it("should support #setDefaults", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.setDefaults({bar: 1});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 1);
+ });
+
+ it("should throw TypeError with conflicting options", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+
+ assert.throws(
+ function () { parser.addArgument(['-f']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['--foo']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['-f', '--flame']); },
+ /Conflicting option string/
+ );
+ assert.throws(
+ function () { parser.addArgument(['-m', '--foo']); },
+ /Conflicting option string/
+ );
+ });
+
+ it("should parse negative arguments", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument([ 'bar' ], { type: 'int' });
+
+ args = parser.parseArgs(['-1']);
+ assert.equal(args.bar, -1);
+ });
+
+ it("No negative number options; neg number is positional argument", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-x'], {dest: 'x'});
+ parser.addArgument(['foo'], {nargs: '?'});
+
+ // no negative number options, so -1 is a positional argument
+ args = parser.parseArgs(['-x', '-1']);
+ // Namespace(foo=None, x='-1')
+ assert.equal(args.x, '-1');
+ // no negative number options, so -1 and -5 are positional arguments
+ args = parser.parseArgs(['-x', '-1', '-5']);
+ // Namespace(foo='-5', x='-1') order not determined
+ assert.equal(args.x, '-1');
+ assert.equal(args.foo, '-5');
+ });
+
+ it("negative number options present, so any neg number is an option", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-1'], {dest: 'one'});
+ parser.addArgument(['foo'], {nargs: '?'});
+
+ // negative number options present, so -1 is an option
+ args = parser.parseArgs(['-1', 'X']);
+ // Namespace(foo=None, one='X')
+ assert.equal(args.one, 'X');
+ // negative number options present, so -2 is an option
+ assert.throws(
+ function () {
+ parser.parseArgs(['-2']);
+ },
+ /Unrecognized arguments: -2/
+ );
+ // negative number options present, so both -1s are options
+ assert.throws(
+ function () {
+ parser.parseArgs(['-1', '-1']);
+ },
+ /argument "-1": Expected one argument/
+ );
+ args = parser.parseArgs(['--', '-f']);
+ // Namespace(foo='-f', one=None)
+ assert.equal(args.foo, '-f');
+ });
+
+
+ it("should infer option destination from long and short options", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']); // from long option
+ parser.addArgument(['-g']); // from short option
+ parser.addArgument(['-x'], { dest: 'xxx' });// from dest keyword
+
+ args = parser.parseArgs(['-f', '1']);
+ assert.deepEqual(args, { foo: '1', g: null, xxx: null});
+ args = parser.parseArgs(['-g', '2']);
+ assert.deepEqual(args, { foo: null, g: '2', xxx: null});
+ args = parser.parseArgs(['-f', 1, '-g', 2, '-x', 3]);
+ assert.deepEqual(args, { foo: 1, g: 2, xxx: 3});
+ });
+
+ it("should accept 0 defaultValue", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['bar'], { nargs: '?', defaultValue: 0});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 0);
+ // could also test for '', and false
+ });
+
+
+ it("getDefault() should get defaults", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['-g', '--goo'], {defaultValue: 42});
+
+ assert.equal(parser.getDefault('goo'), 42);
+ assert.equal(parser.getDefault('help'), require('../lib/const').SUPPRESS);
+ });
+
+ it("should handle mixed positional and optional args", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['x']);
+ parser.addArgument(['y']);
+
+ args = parser.parseArgs(['X', 'Y']);
+ assert.deepEqual(args, {"foo": null, "x": "X", "y": "Y"});
+ args = parser.parseArgs(['-f', 'A', 'X', 'Y']);
+ assert.deepEqual(args, {"foo": "A", "x": "X", "y": "Y"});
+ args = parser.parseArgs(['X', '-f', 'A', 'Y']);
+ assert.deepEqual(args, {"foo": "A", "x": "X", "y": "Y"});
+ // was giving: Error: _mocha: error: Unrecognized arguments: X.
+ });
+
+ it('test empty and space containing arguments', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'x' ], { nargs: '?' });
+ parser.addArgument([ '-y', '--yyy' ], { dest: 'y' });
+
+ args = parser.parseArgs([ '' ]);
+ assert.deepEqual(args, { y: null, x: '' });
+ args = parser.parseArgs([ 'a badger' ]);
+ assert.deepEqual(args, { y: null, x: 'a badger' });
+ args = parser.parseArgs([ '-a badger' ]);
+ assert.deepEqual(args, { y: null, x: '-a badger' });
+ args = parser.parseArgs([ '-y', '' ]);
+ assert.deepEqual(args, { y: '', x: null });
+ args = parser.parseArgs([ '-y', 'a badger' ]);
+ assert.deepEqual(args, { y: 'a badger', x: null });
+ args = parser.parseArgs([ '-y', '-a badger' ]);
+ assert.deepEqual(args, { y: '-a badger', x: null });
+ args = parser.parseArgs([ '--yyy=a badger' ]);
+ assert.deepEqual(args, { y: 'a badger', x: null });
+ args = parser.parseArgs([ '--yyy=-a badger' ]);
+ assert.deepEqual(args, { y: '-a badger', x: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-y' ]);
+ });
+ });
+});
diff --git a/test/childgroups.js b/test/childgroups.js
new file mode 100644
index 0000000..066bbcd
--- /dev/null
+++ b/test/childgroups.js
@@ -0,0 +1,60 @@
+/*global describe, it, beforeEach*/
+// ActionContainer _addContainerActions() had errors passing groups from
+// parent to child parser. This tests for those.
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('child group', function () {
+ var args;
+ var parentParser, group, xgroup, childParser;
+
+ beforeEach(function () {
+ // parent has name group and exclusive group that should be passed to child
+ parentParser = new ArgumentParser({prog: 'PROG', debug: true, addHelp: false});
+ parentParser.addArgument(['--foo'], {help: 'parent foo'});
+ // parentParser.addArgument(['pbar'], {help: 'parent positional'});
+ group = parentParser.addArgumentGroup({title: 'parent group'});
+ group.addArgument(['--gfoo'], {help: 'group foo help'});
+ group.addArgument(['gbar'], {help: 'group bar help'});
+ xgroup = parentParser.addMutuallyExclusiveGroup({required: true});
+ xgroup.addArgument(['--xfoo'], {action: 'storeTrue', help: 'xfoo or xbar, set true'});
+ xgroup.addArgument(['--xbar'], {action: 'storeFalse', help: 'xfoo or xbar, set false'});
+ childParser = new ArgumentParser({parents: [parentParser], description: 'child parser', debug: true});
+ childParser.addArgument(['--cbar'], {help: 'child bar opt arg'});
+ });
+
+ it('compare help parent and child', function () {
+ // format helps and compare selected passages
+ var phelp = parentParser.formatHelp();
+ var chelp = childParser.formatHelp();
+ assert(phelp.match(/parent group:/));
+ assert(chelp.match(/parent group:/));
+ });
+
+ it('child should throw error if an xclusive group member is missing', function () {
+ assert.throws(
+ function () {
+ args = childParser.parseArgs(['gbararg']);
+ },
+ /one of the arguments (.*) is required/
+ );
+ });
+
+ it('child accepts an xgroup item and positional arg from parent', function () {
+ args = childParser.parseArgs(['--xbar', 'gbararg']);
+ assert.equal(args.gbar, 'gbararg');
+ assert.equal(args.xbar, false);
+ });
+
+ it('child throws error if both xclusive options are given', function () {
+ assert.throws(
+ function () {
+ args = childParser.parseArgs(['--xfoo', '--xbar']);
+ },
+ /Not allowed with argument/
+ );
+ });
+});
diff --git a/test/choices.js b/test/choices.js
new file mode 100644
index 0000000..f596af0
--- /dev/null
+++ b/test/choices.js
@@ -0,0 +1,70 @@
+/*global describe, it*/
+
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('choices', function () {
+ var parser;
+ var args;
+
+ it("should store correct choice(choices defined as string)", function () {
+ parser = new ArgumentParser({ debug: true });
+ parser.addArgument(['--foo'], {choices: 'abc'});
+
+ args = parser.parseArgs('--foo a'.split(' '));
+ assert.equal(args.foo, 'a');
+ });
+
+ it("should drop down with 'Invalid choice' error for incorrect choices(choices defined as string)", function () {
+ parser = new ArgumentParser({ debug: true });
+ parser.addArgument(['--foo'], {choices: 'abc'});
+
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--foo e'.split(' '));
+ console.dir(args);
+ },
+ /Invalid choice:/
+ );
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--foo 0'.split(' '));
+ console.dir(args);
+ },
+ /Invalid choice:/
+ );
+ });
+
+
+ it("should store correct choice(choices defined as array)", function () {
+ parser = new ArgumentParser({ debug: true });
+ parser.addArgument(['--foo'], {choices: ['a', 'abc', 'd']});
+
+ args = parser.parseArgs('--foo abc'.split(' '));
+ assert.equal(args.foo, 'abc');
+ });
+
+ it("should drop down with 'Invalid choice' error for incorrect choices(choices defined as array)", function () {
+ parser = new ArgumentParser({ debug: true });
+ parser.addArgument(['--foo'], {choices: ['a', 'abc', 'd']});
+
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--foo e'.split(' '));
+ console.dir(args);
+ },
+ /Invalid choice:/
+ );
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--foo 0'.split(' '));
+ console.dir(args);
+ },
+ /Invalid choice:/
+ );
+ });
+});
diff --git a/test/conflict.js b/test/conflict.js
new file mode 100644
index 0000000..953d0b0
--- /dev/null
+++ b/test/conflict.js
@@ -0,0 +1,97 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('Argument conflict handling', function () {
+ var parser;
+ var help;
+
+ it("test_bad_type", function () {
+ assert.throws(function () {
+ parser = new ArgumentParser({conflictHandler: 'foo'});
+ },
+ /invalid conflict resolution value: foo/i
+ );
+ });
+ it("test_conflict_error", function () {
+ parser = new ArgumentParser();
+ parser.addArgument(['-x']);
+ assert.throws(function () {
+ parser.addArgument(['-x']);
+ },
+ /Conflicting option string/i
+ );
+ parser.addArgument(['--spam']);
+ assert.throws(function () {
+ parser.addArgument(['--spam']);
+ },
+ /Conflicting option string/i
+ );
+ });
+ it("test_resolve_error", function () {
+ parser = new ArgumentParser({prog: 'PROG', conflictHandler: 'resolve'});
+ parser.addArgument(['-x'], {help: 'OLD X'});
+ parser.addArgument(['-x'], {help: 'NEW X'});
+ help = parser.formatHelp();
+ /* expect
+ usage: PROG [-h] [-x X]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -x X NEW X
+ */
+ assert(help.match(/usage: PROG \[-h\] \[-x X\]/im));
+ assert(help.match(/Show this help message and exit/im));
+ assert(help.match(/-x X\s*NEW X/im));
+ parser.addArgument(['--spam'], {metavar: 'OLD_SPAM'});
+ parser.addArgument(['--spam'], {metavar: 'NEW_SPAM'});
+ help = parser.formatHelp();
+ /* expect
+ usage: PROG [-h] [-x X] [--spam NEW_SPAM]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -x X NEW X
+ --spam NEW_SPAM
+ */
+ assert(help.match(/--spam NEW_SPAM/im));
+ });
+ it("TypeError with multiple conflicts", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['-b', '--bar']);
+ assert.throws(
+ function () { parser.addArgument(['--foo', '--bar', '--foobar']); },
+ /argument "--foo\/--bar\/--foobar": Conflicting option string\(s\): --foo, --bar/
+ );
+ });
+ it("resolving multiple conflicts", function () {
+ parser = new ArgumentParser({debug: true, conflictHandler: 'resolve'});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['--bar']);
+ help = parser.formatHelp();
+ /*
+ usage: _mocha [-h] [-f FOO] [--bar BAR]
+ Optional arguments:
+ -h, --help Show this help message and exit.
+ -f FOO, --foo FOO
+ --bar BAR
+ */
+ assert(help.match(/-f FOO, --foo FOO/im));
+ parser.addArgument(['--foo', '--bar', '--foobar']);
+ help = parser.formatHelp();
+ assert(help.match(/-f FOO$/im));
+ assert(help.match(/--foo FOO, --bar FOO, --foobar FOO/im));
+ /*
+ usage: _mocha [-h] [-f FOO] [--foo FOO]
+ Optional arguments:
+ -h, --help Show this help message and exit.
+ -f FOO
+ --foo FOO, --bar FOO, --foobar FOO
+ */
+ });
+});
diff --git a/test/constant.js b/test/constant.js
new file mode 100644
index 0000000..c56eb6a
--- /dev/null
+++ b/test/constant.js
@@ -0,0 +1,59 @@
+/*global describe, it, beforeEach*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('constant actions', function () {
+ var parser;
+ var args;
+
+ beforeEach(function () {
+ parser = new ArgumentParser({debug: true});
+ });
+
+ it("storeConst should store constant as given", function () {
+ parser.addArgument(['-a'], {action: 'storeConst', dest: 'answer',
+ help: 'store constant', constant: 42});
+ args = parser.parseArgs('-a'.split(' '));
+ assert.equal(args.answer, '42');
+ });
+
+ it("storeConst should give error if constant not given (or misspelled)", function () {
+ assert.throws(
+ function () {
+ parser.addArgument(
+ ['-a'],
+ {
+ action: 'storeConst',
+ dest: 'answer',
+ help: 'store constant',
+ const: 42
+ }
+ );
+ },
+ /constant option is required for storeAction/
+ );
+ });
+
+ it("appendConst should append constant as given", function () {
+ parser.addArgument([ '--str' ], {action: 'appendConst', dest: 'types',
+ help: 'append constant "str" to types', constant: 'str'});
+ parser.addArgument([ '--int' ], {action: 'appendConst', dest: 'types',
+ help: 'append constant "int" to types', constant: 'int'});
+ args = parser.parseArgs('--str --int'.split(' '));
+ assert.deepEqual(args.types, [ 'str', 'int' ]);
+ });
+
+ it("appendConst should give error if constant not given (or misspelled)", function () {
+ assert.throws(
+ function () {
+ parser.addArgument(['-a'], {action: 'appendConst', dest: 'answer',
+ help: 'store constant', const: 42});
+ },
+ /constant option is required for appendAction/
+ );
+ });
+});
diff --git a/test/fixtures/hello b/test/fixtures/hello
new file mode 100644
index 0000000..3efd786
--- /dev/null
+++ b/test/fixtures/hello
@@ -0,0 +1,2 @@
+hello world!
+
diff --git a/test/fixtures/invalid b/test/fixtures/invalid
new file mode 100644
index 0000000..3f3dffd
--- /dev/null
+++ b/test/fixtures/invalid
@@ -0,0 +1,2 @@
+ at no-such-path
+
diff --git a/test/fixtures/recursive b/test/fixtures/recursive
new file mode 100644
index 0000000..eabf1b2
--- /dev/null
+++ b/test/fixtures/recursive
@@ -0,0 +1,3 @@
+-a
+A
+ at hello
diff --git a/test/formatters.js b/test/formatters.js
new file mode 100644
index 0000000..805bba4
--- /dev/null
+++ b/test/formatters.js
@@ -0,0 +1,259 @@
+/*global describe, it*/
+'use strict';
+
+var assert = require('assert');
+var _ = require('underscore');
+_.str = require('underscore.string');
+
+var argparse = require('../lib/argparse');
+
+describe('formatterClass alternatives', function () {
+ var a, group, parser, helptext;
+
+ it('ArgumentDefaultsHelpFormatter', function () {
+
+ parser = new argparse.ArgumentParser({
+ debug: true,
+ formatterClass: argparse.ArgumentDefaultsHelpFormatter,
+ description: 'description'
+ });
+
+ parser.addArgument(['--foo'], {
+ help: 'foo help - oh and by the way, %(defaultValue)s'
+ });
+
+ parser.addArgument(['--bar'], {
+ action: 'storeTrue',
+ help: 'bar help'
+ });
+
+ parser.addArgument(['spam'], {
+ help: 'spam help'
+ });
+
+ parser.addArgument(['badger'], {
+ nargs: '?',
+ defaultValue: 'wooden',
+ help: 'badger help'
+ });
+
+ group = parser.addArgumentGroup({
+ title: 'title',
+ description: 'group description'
+ });
+
+ group.addArgument(['--baz'], {
+ type: 'int',
+ defaultValue: 42,
+ help: 'baz help'
+ });
+
+ helptext = parser.formatHelp();
+ // test selected clips
+ // test_argparse.py can match the whole help
+ assert(helptext.match(/badger help \(default: wooden\)/));
+ assert(helptext.match(/foo help - oh and by the way, null/));
+ assert(helptext.match(/bar help \(default: false\)/));
+ assert(helptext.match(/title:\n {2}group description/)); // test indent
+ assert(helptext.match(/baz help \(default: 42\)/im));
+
+/*
+usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
+
+description
+
+positional arguments:
+ spam spam help
+ badger badger help (default: wooden)
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help - oh and by the way, null
+ --bar bar help (default: false)
+
+title:
+ group description
+
+ --baz BAZ baz help (default: 42)
+*/
+ });
+
+ it('RawDescriptionHelpFormatter', function () {
+
+ parser = new argparse.ArgumentParser({
+ debug: true,
+ prog: 'PROG',
+ formatterClass: argparse.RawDescriptionHelpFormatter,
+ description: 'Keep the formatting\n' +
+ ' exactly as it is written\n' +
+ '\n' +
+ 'here\n'
+ });
+
+ a = parser.addArgument(['--foo'], {
+ help: ' foo help should not\n' +
+ ' retain this odd formatting'
+ });
+
+ parser.addArgument(['spam'], {
+ 'help': 'spam help'
+ });
+
+ group = parser.addArgumentGroup({
+ title: 'title',
+ description: ' This text\n' +
+ ' should be indented\n' +
+ ' exactly like it is here\n'
+ });
+
+ group.addArgument(['--bar'], {
+ help: 'bar help'
+ });
+
+ helptext = parser.formatHelp();
+ // test selected clips
+ // the parser description is not changed
+ assert(helptext.match(parser.description));
+ // the argument help is changed
+ assert.equal(helptext.match(a.help), null);
+ // the trimmed argument help matches
+ assert(helptext.match(/foo help should not retain this odd formatting/));
+
+/*
+usage: PROG [-h] [--foo FOO] [--bar BAR] spam
+
+Keep the formatting
+ exactly as it is written
+
+here
+
+positional arguments:
+ spam spam help
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help should not retain this odd formatting
+
+title:
+ This text
+ should be indented
+ exactly like it is here
+
+ --bar BAR bar help
+*/
+ });
+
+ it('RawTextHelpFormatter', function () {
+ parser = new argparse.ArgumentParser({
+ debug: true,
+ prog: 'PROG',
+ formatterClass: argparse.RawTextHelpFormatter,
+ description: 'Keep the formatting\n' +
+ ' exactly as it is written\n' +
+ '\n' +
+ 'here\n'
+ });
+
+ parser.addArgument(['--baz'], {
+ help: ' baz help should also\n' +
+ 'appear as given here'
+ });
+
+ a = parser.addArgument(['--foo'], {
+ help: ' foo help should also\n' +
+ 'appear as given here'
+ });
+
+ parser.addArgument(['spam'], {
+ 'help': 'spam help'
+ });
+
+ group = parser.addArgumentGroup({
+ title: 'title',
+ description: ' This text\n' +
+ ' should be indented\n' +
+ ' exactly like it is here\n'
+ });
+
+ group.addArgument(['--bar'], {
+ help: 'bar help'
+ });
+
+ helptext = parser.formatHelp();
+ // test selected clips
+ assert(helptext.match(parser.description));
+ // part of the unchanged argument help, with spaces
+ assert(helptext.match(/( {14})appear as given here/gm));
+
+/*
+usage: PROG [-h] [--foo FOO] [--bar BAR] spam
+
+Keep the formatting
+ exactly as it is written
+
+here
+
+positional arguments:
+ spam spam help
+
+optional arguments:
+ -h, --help show this help message and exit
+ --foo FOO foo help should also
+ appear as given here
+
+title:
+ This text
+ should be indented
+ exactly like it is here
+
+ --bar BAR bar help
+*/
+ });
+
+ it('should handle metavar as an array', function () {
+ parser = new argparse.ArgumentParser({
+ prog: 'PROG'
+ });
+
+ parser.addArgument(['-w'], {
+ help: 'w',
+ nargs: '+',
+ metavar: ['W1', 'W2']
+ });
+
+ parser.addArgument(['-x'], {
+ help: 'x',
+ nargs: '*',
+ metavar: ['X1', 'X2']
+ });
+
+ parser.addArgument(['-y'], {
+ help: 'y',
+ nargs: 3,
+ metavar: ['Y1', 'Y2', 'Y3']
+ });
+
+ parser.addArgument(['-z'], {
+ help: 'z',
+ nargs: '?',
+ metavar: ['Z1']
+ });
+
+ helptext = parser.formatHelp();
+ var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';
+ ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
+ // have to escape all of those brackets
+ assert(helptext.match(new RegExp(ustring)));
+
+/*
+usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]
+
+optional arguments:
+ -h, --help show this help message and exit
+ -w W1 [W2 ...] w
+ -x [X1 [X2 ...]] x
+ -y Y1 Y2 Y3 y
+ -z [Z1] z
+*/
+ });
+});
diff --git a/test/fromfile.js b/test/fromfile.js
new file mode 100644
index 0000000..f6e770b
--- /dev/null
+++ b/test/fromfile.js
@@ -0,0 +1,77 @@
+/*global describe, it, beforeEach, before, after*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+var assert = require('assert');
+var path = require('path');
+var _ = require('underscore');
+_.str = require('underscore.string');
+
+
+var orig_cwd = process.cwd();
+
+describe('from file', function () {
+ var parser;
+ var args;
+ before(function () {
+ orig_cwd = process.cwd();
+ process.chdir(path.normalize('./test/fixtures'));
+ });
+ beforeEach(function () {
+ parser = new ArgumentParser({debug: true, fromfilePrefixChars: '@'});
+ parser.addArgument(['-a']);
+ parser.addArgument(['x']);
+ parser.addArgument(['y'], {nargs: '+'});
+ });
+ after(function () {
+ process.chdir(orig_cwd);
+ });
+ it("test reading arguments from a file", function () {
+ args = parser.parseArgs(['X', 'Y']);
+ assert.deepEqual(args, {a: null, x: 'X', y: ['Y']});
+ args = parser.parseArgs(['X', '-a', 'A', 'Y', 'Z']);
+ assert.deepEqual(args, { a: 'A', x: 'X', y: ['Y', 'Z']});
+ args = parser.parseArgs(['@hello', 'X']);
+ assert.deepEqual(args, {a: null, x: 'hello world!', y: ['X']});
+ args = parser.parseArgs(['X', '@hello']);
+ assert.deepEqual(args, {a: null, x: 'X', y: ['hello world!']});
+ });
+ it("test recursive reading arguments from files", function () {
+ args = parser.parseArgs(['-a', 'B', '@recursive', 'Y', 'Z']);
+ assert.deepEqual(args, {a: 'A', x: 'hello world!', y: ['Y', 'Z']});
+ args = parser.parseArgs(['X', '@recursive', 'Z', '-a', 'B']);
+ assert.deepEqual(args, {a: 'B', x: 'X', y: ['hello world!', 'Z']});
+ });
+ it('fest reading arguments from an invalid file', function () {
+ assert.throws(
+ function () {
+ args = parser.parseArgs(['@invalid']);
+ },
+ /ENOENT, no such file or directory/
+ );
+ });
+ it('test reading arguments from an missing file', function () {
+ assert.throws(
+ function () {
+ args = parser.parseArgs(['@missing']);
+ },
+ /ENOENT, no such file or directory/
+ );
+ });
+ it('test custom convertArgLineToArgs function', function () {
+ parser.convertArgLineToArgs = function (argLine) {
+ // split line into 'words'
+ args = argLine.split(' ');
+ args = args.map(function (arg) {return arg.trim(); });
+ args = args.filter(function (arg) {return arg.length > 0; });
+ return args;
+ };
+ args = parser.parseArgs(['X', '@hello']);
+ assert.deepEqual(args, {a: null, x: 'X', y: ['hello', 'world!']});
+ });
+});
+
diff --git a/test/group.js b/test/group.js
new file mode 100644
index 0000000..c90c23f
--- /dev/null
+++ b/test/group.js
@@ -0,0 +1,183 @@
+/*global describe, it*/
+
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('group', function () {
+ var parser;
+ var args;
+ var group;
+ var group1;
+ var group2;
+
+ it('group test', function () {
+ parser = new ArgumentParser({prog: 'PROG', addHelp: false, debug: true});
+ group = parser.addArgumentGroup({title: 'group'});
+ group.addArgument(['--foo'], {help: 'foo help'});
+ group.addArgument(['bar'], {help: 'bar help'});
+ // what to test for in help?
+ // parser.print_help()
+ // does group make an difference in parseArgs output?
+ assert(group._groupActions.length, 2);
+ });
+
+ it('2 groups test', function () {
+ parser = new ArgumentParser({prog: 'PROG', addHelp: false, debug: true});
+ group1 = parser.addArgumentGroup({title: 'group1', description: 'group1 description'});
+ group1.addArgument(['foo'], {help: 'foo help'});
+ group2 = parser.addArgumentGroup({title: 'group2', description: 'group2 description'});
+ group2.addArgument(['--bar'], {help: 'bar help'});
+ //parser.print_help();
+ assert(group1._groupActions.length, 1);
+ assert(parser._actionGroups.length, 4); // group1, group2, positionals, optionals
+ });
+
+ it('mutually exclusive group test', function () {
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group = parser.addMutuallyExclusiveGroup();
+ group.addArgument(['--foo'], {action: 'storeTrue'});
+ group.addArgument(['--bar'], {action: 'storeFalse'});
+ args = parser.parseArgs([]);
+ // Python: Namespace(bar=True, foo=False)
+ assert.equal(args.foo, false);
+ assert.equal(args.bar, true);
+
+ args = parser.parseArgs(['--foo']);
+ // Python: Namespace(bar=True, foo=True)
+ assert.equal(args.foo, true);
+ assert.equal(args.bar, true);
+
+ args = parser.parseArgs(['--bar']);
+ // Python: Namespace(bar=False, foo=False)
+ assert.equal(args.foo || args.bar, false);
+ });
+
+ it('mutually exclusive group test (2)', function () {
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group = parser.addMutuallyExclusiveGroup();
+ group.addArgument(['--foo'], {action: 'storeTrue'});
+ group.addArgument(['--bar'], {action: 'storeFalse'});
+
+ assert.throws(
+ function () {
+ args = parser.parseArgs(['--foo', '--bar']);
+ },
+ // Python: error: argument --bar: not allowed with argument --foo
+ // I had problems with the proper pairing of bar and foo
+ // may also test case with 2 overlapping exlusive groups
+ // /("--bar"): Not allowed with argument ("--foo")/
+ function (err) {
+ // right and left actions should be different
+ // allow for variations in formatting
+ var pat = /(.*): not allowed with argument (.*)/i;
+ if (err instanceof Error) {
+ var m = err.message.match(pat);
+ return m && m[1] !== m[2];
+ }
+ },
+ "unexpected error"
+ );
+ });
+
+ it('mutually exclusive group test (3)', function () {
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group = parser.addMutuallyExclusiveGroup({required: true});
+ // or should the input be {required: true}?
+ group.addArgument(['--foo'], {action: 'storeTrue'});
+ group.addArgument(['--bar'], {action: 'storeFalse'});
+ assert.equal(group.required, true);
+ assert.equal(group._groupActions.length, 2);
+ assert.throws(
+ function () {
+ args = parser.parseArgs([]);
+ },
+ // Python: error: one of the arguments --foo --bar is required
+ /one of the arguments (.*) is required/i
+ );
+ });
+
+ it('mutually exclusive group usage', function () {
+ // adapted from test_argparse.py TestMutuallyExclusiveSimple
+ var usage;
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group = parser.addMutuallyExclusiveGroup({required: true});
+ // or should the input be {required: true}?
+ group.addArgument(['--bar'], {help: 'bar help'});
+ group.addArgument(['--baz'], {nargs: '?', constant: 'Z', help: 'baz help'});
+ args = parser.parseArgs(['--bar', 'X']);
+ assert.deepEqual(args, {bar: 'X', baz: null});
+
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--bar X --baz Y'.split(' '));
+ },
+ /Not allowed with argument/i
+ );
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] (--bar BAR | --baz [BAZ])\n');
+ group.required = false;
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] [--bar BAR | --baz [BAZ]]\n');
+ // could also test all or part of parser.formatHelp()
+ });
+
+ it('mutually exclusive optional and positional', function () {
+ // adapted from test_argparse.py TestMutuallyExclusiveOptionalAndPositional
+ var usage;
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group = parser.addMutuallyExclusiveGroup({required: true});
+ // or should the input be {required: true}?
+ group.addArgument(['--foo'], {action: 'storeTrue', help: 'foo help'});
+ group.addArgument(['--spam'], {help: 'spam help'});
+ group.addArgument(['badger'], {nargs: '*', defaultValue: 'X', help: 'badger help'});
+ args = parser.parseArgs(['--spam', 'S']);
+ assert.deepEqual(args, {foo: false, spam: 'S', badger: 'X'});
+ args = parser.parseArgs(['X']);
+ assert.deepEqual(args, {"foo": false, "spam": null, "badger": ['X']});
+ args = parser.parseArgs(['--foo']);
+ assert.deepEqual(args, {foo: true, spam: null, badger: 'X'});
+ assert.throws(
+ function () {
+ args = parser.parseArgs('--foo --spam 5'.split(' '));
+ },
+ /Not allowed with argument/i
+ );
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] (--foo | --spam SPAM | badger [badger ...])\n');
+ group.required = false;
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] [--foo | --spam SPAM | badger [badger ...]]\n');
+ });
+
+ it('two mutually exclusive groups', function () {
+ // adapted from test_argparse.py
+ var usage, group1, group2;
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group1 = parser.addMutuallyExclusiveGroup({required: true});
+ group1.addArgument(['--foo'], {action: 'storeTrue'});
+ group1.addArgument(['--bar'], {action: 'storeFalse'});
+ group2 = parser.addMutuallyExclusiveGroup({required: false});
+ group2.addArgument(['--soup'], {action: 'storeTrue'});
+ group2.addArgument(['--nuts'], {action: 'storeFalse'});
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] (--foo | --bar) [--soup | --nuts]\n');
+ });
+
+ it('suppressed and single action groups', function () {
+ // adapted from test_argparse.py
+ var usage, group1, group2;
+ parser = new ArgumentParser({prog: 'PROG', debug: true});
+ group1 = parser.addMutuallyExclusiveGroup();
+ group1.addArgument(['--sup'], {help: '==SUPPRESS=='});
+ // should produce an empty group (), which is removed
+ group2 = parser.addMutuallyExclusiveGroup({required: true});
+ group2.addArgument(['--xxx'], {});
+ // single entry in a required group, remove group ()
+ usage = parser.formatUsage();
+ assert.equal(usage, 'usage: PROG [-h] --xxx XXX\n');
+ });
+});
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..ec648f2
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1 @@
+-R spec
diff --git a/test/nargs.js b/test/nargs.js
new file mode 100644
index 0000000..641595d
--- /dev/null
+++ b/test/nargs.js
@@ -0,0 +1,860 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+var $$ = require('../lib/const');
+
+describe('nargs', function () {
+ var parser;
+ var args;
+ it('test specifying the 1 arg for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { nargs: 1 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { x: [ 'a' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ it('test specifying the 3 args for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { nargs: 3 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-x', 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { x: [ 'a', 'b', 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', 'a', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-x', 'b' ]);
+ });
+ });
+
+ it('tests not specifying the number of args for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { x: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ it('test specifying an args for an Optional, that accepts one or more', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { nargs: '+' });
+ parser.addArgument([ '-y' ], { "default": 'spam', nargs: '+', defaultValue: 'spam' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: 'spam', x: null });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { y: 'spam', x: [ 'a' ] });
+ args = parser.parseArgs([ '-x', 'a', 'b' ]);
+ assert.deepEqual(args, { y: 'spam', x: [ 'a', 'b' ] });
+ args = parser.parseArgs([ '-y', 'a' ]);
+ assert.deepEqual(args, { y: [ 'a' ], x: null });
+ args = parser.parseArgs([ '-y', 'a', 'b' ]);
+ assert.deepEqual(args, { y: [ 'a', 'b' ], x: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-y' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-y', 'b' ]);
+ });
+ });
+
+ it('test specifying an Optional arg for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-w' ], { nargs: '?' });
+ parser.addArgument([ '-x' ], { const: 42, nargs: '?', constant: 42 });
+ parser.addArgument([ '-y' ], { "default": 'spam', nargs: '?', defaultValue: 'spam' });
+ parser.addArgument([ '-z' ], {
+ "default": '84',
+ nargs: '?',
+ type: 'int',
+ const: '42',
+ defaultValue: '84',
+ constant: '42'
+ });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: null });
+ args = parser.parseArgs([ '-w' ]);
+ assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: null });
+ args = parser.parseArgs([ '-w', '2' ]);
+ assert.deepEqual(args, { y: 'spam', x: null, z: 84, w: '2' });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { y: 'spam', x: 42, z: 84, w: null });
+ args = parser.parseArgs([ '-x', '2' ]);
+ assert.deepEqual(args, { y: 'spam', x: '2', z: 84, w: null });
+ args = parser.parseArgs([ '-y' ]);
+ assert.deepEqual(args, { y: null, x: null, z: 84, w: null });
+ args = parser.parseArgs([ '-y', '2' ]);
+ assert.deepEqual(args, { y: '2', x: null, z: 84, w: null });
+ args = parser.parseArgs([ '-z' ]);
+ assert.deepEqual(args, { y: 'spam', x: null, z: 42, w: null });
+ args = parser.parseArgs([ '-z', '2' ]);
+ assert.deepEqual(args, { y: 'spam', x: null, z: 2, w: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '2' ]);
+ });
+ });
+
+ it('test specifying an args for an Optional that accepts zero or more', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { nargs: '*' });
+ parser.addArgument([ '-y' ], { "default": 'spam', nargs: '*', defaultValue: 'spam' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: 'spam', x: null });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { y: 'spam', x: [] });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { y: 'spam', x: [ 'a' ] });
+ args = parser.parseArgs([ '-x', 'a', 'b' ]);
+ assert.deepEqual(args, { y: 'spam', x: [ 'a', 'b' ] });
+ args = parser.parseArgs([ '-y' ]);
+ assert.deepEqual(args, { y: [], x: null });
+ args = parser.parseArgs([ '-y', 'a' ]);
+ assert.deepEqual(args, { y: [ 'a' ], x: null });
+ args = parser.parseArgs([ '-y', 'a', 'b' ]);
+ assert.deepEqual(args, { y: [ 'a', 'b' ], x: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test a Positional that specifies an nargs of 1', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [ 'a' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ });
+
+ it('test a Positional that specifies an nargs of 2', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 2 });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test a Positional with 2 nargs followed by one with none', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 2 });
+ parser.addArgument([ 'bar' ], {});
+
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: 'c' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c', 'd' ]);
+ });
+ });
+
+ it('test a Positional with 2 nargs followed by one with one or more', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 2 });
+ parser.addArgument([ 'bar' ], { nargs: '+' });
+
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ });
+
+ it('test a Positional with 2 nargs followed by one optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 2 });
+ parser.addArgument([ 'bar' ], { nargs: '?' });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: null });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: 'c' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c', 'd' ]);
+ });
+ });
+
+ it('test a Positional with 2 nargs followed by one with unlimited', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: 2 });
+ parser.addArgument([ 'bar' ], { nargs: '*' });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: [] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test a Positional that doesn\'t specify nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ });
+
+ it('test a Positional with no nargs followed by one with 1', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test two Positionals that don\'t specify nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], {});
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: 'b' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test a Positional with no nargs followed by one with one or more', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: '+' });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b', 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test three Positionals: no nargs, one or more nargs and 1 nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: '+' });
+ parser.addArgument([ 'baz' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { bar: [ 'b' ], foo: 'a', baz: [ 'c' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c', 'd' ]);
+ assert.deepEqual(args, { bar: [ 'b', 'c' ], foo: 'a', baz: [ 'd' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'b' ]);
+ });
+ });
+
+ it('test a Positional with no nargs followed by one with an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: '?' });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a', bar: null });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: 'b' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test three Positionals: no nargs, optional narg and 1 nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { "default": 0.625, nargs: '?', defaultValue: 0.625 });
+ parser.addArgument([ 'baz' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { bar: 0.625, foo: 'a', baz: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { bar: 'b', foo: 'a', baz: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test a Positional with no nargs followed by one with unlimited', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: '*' });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b', 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ });
+
+ it('test three Positionals: no nargs, unlimited nargs and 1 nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], {});
+ parser.addArgument([ 'bar' ], { nargs: '*' });
+ parser.addArgument([ 'baz' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { bar: [], foo: 'a', baz: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { bar: [ 'b' ], foo: 'a', baz: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test a Positional that specifies one or more nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '+' });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ it('test a Positional with one or more nargs followed by one with 1', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '+' });
+ parser.addArgument([ 'bar' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a' ], bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test a Positional with one or more nargs followed by one with none', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '+' });
+ parser.addArgument([ 'bar' ], {});
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a' ], bar: 'b' });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: 'c' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('tests an Optional Positional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: null });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ });
+
+ it('test a Positional with an Optional nargs followed by one with 1', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+ parser.addArgument([ 'bar' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: null, bar: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test an Optional Positional with a default value (that needs to be converted to the appropriate type.)', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { "default": '42', type: 'int', nargs: '?', defaultValue: '42' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: 42 });
+ args = parser.parseArgs([ '1' ]);
+ assert.deepEqual(args, { foo: 1 });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '1', '2' ]);
+ });
+ });
+
+ it('tests an Optional Positional with a default value', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { "default": 42, nargs: '?', defaultValue: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: 42 });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ });
+
+ it('test a Positional with an Optional nargs followed by one with none', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { "default": 42, nargs: '?', defaultValue: 42 });
+ parser.addArgument([ 'bar' ], {});
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 42, bar: 'a' });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: 'b' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test an Optional narg followed by one or more nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+ parser.addArgument([ 'bar' ], { nargs: '+' });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: null, bar: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b', 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ });
+
+ it('test two optional nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+ parser.addArgument([ 'bar' ], { "default": 42, nargs: '?', defaultValue: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: null, bar: 42 });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a', bar: 42 });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: 'b' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ });
+ });
+
+ it('test an Optional narg followed by unlimited nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+ parser.addArgument([ 'bar' ], { nargs: '*' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: null, bar: [] });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b', 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ });
+
+ it('test a Positional that specifies unlimited nargs', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '*' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: [] });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ // Added after bug in ndoc
+ it('test a `nargs` + `append` combination. Not obvious result - nested array', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { action: 'append', nargs: '+' });
+
+ args = parser.parseArgs([ 'a', 'b' ]);
+ // YES, NESTED array, don't try to 'fix' it.
+ assert.deepEqual(args, { foo: [[ 'a', 'b' ]] });
+ });
+
+
+ it('test a Positional with unlimited nargs followed by one with 1', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '*' });
+ parser.addArgument([ 'bar' ], { nargs: 1 });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [], bar: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a' ], bar: [ 'b' ] });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: [ 'c' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ });
+
+ it('test a Positional that specifies unlimited nargs and a default', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { "default": 'bar', nargs: '*', defaultValue: 'bar' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: 'bar' });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [ 'a' ] });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ it('test a Positional with unlimited nargs followed by one with none', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'foo' ], { nargs: '*' });
+ parser.addArgument([ 'bar' ], {});
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: [], bar: 'a' });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: [ 'a' ], bar: 'b' });
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { foo: [ 'a', 'b' ], bar: 'c' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ });
+
+
+ it('test specifying a positional with nargs=REMAINDER', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'x' ], {});
+ parser.addArgument([ 'y' ], { nargs: $$.REMAINDER });
+ parser.addArgument([ '-z' ], {});
+
+ args = parser.parseArgs([ 'X' ]);
+ assert.deepEqual(args, { y: [], x: 'X', z: null });
+ args = parser.parseArgs([ '-z', 'Z', 'X' ]);
+ assert.deepEqual(args, { y: [], x: 'X', z: 'Z' });
+ args = parser.parseArgs([ 'X', 'A', 'B', '-z', 'Z' ]);
+ assert.deepEqual(args, { y: [ 'A', 'B', '-z', 'Z' ], x: 'X', z: null });
+ args = parser.parseArgs([ 'X', 'Y', '--foo' ]);
+ assert.deepEqual(args, { y: [ 'Y', '--foo' ], x: 'X', z: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-z' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-z', 'Z' ]);
+ });
+ });
+
+ it('test specifying an args for an Optional that accepts zero or more', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { nargs: '*' });
+ parser.addArgument([ 'y' ], { nargs: '*' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: [], x: null });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { y: [], x: [] });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { y: [], x: [ 'a' ] });
+ args = parser.parseArgs([ '-x', 'a', '--', 'b' ]);
+ assert.deepEqual(args, { y: [ 'b' ], x: [ 'a' ] });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { y: [ 'a' ], x: null });
+ args = parser.parseArgs([ 'a', '-x' ]);
+ assert.deepEqual(args, { y: [ 'a' ], x: [] });
+ args = parser.parseArgs([ 'a', '-x', 'b' ]);
+ assert.deepEqual(args, { y: [ 'a' ], x: [ 'b' ] });
+
+ });
+
+ it("should accept defaultValue for nargs:'*'", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-f', '--foo']);
+ parser.addArgument(['bar'], { nargs: '*', defaultValue: 42});
+
+ args = parser.parseArgs([]);
+ assert.equal(args.bar, 42);
+ });
+
+ it("should accept negative scientific numbers as values", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['--xlim'], {nargs: 2, type: 'float'});
+
+ args = parser.parseArgs(['--xlim', '-2e-3', '1e4']);
+ assert.equal(args['xlim'][0], -0.002);
+ });
+
+});
diff --git a/test/optionals.js b/test/optionals.js
new file mode 100644
index 0000000..9776d36
--- /dev/null
+++ b/test/optionals.js
@@ -0,0 +1,825 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('optionals', function () {
+ var parser;
+ var args;
+
+ it('test options that may or may not be arguments', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { type: 'float' });
+ parser.addArgument([ '-3' ], { dest: 'y', type: 'float' });
+ parser.addArgument([ 'z' ], { nargs: '*' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: null, x: null, z: [] });
+ args = parser.parseArgs([ '-x', '2.5' ]);
+ assert.deepEqual(args, { y: null, x: 2.5, z: [] });
+ args = parser.parseArgs([ '-x', '2.5', 'a' ]);
+ assert.deepEqual(args, { y: null, x: 2.5, z: [ 'a' ] });
+ args = parser.parseArgs([ '-3.5' ]);
+ assert.deepEqual(args, { y: 0.5, x: null, z: [] });
+ args = parser.parseArgs([ '-3-.5' ]);
+ assert.deepEqual(args, { y: -0.5, x: null, z: [] });
+ args = parser.parseArgs([ '-3', '.5' ]);
+ assert.deepEqual(args, { y: 0.5, x: null, z: [] });
+ args = parser.parseArgs([ 'a', '-3.5' ]);
+ assert.deepEqual(args, { y: 0.5, x: null, z: [ 'a' ] });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { y: null, x: null, z: [ 'a' ] });
+ args = parser.parseArgs([ 'a', '-x', '1' ]);
+ assert.deepEqual(args, { y: null, x: 1, z: [ 'a' ] });
+ args = parser.parseArgs([ '-x', '1', 'a' ]);
+ assert.deepEqual(args, { y: null, x: 1, z: [ 'a' ] });
+ args = parser.parseArgs([ '-3', '1', 'a' ]);
+ assert.deepEqual(args, { y: 1, x: null, z: [ 'a' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-y2.5' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-xa' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-3' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-3.5' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-3', '-3.5' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-2.5' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-2.5', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-3', '-.5' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'x', '-1' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-1', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-3', '-1', 'a' ]);
+ });
+ });
+
+ it('test the append action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--baz' ], { action: 'append' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { baz: null });
+ args = parser.parseArgs([ '--baz', 'a' ]);
+ assert.deepEqual(args, { baz: [ 'a' ] });
+ args = parser.parseArgs([ '--baz', 'a', '--baz', 'b' ]);
+ assert.deepEqual(args, { baz: [ 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--baz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '--baz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--baz', 'a', 'b' ]);
+ });
+ });
+
+ it('test the append_const action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-b' ], { action: 'appendConst',
+ const: 'Exception',
+ constant: 'Exception' });
+ parser.addArgument([ '-c' ], { dest: 'b', action: 'append' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { b: null });
+ args = parser.parseArgs([ '-b' ]);
+ assert.deepEqual(args, { b: [ 'Exception' ] });
+ args = parser.parseArgs([ '-b', '-cx', '-b', '-cyz' ]);
+ assert.deepEqual(args, { b: [ 'Exception', 'x', 'Exception', 'yz' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-c' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-c' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-bx' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-b', 'x' ]);
+ });
+ });
+
+ it('test the append_const action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-b' ], { "default": [ 'X' ],
+ action: 'appendConst',
+ const: 'Exception',
+ defaultValue: [ 'X' ],
+ constant: 'Exception' });
+ parser.addArgument([ '-c' ], { dest: 'b', action: 'append' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { b: [ 'X' ] });
+ args = parser.parseArgs([ '-b' ]);
+ assert.deepEqual(args, { b: [ 'X', 'Exception' ] });
+ args = parser.parseArgs([ '-b', '-cx', '-b', '-cyz' ]);
+ assert.deepEqual(args, { b: [ 'X', 'Exception', 'x', 'Exception', 'yz' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-c' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-c' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-bx' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-b', 'x' ]);
+ });
+ });
+
+ it('test the append action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(
+ [ '--baz' ],
+ { "default": [ 'X' ], action: 'append', defaultValue: [ 'X' ] }
+ );
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { baz: [ 'X' ] });
+ args = parser.parseArgs([ '--baz', 'a' ]);
+ assert.deepEqual(args, { baz: [ 'X', 'a' ] });
+ args = parser.parseArgs([ '--baz', 'a', '--baz', 'b' ]);
+ assert.deepEqual(args, { baz: [ 'X', 'a', 'b' ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--baz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '--baz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--baz', 'a', 'b' ]);
+ });
+ });
+
+ it('test the count action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { action: 'count' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { x: 1 });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', 'a', '-x', 'b' ]);
+ });
+ });
+
+ it('test the store action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { action: 'store' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-xfoo' ]);
+ assert.deepEqual(args, { x: 'foo' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', '-x' ]);
+ });
+ });
+
+ it('test the store_const action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(
+ [ '-y' ],
+ { action: 'storeConst', const: 'object', constant: 'object' }
+ );
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: null });
+ args = parser.parseArgs([ '-y' ]);
+ assert.deepEqual(args, { y: 'object' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test the store_false action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-z' ], { action: 'storeFalse' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { z: true });
+ args = parser.parseArgs([ '-z' ]);
+ assert.deepEqual(args, { z: false });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-za' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-z', 'a' ]);
+ });
+ });
+
+ it('test the store_true action for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--apple' ], { action: 'storeTrue' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { apple: false });
+ args = parser.parseArgs([ '--apple' ]);
+ assert.deepEqual(args, { apple: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--apple=b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--apple', 'b' ]);
+ });
+ });
+
+ it('test negative number args when almost numeric options are present', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'x' ], { nargs: '?' });
+ parser.addArgument([ '-k4' ], { action: 'storeTrue', dest: 'y' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: false, x: null });
+ args = parser.parseArgs([ '-2' ]);
+ assert.deepEqual(args, { y: false, x: '-2' });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { y: false, x: 'a' });
+ args = parser.parseArgs([ '-k4' ]);
+ assert.deepEqual(args, { y: true, x: null });
+ args = parser.parseArgs([ '-k4', 'a' ]);
+ assert.deepEqual(args, { y: true, x: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-k3' ]);
+ });
+ });
+
+
+ it('test specifying the choices for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-f' ], { choices: 'abc' });
+ parser.addArgument([ '-g' ], { type: 'int', choices: [ 0, 1, 2, 3, 4 ] });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { g: null, f: null });
+ args = parser.parseArgs([ '-f', 'a' ]);
+ assert.deepEqual(args, { g: null, f: 'a' });
+ args = parser.parseArgs([ '-f', 'c' ]);
+ assert.deepEqual(args, { g: null, f: 'c' });
+ args = parser.parseArgs([ '-g', '0' ]);
+ assert.deepEqual(args, { g: 0, f: null });
+ args = parser.parseArgs([ '-g', '03' ]);
+ assert.deepEqual(args, { g: 3, f: null });
+ args = parser.parseArgs([ '-fb', '-g4' ]);
+ assert.deepEqual(args, { g: 4, f: 'b' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f', 'd' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fad' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-ga' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-g', '6' ]);
+ });
+ });
+
+ it('test specifying a default for an Optional', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], {});
+ parser.addArgument([ '-y' ], { "default": 42, defaultValue: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: 42, x: null });
+ args = parser.parseArgs([ '-xx' ]);
+ assert.deepEqual(args, { y: 42, x: 'x' });
+ args = parser.parseArgs([ '-yy' ]);
+ assert.deepEqual(args, { y: 'y', x: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test various means of setting destination', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--foo-bar' ], {});
+ parser.addArgument([ '--baz' ], { dest: 'zabbaz' });
+
+ args = parser.parseArgs([ '--foo-bar', 'f' ]);
+ assert.deepEqual(args, { zabbaz: null, foo_bar: 'f' });
+ args = parser.parseArgs([ '--baz', 'g' ]);
+ assert.deepEqual(args, { zabbaz: 'g', foo_bar: null });
+ args = parser.parseArgs([ '--foo-bar', 'h', '--baz', 'i' ]);
+ assert.deepEqual(args, { zabbaz: 'i', foo_bar: 'h' });
+ args = parser.parseArgs([ '--baz', 'j', '--foo-bar', 'k' ]);
+ assert.deepEqual(args, { zabbaz: 'j', foo_bar: 'k' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ });
+
+ it('test an Optional with a double-dash option string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--foo' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: null });
+ args = parser.parseArgs([ '--foo', 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ '--foo=a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ '--foo', '-2.5' ]);
+ assert.deepEqual(args, { foo: '-2.5' });
+ args = parser.parseArgs([ '--foo=-2.5' ]);
+ assert.deepEqual(args, { foo: '-2.5' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo', '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo', '--bar' ]);
+ });
+ });
+
+ it('tests partial matching with a double-dash option string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--badger' ], { action: 'storeTrue' });
+ parser.addArgument([ '--bat' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { bat: null, badger: false });
+ args = parser.parseArgs([ '--bat', 'X' ]);
+ assert.deepEqual(args, { bat: 'X', badger: false });
+ args = parser.parseArgs([ '--bad' ]);
+ assert.deepEqual(args, { bat: null, badger: true });
+ args = parser.parseArgs([ '--badg' ]);
+ assert.deepEqual(args, { bat: null, badger: true });
+ args = parser.parseArgs([ '--badge' ]);
+ assert.deepEqual(args, { bat: null, badger: true });
+ args = parser.parseArgs([ '--badger' ]);
+ assert.deepEqual(args, { bat: null, badger: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--ba' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--b=2' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--ba=4' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--badge', '5' ]);
+ });
+ });
+
+
+
+ it('test an Optional with a short opt string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-1' ], { dest: 'one' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { one: null });
+ args = parser.parseArgs([ '-1', 'a' ]);
+ assert.deepEqual(args, { one: 'a' });
+ args = parser.parseArgs([ '-1a' ]);
+ assert.deepEqual(args, { one: 'a' });
+ args = parser.parseArgs([ '-1-2' ]);
+ assert.deepEqual(args, { one: '-2' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-1' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-1', '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-1', '-y' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-1', '-1' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-1', '-2' ]);
+ });
+ });
+
+ it('test negative number args when numeric options are present', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'x' ], { nargs: '?' });
+ parser.addArgument([ '-4' ], { action: 'storeTrue', dest: 'y' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: false, x: null });
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { y: false, x: 'a' });
+ args = parser.parseArgs([ '-4' ]);
+ assert.deepEqual(args, { y: true, x: null });
+ args = parser.parseArgs([ '-4', 'a' ]);
+ assert.deepEqual(args, { y: true, x: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-2' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-315' ]);
+ });
+ });
+
+ it('tests the an optional action that is required', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { required: true, type: 'int' });
+
+ args = parser.parseArgs([ '-x', '1' ]);
+ assert.deepEqual(args, { x: 1 });
+ args = parser.parseArgs([ '-x42' ]);
+ assert.deepEqual(args, { x: 42 });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ });
+
+ it('test a combination of single- and double-dash option strings', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-v', '--verbose', '-n', '--noisy' ], { action: 'storeTrue' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { verbose: false });
+ args = parser.parseArgs([ '-v' ]);
+ assert.deepEqual(args, { verbose: true });
+ args = parser.parseArgs([ '--verbose' ]);
+ assert.deepEqual(args, { verbose: true });
+ args = parser.parseArgs([ '-n' ]);
+ assert.deepEqual(args, { verbose: true });
+ args = parser.parseArgs([ '--noisy' ]);
+ assert.deepEqual(args, { verbose: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--x', '--verbose' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-N' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-v', 'x' ]);
+ });
+ });
+
+ it('test an Optional with a single-dash option string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: null });
+ args = parser.parseArgs([ '-x', 'a' ]);
+ assert.deepEqual(args, { x: 'a' });
+ args = parser.parseArgs([ '-xa' ]);
+ assert.deepEqual(args, { x: 'a' });
+ args = parser.parseArgs([ '-x', '-1' ]);
+ assert.deepEqual(args, { x: '-1' });
+ args = parser.parseArgs([ '-x-1' ]);
+ assert.deepEqual(args, { x: '-1' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-y' ]);
+ });
+ });
+
+ it('test Optionals that partially match but are not subsets', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-foobar' ], {});
+ parser.addArgument([ '-foorab' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foorab: null, foobar: null });
+ args = parser.parseArgs([ '-foob', 'a' ]);
+ assert.deepEqual(args, { foorab: null, foobar: 'a' });
+ args = parser.parseArgs([ '-foor', 'a' ]);
+ assert.deepEqual(args, { foorab: 'a', foobar: null });
+ args = parser.parseArgs([ '-fooba', 'a' ]);
+ assert.deepEqual(args, { foorab: null, foobar: 'a' });
+ args = parser.parseArgs([ '-foora', 'a' ]);
+ assert.deepEqual(args, { foorab: 'a', foobar: null });
+ args = parser.parseArgs([ '-foobar', 'a' ]);
+ assert.deepEqual(args, { foorab: null, foobar: 'a' });
+ args = parser.parseArgs([ '-foorab', 'a' ]);
+ assert.deepEqual(args, { foorab: 'a', foobar: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fa' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foa' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo', 'b' ]);
+ });
+ });
+
+ it('test an Optional with a single-dash option string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-x' ], { action: 'storeTrue' });
+ parser.addArgument([ '-yyy' ], { action: 'storeConst', const: 42, constant: 42 });
+ parser.addArgument([ '-z' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { x: false, z: null, yyy: null });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { x: true, z: null, yyy: null });
+ args = parser.parseArgs([ '-za' ]);
+ assert.deepEqual(args, { x: false, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-z', 'a' ]);
+ assert.deepEqual(args, { x: false, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-xza' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-xz', 'a' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-x', '-za' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-x', '-z', 'a' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: null });
+ args = parser.parseArgs([ '-y' ]);
+ assert.deepEqual(args, { x: false, z: null, yyy: 42 });
+ args = parser.parseArgs([ '-yyy' ]);
+ assert.deepEqual(args, { x: false, z: null, yyy: 42 });
+ args = parser.parseArgs([ '-x', '-yyy', '-za' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: 42 });
+ args = parser.parseArgs([ '-x', '-yyy', '-z', 'a' ]);
+ assert.deepEqual(args, { x: true, z: 'a', yyy: 42 });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-xa' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x', '-z' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-z', '-x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-yx' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-yz', 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-yyyx' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-yyyza' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-xyza' ]);
+ });
+ });
+
+ it('test an Optional with a multi-character single-dash option string', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-foo' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foo: null });
+ args = parser.parseArgs([ '-foo', 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ '-foo', '-1' ]);
+ assert.deepEqual(args, { foo: '-1' });
+ args = parser.parseArgs([ '-fo', 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ '-f', 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo', '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo', '-y' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fooa' ]);
+ });
+ });
+
+ it('test Optionals where option strings are subsets of each other', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-f' ], {});
+ parser.addArgument([ '-foobar' ], {});
+ parser.addArgument([ '-foorab' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { foorab: null, f: null, foobar: null });
+ args = parser.parseArgs([ '-f', 'a' ]);
+ assert.deepEqual(args, { foorab: null, f: 'a', foobar: null });
+ args = parser.parseArgs([ '-fa' ]);
+ assert.deepEqual(args, { foorab: null, f: 'a', foobar: null });
+ args = parser.parseArgs([ '-foa' ]);
+ assert.deepEqual(args, { foorab: null, f: 'oa', foobar: null });
+ args = parser.parseArgs([ '-fooa' ]);
+ assert.deepEqual(args, { foorab: null, f: 'ooa', foobar: null });
+ args = parser.parseArgs([ '-foobar', 'a' ]);
+ assert.deepEqual(args, { foorab: null, f: null, foobar: 'a' });
+ args = parser.parseArgs([ '-foorab', 'a' ]);
+ assert.deepEqual(args, { foorab: 'a', f: null, foobar: null });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foo', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foob' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fooba' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-foora' ]);
+ });
+ });
+
+ it('test an Optional with single- and double-dash option strings', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '-f' ], { action: 'storeTrue' });
+ parser.addArgument([ '--bar' ], {});
+ parser.addArgument([ '-baz' ], { action: 'storeConst', const: 42, constant: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { bar: null, baz: null, f: false });
+ args = parser.parseArgs([ '-f' ]);
+ assert.deepEqual(args, { bar: null, baz: null, f: true });
+ args = parser.parseArgs([ '--ba', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: false });
+ args = parser.parseArgs([ '-f', '--bar', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: true });
+ args = parser.parseArgs([ '-f', '-b' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+ args = parser.parseArgs([ '-ba', '-f' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fbar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fbaz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-bazf' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-b', 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'B' ]);
+ });
+ });
+
+});
diff --git a/test/parents.js b/test/parents.js
new file mode 100644
index 0000000..c46f02b
--- /dev/null
+++ b/test/parents.js
@@ -0,0 +1,44 @@
+/*global describe, it, beforeEach*/
+
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('parents', function () {
+ var parent_parser;
+ var args;
+
+ beforeEach(function () {
+ parent_parser = new ArgumentParser({debug: true, addHelp: false});
+ parent_parser.addArgument(['--parent']);
+ });
+
+ it("should parse args from parents parser", function () {
+ var parser = new ArgumentParser({
+ parents: [ parent_parser ]
+ });
+ parser.addArgument(['-f', '--foo']);
+
+ args = parser.parseArgs('-f 1 --parent 2'.split(' '));
+ assert.equal(args.foo, 1);
+ assert.equal(args.parent, 2);
+
+ args = parser.parseArgs('-f 1'.split(' '));
+ assert.equal(args.foo, 1);
+ assert.strictEqual(args.parent, null);
+ });
+
+ it("should throw error if has same args as parent", function () {
+ var parser = new ArgumentParser({
+ parents: [ parent_parser ]
+ });
+ parser.addArgument(['-f', '--foo']);
+
+ assert.throws(function () {
+ parent_parser.addArgument(['--parent']);
+ });
+ });
+});
diff --git a/test/positionals.js b/test/positionals.js
new file mode 100644
index 0000000..0ff13fc
--- /dev/null
+++ b/test/positionals.js
@@ -0,0 +1,93 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('positionals', function () {
+ var parser;
+ var args;
+
+ it("test the 'append' action", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'spam' ], { action: 'append' });
+ parser.addArgument([ 'spam' ], { action: 'append', nargs: 2 });
+
+ args = parser.parseArgs([ 'a', 'b', 'c' ]);
+ assert.deepEqual(args, { spam: [ 'a', [ 'b', 'c' ] ] });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'a', 'b', 'c', 'd' ]);
+ });
+ });
+
+ it('test a set of integer choices', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'spam' ], {
+ type: 'int',
+ choices: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
+ });
+
+ args = parser.parseArgs([ '4' ]);
+ assert.deepEqual(args, { spam: 4 });
+ args = parser.parseArgs([ '15' ]);
+ assert.deepEqual(args, { spam: 15 });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'h' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '42' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'ef' ]);
+ });
+ });
+
+ it('test a set of single-character choices', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ 'spam' ], { choices: [ 'a', 'c', 'b', 'e', 'd', 'g', 'f' ] });
+
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { spam: 'a' });
+ args = parser.parseArgs([ 'g' ]);
+ assert.deepEqual(args, { spam: 'g' });
+
+ assert.throws(function () {
+ args = parser.parseArgs([]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--foo' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'h' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '42' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'ef' ]);
+ });
+ });
+
+});
diff --git a/test/prefix.js b/test/prefix.js
new file mode 100644
index 0000000..c337a65
--- /dev/null
+++ b/test/prefix.js
@@ -0,0 +1,223 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('prefix', function () {
+ var parser;
+ var args;
+ it('test an Optional with option strings with custom prefixes', function () {
+ parser = new ArgumentParser({
+ addHelp: false,
+ prefixChars: '+:/',
+ debug: true
+ });
+ parser.addArgument([ '+f' ], { action: 'storeTrue' });
+ parser.addArgument([ '::bar' ], {});
+ parser.addArgument([ '/baz' ], { action: 'storeConst', const: 42, constant: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { bar: null, baz: null, f: false });
+ args = parser.parseArgs([ '+f' ]);
+ assert.deepEqual(args, { bar: null, baz: null, f: true });
+ args = parser.parseArgs([ '::ba', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: false });
+ args = parser.parseArgs([ '+f', '::bar', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: true });
+ args = parser.parseArgs([ '+f', '/b' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+ args = parser.parseArgs([ '/ba', '+f' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fbar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-b', 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar', 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-baz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-h' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--help' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '+h' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '::help' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '/help' ]);
+ });
+ });
+
+ // http://bugs.python.org/issue9444
+ it('when "-" not in prefix_chars, default operators created for help should use the prefix_chars in use rather than - or --', function () {
+ parser = new ArgumentParser({
+ addHelp: true,
+ prefixChars: '+:/',
+ debug: true
+ });
+ parser.addArgument([ '+f' ], { action: 'storeTrue' });
+ parser.addArgument([ '::bar' ], {});
+ parser.addArgument([ '/baz' ], { action: 'storeConst', const: 42, constant: 42 });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { bar: null, baz: null, f: false });
+ args = parser.parseArgs([ '+f' ]);
+ assert.deepEqual(args, { bar: null, baz: null, f: true });
+ args = parser.parseArgs([ '::ba', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: false });
+ args = parser.parseArgs([ '+f', '::bar', 'B' ]);
+ assert.deepEqual(args, { bar: 'B', baz: null, f: true });
+ args = parser.parseArgs([ '+f', '/b' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+ args = parser.parseArgs([ '/ba', '+f' ]);
+ assert.deepEqual(args, { bar: null, baz: 42, f: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-fbar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-b', 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-f' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar', 'B' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-baz' ]);
+ });
+ });
+
+ it('verify that Optionals must be called with their defined prefixes', function () {
+ parser = new ArgumentParser({
+ addHelp: false,
+ prefixChars: '+-',
+ debug: true
+ });
+ parser.addArgument([ '-x' ], { action: 'storeTrue' });
+ parser.addArgument([ '+y' ], { action: 'storeTrue' });
+ parser.addArgument([ '+z' ], { action: 'storeTrue' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: false, x: false, z: false });
+ args = parser.parseArgs([ '-x' ]);
+ assert.deepEqual(args, { y: false, x: true, z: false });
+ args = parser.parseArgs([ '+y', '-x' ]);
+ assert.deepEqual(args, { y: true, x: true, z: false });
+ args = parser.parseArgs([ '+yz', '-x' ]);
+ assert.deepEqual(args, { y: true, x: true, z: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-w' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-xyz' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '+x' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '-y' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '+xyz' ]);
+ });
+ });
+
+ it('test when one double-dash option string is a prefix of another', function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument([ '--badger' ], { action: 'storeTrue' });
+ parser.addArgument([ '--ba' ], {});
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { ba: null, badger: false });
+ args = parser.parseArgs([ '--ba', 'X' ]);
+ assert.deepEqual(args, { ba: 'X', badger: false });
+ args = parser.parseArgs([ '--ba=X' ]);
+ assert.deepEqual(args, { ba: 'X', badger: false });
+ args = parser.parseArgs([ '--bad' ]);
+ assert.deepEqual(args, { ba: null, badger: true });
+ args = parser.parseArgs([ '--badg' ]);
+ assert.deepEqual(args, { ba: null, badger: true });
+ args = parser.parseArgs([ '--badge' ]);
+ assert.deepEqual(args, { ba: null, badger: true });
+ args = parser.parseArgs([ '--badger' ]);
+ assert.deepEqual(args, { ba: null, badger: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '--bar' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--b' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--ba' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--b=2' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '--badge', '5' ]);
+ });
+ });
+
+ it('test arguments from prefix chars only', function () {
+ parser = new ArgumentParser({
+ prefixChars: '-+',
+ debug: true
+ });
+ parser.addArgument([ '-' ], { dest: 'x', const: 'badger', nargs: '?', constant: 'badger' });
+ parser.addArgument([ '+' ], { "default": 42, dest: 'y', type: 'int', defaultValue: 42 });
+ parser.addArgument([ '-+-' ], { action: 'storeTrue', dest: 'z' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, { y: 42, x: null, z: false });
+ args = parser.parseArgs([ '-' ]);
+ assert.deepEqual(args, { y: 42, x: 'badger', z: false });
+ args = parser.parseArgs([ '-', 'X' ]);
+ assert.deepEqual(args, { y: 42, x: 'X', z: false });
+ args = parser.parseArgs([ '+', '-3' ]);
+ assert.deepEqual(args, { y: -3, x: null, z: false });
+ args = parser.parseArgs([ '-+-' ]);
+ assert.deepEqual(args, { y: 42, x: null, z: true });
+ args = parser.parseArgs([ '-', '===' ]);
+ assert.deepEqual(args, { y: 42, x: '===', z: false });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-y' ]);
+ });
+ assert.throws(function () {
+ args = parser.parseArgs([ '+', '-' ]);
+ });
+ });
+
+});
diff --git a/test/sub_commands.js b/test/sub_commands.js
new file mode 100644
index 0000000..261f658
--- /dev/null
+++ b/test/sub_commands.js
@@ -0,0 +1,86 @@
+/*global describe, it, beforeEach*/
+
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('sub-commands', function () {
+ var parser;
+ var args;
+ var c1;
+ var c2;
+
+ beforeEach(function () {
+ parser = new ArgumentParser({debug: true});
+ var subparsers = parser.addSubparsers({
+ title: 'subcommands',
+ dest: 'subcommand_name'
+ });
+ c1 = subparsers.addParser('c1', {aliases: ['co']});
+ c1.addArgument([ '-f', '--foo' ], {});
+ c1.addArgument([ '-b', '--bar' ], {});
+ c2 = subparsers.addParser('c2', {});
+ c2.addArgument([ '--baz' ], {});
+ });
+
+ it("should store command name", function () {
+ args = parser.parseArgs('c1 --foo 5'.split(' '));
+ assert.equal(args.subcommand_name, 'c1');
+ });
+
+ it("should store command arguments", function () {
+ args = parser.parseArgs('c1 --foo 5 -b4'.split(' '));
+ assert.equal(args.foo, 5);
+ assert.equal(args.bar, 4);
+ });
+
+ it("should have same behavior for alias and original command", function () {
+ args = parser.parseArgs('c1 --foo 5 -b4'.split(' '));
+ var aliasArgs = parser.parseArgs('co --foo 5 -b4'.split(' '));
+ assert.equal(args.foo, aliasArgs.foo);
+ assert.equal(args.bar, aliasArgs.bar);
+ });
+
+ it("should have different behavior for different commands", function () {
+ assert.doesNotThrow(function () {
+ parser.parseArgs('c1 --foo 5 -b4'.split(' '));
+ });
+ assert.throws(function () {
+ parser.parseArgs('c2 --foo 5 -b4'.split(' '));
+ });
+ assert.doesNotThrow(function () {
+ parser.parseArgs('c2 --baz 1'.split(' '));
+ });
+ assert.throws(function () {
+ parser.parseArgs('c1 --baz 1'.split(' '));
+ });
+ });
+
+ it('should drop down with "Invalid choice" error if parse unrecognized command', function () {
+ assert.throws(
+ function () {parser.parseArgs('command --baz 1'.split(' ')); },
+ /Invalid choice:/
+ );
+ });
+
+ it("should drop down with empty args ('too few arguments' error)", function () {
+ assert.throws(
+ function () {parser.parseArgs([]); },
+ /too few arguments/
+ );
+ });
+
+ it("should support #setDefaults", function () {
+ c1.setDefaults({spam: 1});
+ c2.setDefaults({eggs: 2});
+ args = parser.parseArgs(['c1']);
+ assert.equal(args.spam, 1);
+ assert.strictEqual(args.eggs, undefined);
+ args = parser.parseArgs(['c2']);
+ assert.equal(args.eggs, 2);
+ assert.strictEqual(args.spam, undefined);
+ });
+});
diff --git a/test/suppress.js b/test/suppress.js
new file mode 100644
index 0000000..1965410
--- /dev/null
+++ b/test/suppress.js
@@ -0,0 +1,78 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+var $$ = require('../lib/const');
+
+describe('suppress defaults', function () {
+ var parser;
+ var args;
+ it('test actions with suppressed defaults', function () {
+ parser = new ArgumentParser({debug: true});
+
+ parser.addArgument([ 'foo' ], {
+ "default": $$.SUPPRESS,
+ nargs: '?',
+ defaultValue: $$.SUPPRESS
+ });
+ parser.addArgument([ 'bar' ], {
+ "default": $$.SUPPRESS,
+ nargs: '*',
+ defaultValue: $$.SUPPRESS
+ });
+ parser.addArgument([ '--baz' ], {
+ "default": $$.SUPPRESS,
+ action: 'storeTrue',
+ defaultValue: $$.SUPPRESS
+ });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, {});
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ '--baz' ]);
+ assert.deepEqual(args, { baz: true });
+ args = parser.parseArgs([ 'a', '--baz' ]);
+ assert.deepEqual(args, { foo: 'a', baz: true });
+ args = parser.parseArgs([ '--baz', 'a', 'b' ]);
+ assert.deepEqual(args, { bar: [ 'b' ], foo: 'a', baz: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+ it('test actions with a parser-level default of SUPPRESS', function () {
+ parser = new ArgumentParser({
+ argument_default: $$.SUPPRESS,
+ argumentDefault: $$.SUPPRESS,
+ debug: true
+ });
+ parser.addArgument([ 'foo' ], { nargs: '?' });
+ parser.addArgument([ 'bar' ], { nargs: '*' });
+ parser.addArgument([ '--baz' ], { action: 'storeTrue' });
+
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, {});
+ args = parser.parseArgs([ 'a' ]);
+ assert.deepEqual(args, { foo: 'a' });
+ args = parser.parseArgs([ 'a', 'b' ]);
+ assert.deepEqual(args, { foo: 'a', bar: [ 'b' ] });
+ args = parser.parseArgs([ '--baz' ]);
+ assert.deepEqual(args, { baz: true });
+ args = parser.parseArgs([ 'a', '--baz' ]);
+ assert.deepEqual(args, { foo: 'a', baz: true });
+ args = parser.parseArgs([ '--baz', 'a', 'b' ]);
+ assert.deepEqual(args, { bar: [ 'b' ], foo: 'a', baz: true });
+
+ assert.throws(function () {
+ args = parser.parseArgs([ '-x' ]);
+ });
+ });
+
+});
diff --git a/test/user_defined_type.js b/test/user_defined_type.js
new file mode 100644
index 0000000..8b795ac
--- /dev/null
+++ b/test/user_defined_type.js
@@ -0,0 +1,119 @@
+/*global describe, it*/
+
+'use strict';
+
+var assert = require('assert');
+
+var ArgumentParser = require('../lib/argparse').ArgumentParser;
+
+describe('user defined type', function () {
+ var parser;
+ var args;
+
+ it("should handle builtin types", function () {
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['--eggs'], {type: 'int'});
+ parser.addArgument(['spam'], {type: 'float'});
+
+ args = parser.parseArgs(['--eggs=42', '42']);
+ assert.deepEqual(args, {eggs: 42, spam: 42.0});
+ args = parser.parseArgs(['1024.675']);
+ assert.deepEqual(args, {eggs: null, spam: 1024.675});
+ assert.throws(
+ function () { parser.parseArgs(['--eggs', 'a']); },
+ /Invalid int value: a/i
+ );
+ });
+
+ it("should handle user-defined type", function () {
+ function myType(arg) {
+ return arg;
+ }
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-x'], {type: myType});
+ parser.addArgument(['spam'], {type: myType});
+
+ args = parser.parseArgs(['a', '-x', 'b']);
+ assert.deepEqual(args, {x: myType('b'), spam: myType('a')});
+ args = parser.parseArgs(['-xf', 'g']);
+ assert.deepEqual(args, {x: myType('f'), spam: myType('g')});
+ });
+
+ it("should give consistent type errors", function () {
+ function dateType(arg) {
+ var x = new Date(arg);
+ if (x.toString().match('Invalid')) {
+ throw new TypeError("" + arg + " is not a valid date.");
+ }
+ return x;
+ }
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-i'], {type: 'int', defaultValue: 0});
+ parser.addArgument(['-f'], {type: 'float', defaultValue: 0});
+ parser.addArgument(['-d'], {type: dateType, defaultValue: new Date(0)});
+ assert.throws(
+ function () { parser.parseArgs(['-f', 'abc']); },
+ /Invalid float value: abc/i
+ );
+ assert.throws(
+ function () { parser.parseArgs(['-i', 'abc']); },
+ /Invalid int value: abc/i
+ );
+ args = parser.parseArgs([]);
+ assert.deepEqual(args, {i: 0, f: 0, d: new Date(0)});
+ args = parser.parseArgs(['-d', '1/1/2012']);
+ assert.deepEqual(args, {i: 0, f: 0, d: new Date('1/1/2012')});
+ assert.throws(
+ function () {parser.parseArgs(['-d', '13/1/2000']); },
+ /Invalid dateType value: (.*)/i
+ /*
+ it used to insert the function code rather than its name
+ Invalid <dateType.toString()> value: 13/1/2000
+ */
+ );
+ assert.throws(
+ function () { parser.parseArgs(['-d', 'abc']); },
+ /Invalid dateType value: (.*)/i
+ );
+ });
+
+ it("test a user-defined type by registering it", function () {
+ function dateType(arg) {
+ var x = new Date(arg);
+ if (x.toString().match('Invalid')) {
+ throw new TypeError("" + arg + " is not a valid date.");
+ }
+ return x;
+ }
+ parser = new ArgumentParser({debug: true});
+ parser.register('type', 'dateType', dateType);
+ parser.addArgument(['-d'], {type: 'dateType'});
+ args = parser.parseArgs(['-d', '1/1/2012']);
+ assert.deepEqual(args, {d: new Date('1/1/2012')});
+ assert.throws(
+ function () { parser.parseArgs(['-d', '13/1/2000']); },
+ /Invalid dateType value: (.*)/
+ );
+ });
+
+ it("test an anonymous user-defined type", function () {
+ var dateType = function (arg) {
+ var x = new Date(arg);
+ if (x.toString().match('Invalid')) {
+ throw new TypeError("" + arg + " is not a valid date.");
+ }
+ return x;
+ };
+ //dateType.displayName = 'dateType';
+ parser = new ArgumentParser({debug: true});
+ parser.addArgument(['-d'], {type: dateType});
+ args = parser.parseArgs(['-d', '1/1/2012']);
+ assert.deepEqual(args, {d: new Date('1/1/2012')});
+ assert.throws(
+ function () { parser.parseArgs(['-d', 'abc']); },
+ /Invalid <function> value: abc\nabc is not a valid date/im
+ );
+ });
+});
+// could test for: Error: "dateType" is not callable
+// by using an unregistered string or other nonfunction
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-argparse.git
More information about the Pkg-javascript-commits
mailing list