Comparing version 0.1.6 to 0.1.7
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: arguments' | ||
@@ -10,0 +10,0 @@ }); |
@@ -7,7 +7,7 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: choice', | ||
}); | ||
parser.addArgument(['foo'], {choices:'abc'}); | ||
parser.addArgument(['foo'], {choices: 'abc'}); | ||
@@ -14,0 +14,0 @@ parser.printHelp(); |
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: constant', | ||
@@ -15,6 +15,6 @@ }); | ||
{ | ||
action:'storeConst', | ||
dest: 'answer', | ||
help: 'store constant', | ||
constant:42 | ||
action: 'storeConst', | ||
dest: 'answer', | ||
help: 'store constant', | ||
constant: 42 | ||
} | ||
@@ -25,5 +25,5 @@ ); | ||
{ | ||
action:'appendConst', | ||
dest:'types', | ||
help: 'append constant "str" to types', | ||
action: 'appendConst', | ||
dest: 'types', | ||
help: 'append constant "str" to types', | ||
constant: 'str' | ||
@@ -35,5 +35,5 @@ } | ||
{ | ||
action:'appendConst', | ||
dest:'types', | ||
help: 'append constant "int" to types', | ||
action: 'appendConst', | ||
dest: 'types', | ||
help: 'append constant "int" to types', | ||
constant: 'int' | ||
@@ -40,0 +40,0 @@ } |
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: help', | ||
@@ -10,0 +10,0 @@ epilog: 'help epilog', |
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: nargs' | ||
@@ -14,3 +14,4 @@ }); | ||
{ | ||
help: 'foo bar', nargs:1 | ||
help: 'foo bar', | ||
nargs: 1 | ||
} | ||
@@ -21,3 +22,4 @@ ); | ||
{ | ||
help: 'bar foo', nargs:'*' | ||
help: 'bar foo', | ||
nargs: '*' | ||
} | ||
@@ -24,0 +26,0 @@ ); |
@@ -7,8 +7,8 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: prefix_chars', | ||
prefixChars:'-+' | ||
prefixChars: '-+' | ||
}); | ||
parser.addArgument(['+f', '++foo']); | ||
parser.addArgument(['++bar'], {action:'storeTrue'}); | ||
parser.addArgument(['++bar'], {action: 'storeTrue'}); | ||
@@ -23,3 +23,3 @@ parser.printHelp(); | ||
console.dir(args); | ||
args = parser.parseArgs(['++foo','2', '++bar']); | ||
args = parser.parseArgs(['++foo', '2', '++bar']); | ||
console.dir(args); |
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
version: '0.0.1', | ||
addHelp:true, | ||
addHelp: true, | ||
description: 'Argparse examples: sub-commands', | ||
@@ -13,7 +13,7 @@ }); | ||
var subparsers = parser.addSubparsers({ | ||
title:'subcommands', | ||
dest:"subcommand_name" | ||
title: 'subcommands', | ||
dest: "subcommand_name" | ||
}); | ||
var bar = subparsers.addParser('c1', {addHelp:true, help:'c1 help'}); | ||
var bar = subparsers.addParser('c1', {addHelp: true, help: 'c1 help'}); | ||
bar.addArgument( | ||
@@ -28,3 +28,3 @@ [ '-f', '--foo' ], | ||
'c2', | ||
{aliases:['co'], addHelp:true, help:'c2 help'} | ||
{aliases: ['co'], addHelp: true, help: 'c2 help'} | ||
); | ||
@@ -52,2 +52,2 @@ bar.addArgument( | ||
console.log('-----------'); | ||
parser.parseArgs(['c1','-h']); | ||
parser.parseArgs(['c1', '-h']); |
@@ -0,1 +1,8 @@ | ||
0.1.7 / 2012-10-14 | ||
------------------ | ||
* Fixed 'choices' argument parse (issue #16) | ||
* Fixed stderr output (issue #15) | ||
0.1.6 / 2012-09-09 | ||
@@ -2,0 +9,0 @@ ------------------ |
@@ -16,3 +16,2 @@ /** internal | ||
//Actions | ||
var Action = require('./action'); | ||
var ActionHelp = require('./action/help'); | ||
@@ -19,0 +18,0 @@ var ActionAppend = require('./action/append'); |
@@ -33,3 +33,2 @@ /** | ||
var util = require('util'); | ||
var _ = require('underscore'); | ||
@@ -78,20 +77,20 @@ _.str = require('underscore.string'); | ||
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; | ||
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'); | ||
} | ||
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'); | ||
} | ||
}; | ||
@@ -105,10 +104,10 @@ | ||
Action.prototype.getName = function () { | ||
if (this.optionStrings.length > 0) { | ||
return this.optionStrings.join('/'); | ||
} else if (this.metavar !== undefined && this.metavar !== $$.SUPPRESS) { | ||
return this.metavar; | ||
} else if (this.dest !== undefined && this.dest !== $$.SUPPRESS) { | ||
return this.dest; | ||
} | ||
return null; | ||
if (this.optionStrings.length > 0) { | ||
return this.optionStrings.join('/'); | ||
} else if (this.metavar !== undefined && this.metavar !== $$.SUPPRESS) { | ||
return this.metavar; | ||
} else if (this.dest !== undefined && this.dest !== $$.SUPPRESS) { | ||
return this.dest; | ||
} | ||
return null; | ||
}; | ||
@@ -122,3 +121,3 @@ | ||
Action.prototype.isOptional = function () { | ||
return !this.isPositional(); | ||
return !this.isPositional(); | ||
}; | ||
@@ -132,3 +131,3 @@ | ||
Action.prototype.isPositional = function () { | ||
return (this.optionStrings.length === 0); | ||
return (this.optionStrings.length === 0); | ||
}; | ||
@@ -152,4 +151,4 @@ | ||
**/ | ||
Action.prototype.call = function (parser, namespace, values, optionString) { | ||
throw new Error('.call() not defined');// Not Implemented error | ||
Action.prototype.call = function () { | ||
throw new Error('.call() not defined');// Not Implemented error | ||
}; |
@@ -27,12 +27,12 @@ /*:nodoc:* | ||
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); | ||
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); | ||
}; | ||
@@ -50,7 +50,7 @@ util.inherits(ActionAppend, Action); | ||
**/ | ||
ActionAppend.prototype.call = function (parser, namespace, values, optionString) { | ||
var items = [].concat(namespace[this.dest] || [], values); | ||
namespace.set(this.dest, items); | ||
ActionAppend.prototype.call = function (parser, namespace, values) { | ||
var items = [].concat(namespace[this.dest] || [], values); | ||
namespace.set(this.dest, items); | ||
}; | ||
@@ -25,5 +25,5 @@ /*:nodoc:* | ||
var ActionAppendConstant = module.exports = function ActionAppendConstant(options) { | ||
options = options || {}; | ||
options.nargs = 0; | ||
Action.call(this, options); | ||
options = options || {}; | ||
options.nargs = 0; | ||
Action.call(this, options); | ||
}; | ||
@@ -41,6 +41,6 @@ util.inherits(ActionAppendConstant, Action); | ||
**/ | ||
ActionAppendConstant.prototype.call = function (parser, namespace, values, optionString) { | ||
var items = [].concat(namespace[this.dest] || []); | ||
items.push(this.constant); | ||
namespace.set(this.dest, items); | ||
ActionAppendConstant.prototype.call = function (parser, namespace) { | ||
var items = [].concat(namespace[this.dest] || []); | ||
items.push(this.constant); | ||
namespace.set(this.dest, items); | ||
}; |
@@ -38,4 +38,4 @@ /*:nodoc:* | ||
**/ | ||
ActionCount.prototype.call = function (parser, namespace, values, optionString) { | ||
ActionCount.prototype.call = function (parser, namespace) { | ||
namespace.set(this.dest, (namespace[this.dest] || 0) + 1); | ||
}; |
@@ -29,3 +29,3 @@ /*:nodoc:* | ||
} | ||
options.dest = (options.dest !== null? options.dest: $$.SUPPRESS); | ||
options.dest = (options.dest !== null ? options.dest: $$.SUPPRESS); | ||
options.nargs = 0; | ||
@@ -46,5 +46,5 @@ Action.call(this, options); | ||
**/ | ||
ActionHelp.prototype.call = function (parser, namespace, values, optionString) { | ||
ActionHelp.prototype.call = function (parser) { | ||
parser.printHelp(); | ||
parser.exit(); | ||
}; |
@@ -48,4 +48,4 @@ /*:nodoc:* | ||
**/ | ||
ActionStore.prototype.call = function (parser, namespace, values, optionString) { | ||
ActionStore.prototype.call = function (parser, namespace, values) { | ||
namespace.set(this.dest, values); | ||
}; |
@@ -23,5 +23,5 @@ /*:nodoc:* | ||
var ActionStoreConstant = module.exports = function ActionStoreConstant(options) { | ||
options = options || {}; | ||
options.nargs = 0; | ||
Action.call(this, options); | ||
options = options || {}; | ||
options.nargs = 0; | ||
Action.call(this, options); | ||
}; | ||
@@ -39,4 +39,4 @@ util.inherits(ActionStoreConstant, Action); | ||
**/ | ||
ActionStoreConstant.prototype.call = function (parser, namespace, values, optionString) { | ||
namespace.set(this.dest, this.constant); | ||
ActionStoreConstant.prototype.call = function (parser, namespace) { | ||
namespace.set(this.dest, this.constant); | ||
}; |
@@ -22,7 +22,7 @@ /*:nodoc:* | ||
var ActionStoreFalse = module.exports = function ActionStoreFalse(options) { | ||
options = options || {}; | ||
options.constant = false; | ||
options.defaultValue = options.defaultValue !== null ? options.defaultValue: true; | ||
ActionStoreConstant.call(this, options); | ||
options = options || {}; | ||
options.constant = false; | ||
options.defaultValue = options.defaultValue !== null ? options.defaultValue: true; | ||
ActionStoreConstant.call(this, options); | ||
}; | ||
util.inherits(ActionStoreFalse, ActionStoreConstant); |
@@ -21,7 +21,7 @@ /*:nodoc:* | ||
var ActionStoreTrue = module.exports = function ActionStoreTrue(options) { | ||
options = options || {}; | ||
options.constant = true; | ||
options.defaultValue = options.defaultValue !== null ? options.defaultValue: false; | ||
ActionStoreConstant.call(this, options); | ||
options = options || {}; | ||
options.constant = true; | ||
options.defaultValue = options.defaultValue !== null ? options.defaultValue: false; | ||
ActionStoreConstant.call(this, options); | ||
}; | ||
util.inherits(ActionStoreTrue, ActionStoreConstant); |
@@ -29,7 +29,7 @@ /** internal | ||
**/ | ||
var ChoicesPseudoAction = function(name, help){ | ||
var ChoicesPseudoAction = function (name, help) { | ||
var options = { | ||
optionStrings:[], | ||
dest:name, | ||
help:help | ||
optionStrings: [], | ||
dest: name, | ||
help: help | ||
}; | ||
@@ -39,3 +39,3 @@ | ||
}; | ||
util.inherits(ChoicesPseudoAction , Action); | ||
util.inherits(ChoicesPseudoAction, Action); | ||
@@ -105,3 +105,3 @@ /** | ||
// make parser available under aliases also | ||
aliases.forEach(function(alias) { | ||
aliases.forEach(function (alias) { | ||
self._nameParserMap[alias] = parser; | ||
@@ -126,3 +126,3 @@ }); | ||
**/ | ||
ActionSubparsers.prototype.call = function (parser, namespace, values, optionString) { | ||
ActionSubparsers.prototype.call = function (parser, namespace, values) { | ||
var parserName = values[0]; | ||
@@ -129,0 +129,0 @@ var argStrings = values.slice(1); |
@@ -23,9 +23,9 @@ /*:nodoc:* | ||
**/ | ||
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); | ||
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); | ||
}; | ||
@@ -43,7 +43,7 @@ util.inherits(ActionVersion, Action); | ||
**/ | ||
ActionVersion.prototype.call = function (parser, namespace, values, optionString) { | ||
var version = this.version || parser.version; | ||
var formatter = parser._getFormatter(); | ||
formatter.addText(version); | ||
parser.exit(0, formatter.formatHelp()); | ||
ActionVersion.prototype.call = function (parser) { | ||
var version = this.version || parser.version; | ||
var formatter = parser._getFormatter(); | ||
formatter.addText(version); | ||
parser.exit(0, formatter.formatHelp()); | ||
}; | ||
@@ -50,0 +50,0 @@ |
@@ -11,3 +11,2 @@ /** | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
var Path = require('path'); | ||
@@ -199,3 +198,3 @@ | ||
ArgumentParser.prototype._getOptionalActions = function () { | ||
return this._actions.filter(function (action, actionIndex) { | ||
return this._actions.filter(function (action) { | ||
return action.isOptional(); | ||
@@ -206,3 +205,3 @@ }); | ||
ArgumentParser.prototype._getPositionalActions = function () { | ||
return this._actions.filter(function (action, actionIndex) { | ||
return this._actions.filter(function (action) { | ||
return action.isPositional(); | ||
@@ -261,3 +260,3 @@ }); | ||
self._actions.forEach(function(action) { | ||
self._actions.forEach(function (action) { | ||
if (action.dest !== $$.SUPPRESS) { | ||
@@ -267,3 +266,3 @@ if (_.indexOf(namespace, action.dest) === -1) { | ||
var defaultValue = action.defaultValue; | ||
if (_.isString(action.defaultValue)){ | ||
if (_.isString(action.defaultValue)) { | ||
defaultValue = self._getValue(action, defaultValue); | ||
@@ -277,3 +276,3 @@ } | ||
_.keys(self._defaults).forEach(function(dest){ | ||
_.keys(self._defaults).forEach(function (dest) { | ||
namespace[dest] = self._defaults[dest]; | ||
@@ -319,3 +318,3 @@ }); | ||
argStrings.forEach(function (argString, argStringIndex) { | ||
if (argString === '--'){ | ||
if (argString === '--') { | ||
argStringPatternParts.push('-'); | ||
@@ -348,3 +347,3 @@ while (argStringIndex < argStrings.length) { | ||
function takeAction(action, argumentStrings, optionString){ | ||
function takeAction(action, argumentStrings, optionString) { | ||
seenActions.push(action); | ||
@@ -356,8 +355,8 @@ var argumentValues = self._getValues(action, argumentStrings); | ||
// value don't really count as "present" | ||
if (argumentValues !== action.default){ | ||
if (argumentValues !== action.default) { | ||
seenNonDefaultActions.push(action); | ||
if (!!actionConflicts[action]) { | ||
for (var i=0; i < actionConflicts[action].length; i++) { | ||
for (var i = 0; i < actionConflicts[action].length; i++) { | ||
var actionConflict = actionConflicts[action][i]; | ||
if (seenNonDefaultActions.indexOf(actionConflict)>0){ | ||
if (seenNonDefaultActions.indexOf(actionConflict) > 0) { | ||
throw argumentErrorHelper( | ||
@@ -380,3 +379,3 @@ action, | ||
function consumeOptional(startIndex){ | ||
function consumeOptional(startIndex) { | ||
// get the optional identified at this index | ||
@@ -394,8 +393,8 @@ var optionTuple = optionStringIndices[startIndex]; | ||
while(true){ | ||
if (!action){ | ||
while (true) { | ||
if (!action) { | ||
extras.push(argStrings[startIndex]); | ||
return startIndex + 1; | ||
} | ||
if (!!explicitArg){ | ||
if (!!explicitArg) { | ||
argCount = self._matchArgument(action, 'A'); | ||
@@ -407,3 +406,3 @@ | ||
var chars = self.prefixChars; | ||
if (argCount === 0 && chars.indexOf(optionString[1]) < 0){ | ||
if (argCount === 0 && chars.indexOf(optionString[1]) < 0) { | ||
actionTuples.push([action, [], optionString]); | ||
@@ -414,7 +413,7 @@ optionString = optionString[0] + explicitArg[0]; | ||
if (_.keys(optionalsMap).indexOf(optionString) >= 0){ | ||
if (_.keys(optionalsMap).indexOf(optionString) >= 0) { | ||
action = optionalsMap[optionString]; | ||
explicitArg = newExplicitArg; | ||
} | ||
else{ | ||
else { | ||
var msg = 'ignored explicit argument %r'; | ||
@@ -426,3 +425,3 @@ throw argumentErrorHelper(action, msg); | ||
// successfully matched the option; exit the loop | ||
else if (argCount === 1){ | ||
else if (argCount === 1) { | ||
stop = startIndex + 1; | ||
@@ -435,3 +434,3 @@ args = [explicitArg]; | ||
// explicit argument | ||
else{ | ||
else { | ||
var message = 'ignored explicit argument %r'; | ||
@@ -444,3 +443,3 @@ throw argumentErrorHelper(action, _.str.sprintf(message, explicitArg)); | ||
// if successful, exit the loop | ||
else{ | ||
else { | ||
@@ -467,3 +466,3 @@ start = startIndex + 1; | ||
} | ||
for (var i=0; i < actionTuples.length; i++) { | ||
for (var i = 0; i < actionTuples.length; i++) { | ||
takeAction.apply(self, actionTuples[i]); | ||
@@ -478,3 +477,3 @@ } | ||
function consumePositionals(startIndex){ | ||
function consumePositionals(startIndex) { | ||
// match as many Positionals as possible | ||
@@ -487,3 +486,3 @@ var selectedPattern = argStringsPattern.substr(startIndex); | ||
if (argCounts.length > 0) { | ||
_.zip(positionals, argCounts).forEach(function(item) { | ||
_.zip(positionals, argCounts).forEach(function (item) { | ||
var action = item[0]; | ||
@@ -510,3 +509,3 @@ var argCount = item[1]; | ||
var maxOptionStringIndex = -1; | ||
if (!!optionStringIndices){ | ||
if (!!optionStringIndices) { | ||
for (position in optionStringIndices) { | ||
@@ -519,6 +518,6 @@ maxOptionStringIndex = Math.max(maxOptionStringIndex, parseInt(position, 10)); | ||
while (startIndex <= maxOptionStringIndex){ | ||
while (startIndex <= maxOptionStringIndex) { | ||
// consume any Positionals preceding the next option | ||
nextOptionStringIndex = null; | ||
for (position in optionStringIndices){ | ||
for (position in optionStringIndices) { | ||
position = parseInt(position, 10); | ||
@@ -535,11 +534,11 @@ if (position >= startIndex) { | ||
if (startIndex !== nextOptionStringIndex){ | ||
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){ | ||
if (positionalsEndIndex > startIndex) { | ||
startIndex = positionalsEndIndex; | ||
continue; | ||
} | ||
else{ | ||
else { | ||
startIndex = positionalsEndIndex; | ||
@@ -551,3 +550,3 @@ } | ||
// at the index of an option string, there were extra arguments | ||
if (!optionStringIndices[startIndex]){ | ||
if (!optionStringIndices[startIndex]) { | ||
var strings = argStrings.slice(startIndex, nextOptionStringIndex); | ||
@@ -569,3 +568,3 @@ extras = extras.concat(strings); | ||
// arg strings supplied. | ||
if (positionals.length > 0){ | ||
if (positionals.length > 0) { | ||
self.error('too few arguments'); | ||
@@ -575,5 +574,5 @@ } | ||
// make sure all required actions were present | ||
self._actions.forEach(function(action){ | ||
if (action.required){ | ||
if (_.indexOf(seenActions, action) < 0){ | ||
self._actions.forEach(function (action) { | ||
if (action.required) { | ||
if (_.indexOf(seenActions, action) < 0) { | ||
self.error(_.str.sprintf('Argument "%(name)s" is required', {name: action.getName()})); | ||
@@ -601,14 +600,14 @@ } | ||
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 %(count)s argument(s)'; | ||
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 %(count)s argument(s)'; | ||
} | ||
@@ -793,30 +792,30 @@ | ||
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) + '-*)'; | ||
// 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) + '-*)'; | ||
} | ||
@@ -938,14 +937,22 @@ | ||
var choices = action.choices; | ||
if (!!choices && | ||
!choices[value] && | ||
(_.isFunction(choices.indexOf) && choices.indexOf(value) === -1)) { | ||
if (!_.isString(choices)) { | ||
if (_.isObject(choices)) { | ||
choices = _.keys(action.choices).join(', '); | ||
} | ||
else { | ||
choices = action.choices.join(', '); | ||
} | ||
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 = _.str.sprintf( | ||
@@ -997,3 +1004,3 @@ 'Invalid choice: %(value)s (choose from [%(choices)s])', | ||
// positionals, optionals and user-defined groups | ||
this._actionGroups.forEach(function (actionGroup, actionIndex) { | ||
this._actionGroups.forEach(function (actionGroup) { | ||
formatter.startSection(actionGroup.title); | ||
@@ -1044,3 +1051,3 @@ formatter.addText(actionGroup.description); | ||
**/ | ||
ArgumentParser.prototype.printHelp = function() { | ||
ArgumentParser.prototype.printHelp = function () { | ||
this._printMessage(this.formatHelp()); | ||
@@ -1093,2 +1100,3 @@ }; | ||
ArgumentParser.prototype.error = function (err) { | ||
var message; | ||
if (err instanceof Error) { | ||
@@ -1098,8 +1106,10 @@ if (this.debug === true) { | ||
} | ||
err = err.err; | ||
message = err.message; | ||
} | ||
else { | ||
message = err; | ||
} | ||
var msg = _.str.sprintf( | ||
'%(prog)s: error: %(err)s', | ||
{prog: this.prog, err: err}) + $$.EOL; | ||
{prog: this.prog, err: message}) + $$.EOL; | ||
@@ -1106,0 +1116,0 @@ if (this.debug === true) { |
@@ -32,2 +32,6 @@ 'use strict'; | ||
var argumentName = null; | ||
var format; | ||
var errMessage; | ||
var err; | ||
if (argument.getName) { | ||
@@ -38,11 +42,11 @@ argumentName = argument.getName(); | ||
} | ||
var format = !argumentName ? '%(message)s': 'argument "%(argumentName)s": %(message)s'; | ||
format = !argumentName ? '%(message)s': 'argument "%(argumentName)s": %(message)s'; | ||
var errMessage = _.str.sprintf(format, { | ||
errMessage = _.str.sprintf(format, { | ||
message: message, | ||
argumentName: argumentName | ||
}); | ||
var err = new TypeError(errMessage); | ||
err = new TypeError(errMessage); | ||
err.code = ERR_CODE; | ||
return err; | ||
}; |
@@ -16,3 +16,2 @@ /** | ||
var util = require('util'); | ||
var _ = require('underscore'); | ||
@@ -317,3 +316,3 @@ _.str = require('underscore.string'); | ||
if (!prefix && !_.isString(prefix)) { | ||
prefix = 'usage: '; | ||
prefix = 'usage: '; | ||
} | ||
@@ -331,7 +330,7 @@ | ||
} else if (!usage && actions.length === 0) { | ||
usage = _.str.sprintf('%(prog)s' , {prog: this._prog}); | ||
usage = _.str.sprintf('%(prog)s', {prog: this._prog}); | ||
// if optionals and positionals are available, calculate usage | ||
} else if (!usage) { | ||
var prog = _.str.sprintf('%(prog)s' , {prog: this._prog}); | ||
var prog = _.str.sprintf('%(prog)s', {prog: this._prog}); | ||
var optionals = []; | ||
@@ -614,3 +613,3 @@ var positionals = []; | ||
// or add a newline if the description doesn't end with one | ||
} else if (actionHeader.charAt(actionHeader.length-1) !== $$.EOL) { | ||
} else if (actionHeader.charAt(actionHeader.length - 1) !== $$.EOL) { | ||
parts.push($$.EOL); | ||
@@ -663,11 +662,12 @@ } | ||
var choices = action.choices; | ||
if (!_.isString(choices)) { | ||
if (_.isObject(choices)){ | ||
choices = _.keys(choices).join(','); | ||
} | ||
else | ||
{ | ||
choices = choices.join(','); | ||
} | ||
if (_.isString(choices)) { | ||
choices = choices.split('').join(', '); | ||
} else if (_.isArray(choices)) { | ||
choices = choices.join(','); | ||
} | ||
else | ||
{ | ||
choices = _.keys(choices).join(','); | ||
} | ||
result = '{' + choices + '}'; | ||
@@ -677,2 +677,3 @@ } else { | ||
} | ||
return function (size) { | ||
@@ -698,29 +699,29 @@ if (Array.isArray(result)) { | ||
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(' '); | ||
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(' '); | ||
} | ||
@@ -746,9 +747,11 @@ return result; | ||
var choices = params.choices; | ||
if (!!choices && !_.isString(choices)) { | ||
if (_.isObject(choices)){ | ||
params.choices = _.keys(choices).join(', '); | ||
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 = choices.join(', '); | ||
params.choices = _.keys(params.choices).join(', '); | ||
} | ||
@@ -755,0 +758,0 @@ } |
{ | ||
"name" : "argparse", | ||
"description" : "Very powerful CLI arguments parser. Native port of argparse - python's options parsing library", | ||
"version" : "0.1.6", | ||
"version" : "0.1.7", | ||
"keywords" : ["cli", "parser", "argparse", "option", "args"], | ||
@@ -6,0 +6,0 @@ "homepage" : "https://github.com/nodeca/argparse", |
@@ -13,3 +13,3 @@ /*global describe, it, before, after, beforeEach, afterEach*/ | ||
var args; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
parser = new ArgumentParser({debug: true}); | ||
@@ -28,3 +28,3 @@ parser.addArgument([ '-f', '--foo' ]); | ||
it("should parse argument in long form", function() { | ||
it("should parse argument in long form", function () { | ||
args = parser.parseArgs('--foo 1'.split(' ')); | ||
@@ -36,3 +36,3 @@ assert.equal(args.foo, 1); | ||
it("should parse multiple arguments", function(){ | ||
it("should parse multiple arguments", function () { | ||
parser.addArgument(['--bar' ]); | ||
@@ -44,13 +44,19 @@ args = parser.parseArgs('--foo 5 --bar 6'.split(' ')); | ||
it("should check argument type", function(){ | ||
parser.addArgument(['--bar' ], {type:'int'}); | ||
assert.throws(function () { parser.parseArgs('--bar bar'.split(' '));}); | ||
assert.doesNotThrow(function () { parser.parseArgs('--bar 1'.split(' '));}); | ||
it("should check argument type", function () { | ||
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(){ | ||
assert.doesNotThrow(function () {parser.parseArgs([]); }); | ||
it("should not drop down with empty args (without positional arguments)", function () { | ||
assert.doesNotThrow(function () { | ||
parser.parseArgs([]); | ||
}); | ||
}); | ||
it("should drop down with empty args (positional arguments)", function(){ | ||
it("should drop down with empty args (positional arguments)", function () { | ||
parser.addArgument([ 'baz']); | ||
@@ -63,3 +69,3 @@ assert.throws( | ||
it("should support pseudo-argument", function() { | ||
it("should support pseudo-argument", function () { | ||
parser.addArgument([ 'bar' ], { nargs: '+' }); | ||
@@ -71,3 +77,3 @@ args = parser.parseArgs([ '-f', 'foo', '--', '-f', 'bar' ]); | ||
it("should support #setDefaults", function() { | ||
it("should support #setDefaults", function () { | ||
parser.setDefaults({bar: 1}); | ||
@@ -78,26 +84,26 @@ args = parser.parseArgs([]); | ||
it("should throw TypeError with conflicting options", function() { | ||
it("should throw TypeError with conflicting options", function () { | ||
assert.throws( | ||
function () { | ||
parser.addArgument(['-f']); | ||
}, | ||
/Conflicting option string/ | ||
function () { | ||
parser.addArgument(['-f']); | ||
}, | ||
/Conflicting option string/ | ||
); | ||
assert.throws( | ||
function () { | ||
parser.addArgument(['--foo']); | ||
}, | ||
/Conflicting option string/ | ||
function () { | ||
parser.addArgument(['--foo']); | ||
}, | ||
/Conflicting option string/ | ||
); | ||
assert.throws( | ||
function () { | ||
parser.addArgument(['-f', '--flame']); | ||
}, | ||
/Conflicting option string/ | ||
function () { | ||
parser.addArgument(['-f', '--flame']); | ||
}, | ||
/Conflicting option string/ | ||
); | ||
assert.throws( | ||
function () { | ||
parser.addArgument(['-m', '--foo']); | ||
}, | ||
/Conflicting option string/ | ||
function () { | ||
parser.addArgument(['-m', '--foo']); | ||
}, | ||
/Conflicting option string/ | ||
); | ||
@@ -104,0 +110,0 @@ }); |
@@ -17,9 +17,9 @@ /*global describe, it, before, after, beforeEach, afterEach*/ | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
parser = new ArgumentParser({debug: true}); | ||
var subparsers = parser.addSubparsers({ | ||
title:'subcommands', | ||
dest:"subcommand_name" | ||
title: 'subcommands', | ||
dest: 'subcommand_name' | ||
}); | ||
c1 = subparsers.addParser('c1', {aliases:['co']}); | ||
c1 = subparsers.addParser('c1', {aliases: ['co']}); | ||
c1.addArgument([ '-f', '--foo' ], {}); | ||
@@ -31,3 +31,3 @@ c1.addArgument([ '-b', '--bar' ], {}); | ||
it("should store command name", function(){ | ||
it("should store command name", function () { | ||
args = parser.parseArgs('c1 --foo 5'.split(' ')); | ||
@@ -37,3 +37,3 @@ assert.equal(args.subcommand_name, 'c1'); | ||
it("should store command arguments", function(){ | ||
it("should store command arguments", function () { | ||
args = parser.parseArgs('c1 --foo 5 -b4'.split(' ')); | ||
@@ -44,3 +44,3 @@ assert.equal(args.foo, 5); | ||
it("should have same behavior for alias and original command", function(){ | ||
it("should have same behavior for alias and original command", function () { | ||
args = parser.parseArgs('c1 --foo 5 -b4'.split(' ')); | ||
@@ -52,17 +52,25 @@ var aliasArgs = parser.parseArgs('co --foo 5 -b4'.split(' ')); | ||
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 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 'unknown parser' error if parse unrecognized command", function(){ | ||
it("should drop down with 'Invalid choice' error if parse unrecognized command", function () { | ||
assert.throws( | ||
function () {parser.parseArgs('command --baz 1'.split(' ')); }, | ||
/Unknown parser/ | ||
/Invalid choice:/ | ||
); | ||
}); | ||
it("should drop down with empty args ('too few arguments' error)", function(){ | ||
it("should drop down with empty args ('too few arguments' error)", function () { | ||
assert.throws( | ||
@@ -74,3 +82,3 @@ function () {parser.parseArgs([]); }, | ||
it("should support #setDefaults", function() { | ||
it("should support #setDefaults", function () { | ||
c1.setDefaults({spam: 1}); | ||
@@ -77,0 +85,0 @@ c2.setDefaults({eggs: 2}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
121839
40
3190
1