Permalink
Browse files

replaced most of `sprintf` with more compact `format`

  • Loading branch information...
1 parent 02df5d8 commit 1426b3e3d689a1c8a430009f2ace33e22ec65aca @puzrin puzrin committed Feb 16, 2013
Showing with 52 additions and 80 deletions.
  1. +0 −2 lib/action.js
  2. +8 −9 lib/action/subparsers.js
  3. +14 −21 lib/action_container.js
  4. +11 −11 lib/argument/error.js
  5. +17 −35 lib/argument_parser.js
  6. +2 −2 lib/help/formatter.js
View
2 lib/action.js
@@ -30,8 +30,6 @@
'use strict';
-var _ = require('underscore');
-_.str = require('underscore.string');
// Constants
var $$ = require('./const');
View
17 lib/action/subparsers.js
@@ -7,9 +7,10 @@
**/
'use strict';
-var util = require('util');
-var _ = require('underscore');
-_.str = require('underscore.string');
+var util = require('util');
+var format = require('util').format;
+var _ = require('underscore');
+
var Action = require('../action');
@@ -133,12 +134,10 @@ ActionSubparsers.prototype.call = function (parser, namespace, values) {
if (!!this._nameParserMap[parserName]) {
parser = this._nameParserMap[parserName];
} else {
- throw argumentErrorHelper(_.str.sprintf(
- 'Unknown parser "%(name)s" (choices: [%(choices)s]).',
- {
- name: parserName,
- choices: _.keys(this._nameParserMap).join(', ')
- }
+ throw argumentErrorHelper(format(
+ 'Unknown parser "%s" (choices: [%s]).',
+ parserName,
+ _.keys(this._nameParserMap).join(', ')
));
}
View
35 lib/action_container.js
@@ -6,7 +6,9 @@
'use strict';
-var _ = require('underscore');
+var format = require('util').format;
+var _ = require('underscore');
+
_.str = require('underscore.string');
// Constants
@@ -207,20 +209,14 @@ ActionContainer.prototype.addArgument = function (args, options) {
// create the action object, and add it to the parser
var ActionClass = this._popActionClass(options);
if (! _.isFunction(ActionClass)) {
- throw new Error(_.str.sprintf(
- 'Unknown action "%(action)s".',
- {action: 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(_.str.sprintf(
- '"%(function)s" is not callable',
- {'function': typeFunction}
- ));
+ throw new Error(format('"%s" is not callable', typeFunction));
}
return this._addAction(action);
@@ -290,7 +286,7 @@ ActionContainer.prototype._addContainerActions = function (container) {
var titleGroupMap = {};
this._actionGroups.forEach(function (group) {
if (titleGroupMap[group.title]) {
- throw new Error(_.str.sprintf('Cannot merge actions - two groups are named "%(group.title)s".', group));
+ throw new Error(format('Cannot merge actions - two groups are named "%s".', group.title));
}
titleGroupMap[group.title] = group;
});
@@ -377,10 +373,10 @@ ActionContainer.prototype._getOptional = function (args, options) {
args.forEach(function (optionString) {
// error on strings that don't start with an appropriate prefix
if (prefixChars.indexOf(optionString[0]) < 0) {
- throw new Error(_.str.sprintf('Invalid option string "%(option)s": must start with a "%(prefix)s".', {
- option: optionString,
- prefix: prefixChars
- }));
+ throw new Error(format('Invalid option string "%s": must start with a "%s".',
+ optionString,
+ prefixChars
+ ));
}
// strings starting with two prefix characters are long options
@@ -399,10 +395,9 @@ ActionContainer.prototype._getOptional = function (args, options) {
dest = _.str.strip(optionStringDest, this.prefixChars);
if (dest.length === 0) {
- throw new Error(_.str.sprintf(
- 'dest= is required for options like "%(option)s"',
- {option: optionStrings.join(', ')}
- ));
+ throw new Error(
+ format('dest= is required for options like "%s"', optionStrings.join(', '))
+ );
}
dest = dest.replace('-', '_');
}
@@ -460,9 +455,7 @@ ActionContainer.prototype._handleConflictError = function (action, conflOptional
conflicts = conflicts.join(', ');
throw argumentErrorHelper(
action,
- _.str.sprintf('Conflicting option string(s): %(conflict)s', {
- conflict: conflicts
- })
+ format('Conflicting option string(s): %s', conflicts)
);
};
View
22 lib/argument/error.js
@@ -1,7 +1,9 @@
'use strict';
-var _ = require('underscore');
-_.str = require('underscore.string');
+
+var format = require('util').format;
+
+
var ERR_CODE = 'ARGError';
/*:nodoc:*
@@ -20,16 +22,13 @@ var ERR_CODE = 'ARGError';
* if (conflictOptionals.length > 0) {
* throw argumentErrorHelper(
* action,
- * _.str.sprintf('Conflicting option string(s): %(conflict)s', {
- * conflict: conflictOptionals.join(', ')
- * })
+ * format('Conflicting option string(s): %s', conflictOptionals.join(', '))
* );
* }
*
**/
module.exports = function (argument, message) {
var argumentName = null;
- var format;
var errMessage;
var err;
@@ -38,12 +37,13 @@ module.exports = function (argument, message) {
} else {
argumentName = '' + argument;
}
- format = !argumentName ? '%(message)s' : 'argument "%(argumentName)s": %(message)s';
- errMessage = _.str.sprintf(format, {
- message: message,
- argumentName: argumentName
- });
+ if (!argumentName) {
+ errMessage = message;
+ } else {
+ errMessage = format('argument "%s": %s', argumentName, message);
+ }
+
err = new TypeError(errMessage);
err.code = ERR_CODE;
return err;
View
52 lib/argument_parser.js
@@ -7,8 +7,9 @@
**/
'use strict';
-var util = require('util');
-var Path = require('path');
+var util = require('util');
+var format = require('util').format;
+var Path = require('path');
var _ = require('underscore');
_.str = require('underscore.string');
@@ -233,9 +234,7 @@ ArgumentParser.prototype.parseArgs = function (args, namespace) {
argv = result[1];
if (argv && argv.length > 0) {
this.error(
- _.str.sprintf('Unrecognized arguments: %(args)s.', {
- args: argv.join(' ')
- })
+ format('Unrecognized arguments: %s.', argv.join(' '))
);
}
return args;
@@ -381,10 +380,7 @@ ArgumentParser.prototype._parseKnownArgs = function (argStrings, namespace) {
if (seenNonDefaultActions.indexOf(actionConflict) >= 0) {
throw argumentErrorHelper(
action,
- _.str.sprintf(
- 'Not allowed with argument "%(argument)s".',
- {argument: actionConflict.getName()}
- )
+ format('Not allowed with argument "%s".', actionConflict.getName())
);
}
});
@@ -580,7 +576,7 @@ ArgumentParser.prototype._parseKnownArgs = function (argStrings, namespace) {
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()}));
+ self.error(format('Argument "%s" is required', action.getName()));
}
}
});
@@ -668,13 +664,13 @@ ArgumentParser.prototype._matchArgument = function (action, regexpArgStrings) {
message = 'Expected at least one argument.';
break;
default:
- message = 'Expected %(count)s argument(s)';
+ message = 'Expected %s argument(s)';
}
throw argumentErrorHelper(
action,
- _.str.sprintf(message, {count: action.nargs}
- ));
+ format(message, action.nargs)
+ );
}
// return the number of arguments matched
return matches[1].length;
@@ -758,9 +754,9 @@ ArgumentParser.prototype._parseOptional = function (argString) {
var optionStrings = optionTuples.map(function (optionTuple) {
return optionTuple[1];
});
- this.error(_.str.sprintf(
- 'Ambiguous option: "%(argument)s" could match %(values)s.',
- {argument: argString, values: optionStrings.join(', ')}
+ 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
@@ -834,10 +830,7 @@ ArgumentParser.prototype._getOptionTuples = function (optionString) {
// shouldn't ever get here
} else {
- throw new Error(_.str.sprintf(
- 'Unexpected option string: %(argument)s.',
- {argument: optionString}
- ));
+ throw new Error(format('Unexpected option string: %s.', optionString));
}
// return the collected option tuples
return result;
@@ -963,10 +956,7 @@ ArgumentParser.prototype._getValue = function (action, argString) {
var typeFunction = this._registryGet('type', action.type, action.type);
if (!_.isFunction(typeFunction)) {
- var message = _.str.sprintf(
- '%(callback)s is not callable',
- {callback: typeFunction}
- );
+ var message = format('%s is not callable', typeFunction);
throw argumentErrorHelper(action, message);
}
@@ -985,10 +975,7 @@ ArgumentParser.prototype._getValue = function (action, argString) {
} else {
name = action.type.name || action.type.displayName || '<function>';
}
- var msg = _.str.sprintf('Invalid %(type)s value: %(value)s', {
- type: name,
- value: argString
- });
+ var msg = format('Invalid %s value: %s', name, argString);
if (name === '<function>') {msg += '\n' + e.message; }
throw argumentErrorHelper(action, msg);
}
@@ -1019,10 +1006,7 @@ ArgumentParser.prototype._checkValue = function (action, value) {
else {
choices = _.keys(choices).join(', ');
}
- var message = _.str.sprintf(
- 'Invalid choice: %(value)s (choose from [%(choices)s])',
- {value: value, choices: choices}
- );
+ var message = format('Invalid choice: %s (choose from [%s])', value, choices);
throw argumentErrorHelper(action, message);
}
};
@@ -1169,9 +1153,7 @@ ArgumentParser.prototype.error = function (err) {
else {
message = err;
}
- var msg = _.str.sprintf(
- '%(prog)s: error: %(err)s',
- {prog: this.prog, err: message}) + $$.EOL;
+ var msg = format('%s: error: %s', this.prog, message) + $$.EOL;
if (this.debug === true) {
throw new Error(msg);
View
4 lib/help/formatter.js
@@ -325,11 +325,11 @@ HelpFormatter.prototype._formatUsage = function (usage, actions, groups, prefix)
// if no optionals or positionals are available, usage is just prog
} else if (!usage && actions.length === 0) {
- usage = _.str.sprintf('%(prog)s', {prog: this._prog});
+ usage = this._prog;
// if optionals and positionals are available, calculate usage
} else if (!usage) {
- var prog = _.str.sprintf('%(prog)s', {prog: this._prog});
+ var prog = this._prog;
var optionals = [];
var positionals = [];
var actionUsage;

0 comments on commit 1426b3e

Please sign in to comment.