vorpal-setorprint
v1.0.1
Published
Quickly create vorpal commands which set a value, or print it if no argument was given.
Downloads
18
Readme
Vorpal - SetOrPrint
A Vorpal.js extension for quickly creating commands which set a value, or print it if no argument was given. Think instant getters/setters for the console.
Installation
npm install vorpal-setorprint
npm install vorpalGetting Started
const vorpal = (require('vorpal'))();
const vorpalSOP = require('vorpal-setorprint');
vorpal.use(vorpalSOP)
.delimiter('vorpal-setorprint demo $')
.show();
const sop = vorpal.sop;
const options = {foo: 'bar'};
sop.command('foo', options);$ node ./myapp.js
vorpal-setorprint demo $ help foo
Usage: foo [options] [foo]
set or print foo
Options:
--help output usage information
vorpal-setorprint demo $ foo
bar
vorpal-setorprint demo $ foo qux
vorpal-setorprint demo $ foo
qux
vorpal-setorprint demo $Usage
Adds a sop object to vorpal, which stores options and provides a command function.
command(key, obj[, options]): Adds a command to vorpal which either sets or prints a specific value. Returns the command object, just like vorpal.command does. This allows convenient chaining, e.g.
sop.command(key, obj)
.alias('foobar')
.description('I like trains.');obj,key: When the added command is called without arguments,obj[key]is printed to the user. When the command is called with an argument,obj[key]is set to this argument.keyis also the name of the added command.options: The following values of the option object are used:validate: an optional function to validate and parse the input. Receivesargs[key]and should return either the value forobj[key], or null, in which caseobj[key]remains unchanged.print: The function to be called when the added command is run without arguments. Defaults tosop.options.print.failedValidation: A function which is called whenvalidatereturns null. Defaults tosop.options.failedValidation.passedValidation: A function which is called whenvalidatedoes not return null. Defaults tosop.options.passedValidation.
See the example here for a simple usage of all the options.
Options
The following options passed by vorpal.use(vorpalSOP, options) are used:
print: a function to be called with the value to print if no argument was given a sop-command. The default function tries to use vorpal-log'slogger.info(arg)and falls back to eithervorpal.activeCommand.logorvorpal.log.describe: a function to be called with the key of each added command. The return value is used as the description for the help entry. Defaults toreturn "set or print #{key}". This value can simply be overridden by calling vorpal'sdescriptionmethod on the command object returned bysop.command.failedValidation: a function to be called withkey,argwhen an input fails to pass validation.argis the argument as parsed by vorpal. By default, this prints"#{arg} is an invalid value for #{key}".passedValidation: a function to be called withkey,argandvaluewhen an input passes validation.argis the argument as parsed by vorpal,valueis what thevalidatefunction returned. By default, this prints"set #{key} to #{arg}".
