-
Notifications
You must be signed in to change notification settings - Fork 151
Commands
The basic usage of commands looks like this:
commands={{
somecmd: () => { // do some function },
}}
If you return a string or react element from your command it will be rendered ie
commands={{
somestring: () => { return 'some string to render'; },
someelement: () => { return <span>some element</span>; }
}}
An array of the input separated by spaces.
So for an input of echo hello world
the arguments would be ['echo', 'hello', 'world'] this is in effect to
simulate node's process.args
.
commands={{
sum: (args) => {
return args.slice(1).reduce((sum, val) => (sum + val), 0);
},
}}
This function is used to print an output. This is very useful if you command is async.
commands={{
fetch: (args, printLine) => {
fetch(args.slice(1).join(' '))
.then(res => res.json())
.then(res => printLine(res))
.catch(err => printLine(err));
},
}}
This function is used to attempt and run another command. The input has to be a string.
commands={{
'type-text': (args, printLine, runCommand) => {
const text = args.slice(1).join(' ');
printLine('');
for (let i = 0; i < text.length; i += 1) {
setTimeout(() => {
runCommand(`edit-line ${text.slice(0, i + 1)}`);
}, 115 * i);
}
}
}}
Commands allow for more configuration allowing for command line options.
{
somecmd: {
method: (args, printLine, runCommand) => {
// run command
},
options: [
]
}
}
Options are structured like so:
{
options: [
{
name: 'required',
description: 'required',
defaultValue: 'optional'
}
]
}
This is the only change to the api of the command function. The args
option changes to an object.
The args object will have a key for each option you describe as well as a key _
that is an array of any text not belonging to an option.
How this would look in the scope of a command.
commands={{
request: {
method: (args, printLine) => {
fetch(args.url, JSON.parse(args.body))
.then((res) => printLine(res));
},
options: [
{
name: 'url',
description: 'Url to request',
defaultValue: ''
},
{
name: 'method',
description: 'Http method to use',
defaultValue: 'GET'
},
{
name: 'body',
description: 'Body to send',
defaultValue: '{}'
}
]
},
}}
The usage of advanced commands always gives you a -h
and --help
option that prints out the usage of the command.
An example is the built in edit-line
.
> edit-line --help
Usage: edit-line [options]
Options:
-h, --help Output usage information
-l, --line <n> the line you want to edit. -1 is the last line (defaults to -1)