package
Implement a command line interface that calls functions within your application.
npm install command-line-callbackGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Create git-style command structure that calls functions within your application. Defined commands can also produce help output when the --help flag is used and can accept environment variables and environment files.
Estimated unpacked size across 7 packages.
118.9 kB
Includes dependencies.
index.js
var Command = require('command-line-callback');
// define the command configuration
var configuration = {
brief: 'Assign a random value to a person.',
options: {
fullName: {
alias: 'f',
description: "A person's full name.",
env: 'FULL_NAME',
type: String,
validate: function(value) {
return value.length >= 0;
},
required: true
},
min: {
defaultValue: 0,
description: 'The minimum assignable value.',
type: Number,
transform: function(value) {
return Math.round(value);
}
},
max: {
alias: 'x',
description: 'The maximum assignable value.',
type: Number,
required: true
}
}
};
// define the command callback
function assign(options) {
var diff = options.max - options.min;
var value = options.min + (Math.random() * diff);
console.log(options.fullName + ' assigned value ' + value);
}
// define the command
Command.define('assign', assign, configuration);
// evaluate the command line arguments and call the associated command
Command.evaluate();Execute the Command from the Command Line
$ node index.js assign --full-name 'Bob Smith' --min 5 -x 10Optionally, because just one command is defined it will be called if no command is specified.
$ node index.js -f "Bob Smith" --max 10Get Command Line Help
$ node index.js --helpGet or set the name of the default command. If the application is stared from the command line without a command, and multiple commands are defined, then the application will attempt to execute the default command. The default command defaults to default.
Example
var command = require('command-line-callback');
command.defaultCommand = 'foo';
command.define('foo', function(config) { ... }, { ... });
command.define('bar', function(config) { ... }, { ... });
command.evaluate();Get a normalized configuration for the specified command. The configuration returned from this function can be passed directly into the callback function that was defined for this command.
Returns a normalized and validated configuration object.
Define a git-style command, the function it should call, and the configuration options that link the command line to the callback.
Parameters
Returns undefined.
Evaluate input arguments to determine which command to run and with what options. The result will be logged to the console.
Parameters
Returns undefined.
Execute a defined command from within your code.
Parameters
Returns whatever the command returns.
Get the help string for the command specified.
Parameters
Returns a string with the command's help.
Get the names of all defined commands.
Returns a string with the command's help.
The parser is the tool that is used to convert string values into primitives or objects. This tool is uses by the command line args tool to convert strings from the command line into usable values.
Define a parser that converts the a string into its value equivalent.
Parameters
Returns undefined.
Example
var command = require('command-line-callback');
command.parser.define(Boolean, 'true', function(value, factory) {
// Note: factory === Boolean
if (value === 'true') return factory(true);
if (value === 'false') return factory(false);
if (!isNaN(value)) return factory(parseInt(value));
return factory(!!value);
});Check if the parser has been defined.
Parameters
Returns a boolean.
Parse and string value and get back its instance value.
Parameters
Returns the value that the factory produces.
Example
var value = command.parser.parse(Boolean, ''); // true^3.0.6
3.0.9
^2.1.1
2.1.2