
How to Make Your First Command
How to Make Your First Command 관련
After the greeting, we can write our first command. It will be helpful and will work with any commands we add later.
const commanns = {
help() {
}
};
This will be our help command where we'll add a list of commands available to our terminal portfolio. We will use Intl.ListFormat
, which creates a list of elements with and before the last element.
const formatter = new Intl.ListFormat('en', {
style: 'long',
type: 'conjunction',
});
To create a list, we need to use formatter.format()
and pass an array of commands. To get that array we can use Object.keys()
:
const commands = {
help() {
term.echo(`List of available commands: ${help}`);
}
};
const command_list = Object.keys(commands);
const help = formatter.format(command_list);
When you type help you should see:
List of available commands: help
You also need to add the echo
command:
const commands = {
help() {
term.echo(`List of available commands: ${help}`);
},
echo(...args) {
term.echo(args.join(' '));
}
};
Now the help command works:
List of available commands: help and echo
But if you try to execute 'echo hello' you will get an error:
[Arity] Wrong number of arguments. The function 'echo' expects 0 got 1!
By default, jQuery Terminal checks the number of arguments and the number of parameters the function accepts. The problem is that the rest
operator makes all arguments optional and the length function property is 0. To fix the issue we need to disable the Arity
check with an option:
const term = $('body').terminal(commands, {
greetings: false,
checkArity: false
});
Now the echo commands should work.