Permalink
Browse files

Merge pull request #43 from applepicke/master

Conflict Handler
  • Loading branch information...
2 parents 4c7e90c + d3b145e commit 2141d0a712c5009f9d6984933c808a8260195b59 @shkuropat shkuropat committed Jan 23, 2013
Showing with 26 additions and 5 deletions.
  1. +2 −1 README.md
  2. +10 −3 lib/action_container.js
  3. +1 −1 lib/argument/group.js
  4. +2 −0 lib/argument_parser.js
  5. +11 −0 test/base.js
View
@@ -87,11 +87,12 @@ Creates a new ArgumentParser object.
- ```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.
-- ```conflictHandler``` - Usually unnecessary, defines strategy for resolving conflicting optionals.
+
Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
View
@@ -39,15 +39,15 @@ var argumentErrorHelper = require('./argument/error');
* - `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 || '';
-
- // ToDo conflict handler
+ this.conflictHandler = options.conflictHandler;
// set up registries
this._registries = {};
@@ -416,17 +416,24 @@ ActionContainer.prototype._popActionClass = function (options, defaultValue) {
};
ActionContainer.prototype._checkConflict = function (action) {
+ var conflictHandler = this._container.conflictHandler;
var optionStringActions = this._optionStringActions;
var conflictOptionals = [];
// find all options that conflict with this option
action.optionStrings.forEach(function (optionString) {
if (!!optionStringActions[optionString]) {
- conflictOptionals.push([optionString]);
+ conflictOptionals.push(optionString);
}
});
if (conflictOptionals.length > 0) {
+
+ if (conflictHandler === 'resolve') {
+ this._removeAction(optionStringActions['--glop']);
@hpaulj

hpaulj Feb 8, 2013

Contributor

--glop??? Looks like something left over from testing.

The Python version delegates action to functions _handle_conflict_error and _handle_conflict_resolve
and can handle multiple conflicts

+ return;
+ }
+
throw argumentErrorHelper(
action,
_.str.sprintf('Conflicting option string(s): %(conflict)s', {
View
@@ -66,6 +66,6 @@ ArgumentGroup.prototype._addAction = function (action) {
ArgumentGroup.prototype._removeAction = function (action) {
// Parent remove action
ActionContainer.prototype._removeAction.call(this, action);
- this._groupActions.remove(action);
+ this._groupActions.splice(action);
@hpaulj

hpaulj Feb 8, 2013

Contributor

splice takes 2 numbers, not an object

};
View
@@ -41,6 +41,7 @@ var Namespace = require('./namespace');
* - `prefixChars` Characters that prefix optional 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)
@@ -68,6 +69,7 @@ var ArgumentParser = module.exports = function ArgumentParser(options) {
this.version = options.version;
this.debug = (options.debug === true);
+ this.conflictHandler = options.conflictHandler;
this.formatterClass = (options.formatterClass || HelpFormatter);
View
@@ -115,6 +115,17 @@ describe('base', function () {
);
});
+ it("should overwrite arguments when given the 'resolve' conflictHandler", function () {
+ parser = new ArgumentParser({conflictHandler: 'resolve'});
+
+ parser.addArgument(['--foo'], {help: 'old foo'});
+ parser.addArgument(['--foo'], {help: 'new foo'});
+
+ var help = parser._optionStringActions['--foo'].help;
@hpaulj

hpaulj Feb 8, 2013

Contributor

Testing shouldn't access private attributes

@shkuropat

shkuropat Feb 9, 2013

Collaborator

I agree, missed

+
+ assert.equal(help, 'new foo');
+ });
+
it("should parse negative arguments", function () {
parser = new ArgumentParser({debug: true});
parser.addArgument(['-f', '--foo']);

0 comments on commit 2141d0a

Please sign in to comment.