cli
object exposes methods for reading command-line arguments, displaying messages to the user, and asking the user for input.cli.accept(Object definitions)
Reads and parses program arguments according to definitions, and populates cli.args
with the resulting values.
For each definition that matches a program argument, the processed value is stored in cli.args
, named after the definition's key in definitions.
Array
with the following values:
String
.required()
or defaultsTo(value)
. Applied when the argument isn't provided by the user.
The argument value is sourced differently depending on the definition's type.
If the type is Boolean
, the value is set to true if the argument is present, and otherwise to false.
If the type is other than Boolean
, the next program argument is consumed as a value.
--name
.-n
.-vn
.
#0
.#+
.If there is no definition using a rest identifier, the rest values are implicitely collected in a value named rest
.
definitions |
A map of argument definitions, by value name.
|
// $ lemon adduser.js -a --name=Rose
cli.accept({
username: ['--name', String, 'Name of user to add'],
isAdmin: ['-a', Boolean, 'Make user an admin']
});
return cli.args; // {username: 'Rose', isAdmin: true}
// $ lemon transpile.js -x app.ts build.js
cli.accept({
sourcePath: ['#0', File, 'Source file'],
destinationPath: ['#1', File, 'Where to write the transpiled file'],
overrideExisting: ['-x', Boolean, 'Silently override the destination']
});
return cli.args; // {sourcePath: File('app.ts'), destinationPath: File('build.js'), overrideExisting: true}
// $ lemon check.js -v --strict catfacts.txt dogfacts.txt
cli.accept({
paths: ['#+', File, 'Files to check for errors'],
verbose: ['-v --verbose', Boolean, 'Shows all errors'],
strictMode: ['-s --strict', Boolean, 'Enable strict checks']
});
return cli.args; // {paths: [File('catfacts.txt'), File('dogfacts.txt')], verbose: true, strictMode: true}
// $ lemon delete-big-files.js cat-videos
cli.accept({
inputFolder: ['#0', Folder, required(), 'The folder to clean up'],
maximumSize: ['-s --size', Number, defaultsTo(1000), 'The maximum file size in MB'],
extensionFilter: ['-e --extension', String, 'Only delete files with this extension']
});
return cli.args; // {inputFolder: Folder('cat-videos/'), maximumSize: 1000, extensionFilter: null}
cli.args
Object
cli.accept()
.
cli.tell(String text)
stdout
.text |
The text to display.
|
cli.tell('Hello there!'); // displays “Hello there!”
Promise<Any>
cli.tellWhile(String text, Promise asyncOperation)
stdout
, then erases it once asyncOperation resolves.Promise
.
text |
The text to display while awaiting.
|
asyncOperation |
The
Promise to wait for. |
const versionData = await cli.tellWhile(
'Loading version data...',
net.getJSON('http://example.org/versions.json')
);
cli.tell(`Latest version is ${versionData.latest}.`);
Promise<Any>
cli.ask(String promptText, [TypeDefinition type = String, [Boolean optional = false]])
Promise
. If the value is invalid, the user is asked again.
promptText |
The prompt to display.
|
type |
The type definition to apply to the entered value.
Defaults to String .
|
optional |
If
Defaults to true , allows the user to enter an empty string, which will result in the value being null .false .
|
const balloonCount = await cli.ask('How many balloons do you want?', Number);
cli.tell(`Number of balloons requested: ${balloonCount}`);
Promise<Object>
cli.askMany(Object definitions)
cli.ask()
for each item in definitions, using the item's values for arguments.Promise
.
definitions |
A map of argument arrays for
ask() , by value name. |
const responses = await cli.askMany({
flavor: ['What flavor should the cake be?', String],
deliveryDate: ['When should it be delivered?', moment]
});
const formattedDeliveryDate = format.date(responses.deliveryDate, true);
cli.tell(`Your ${responses.flavor} cake will be delivered ${formattedDeliveryDate}.`);