creply
v1.1.2
Published
creply create read–eval–print-loop (repl) programs
Downloads
11
Maintainers
Readme
creply
create read–eval–print-loop (repl) programs want to create a repl? with creply you create easily you can use it with TypeScript too!
- also see the documetation generated by typedoc
creating the repl
const creply = require("creply");
const repl = new creply({
name: "app",
description: "simple node.js repl app",
version: "v1.0.0",
history: "app.history",
prompt: "app> ",
prefix: "!"
});
- this will create a repl with the following options:
{
"name": "app",
"description": "simple node.js repl app",
"version": "v1.0.0",
"history": "app.history",
"prompt": "app> ",
"prefix": "!"
}
starting the repl
repl.start();
- this will start the repl and output the following:
app>
adding commands
- commands are added with the
repl.addCommand()
method - commands are saved in the
repl.commands
object
repl.addCommand("hello",{
description: "hello world",
exec: (args) => {
console.log("hello world")
},
usage: () => "hello [name]"
})
- this will add a command with the following options:
{
"description": "say hello",
"exec": "[Function: exec]",
"usage": "[Function: usage]"
}
removing commands
- commands are removed with the
repl.removeCommand()
method
repl.removeCommand("hello");
- this will remove the command with the name
hello
- the command will be removed from the
repl.commands
object
updating options
- options are updated with the
repl.set()
method - options are saved in the
repl.options
object - example updating the prompt
repl.set({
prompt: "cli> "
});
- this will update the prompt to
cli>
- output will be:
cli>
getting options
- options are retrieved with the
repl.get()
method
console.log(repl.get("prompt"));
- this will output the prompt
- output will be:
cli>
listening to events
events are listen with the
repl.on()
methodwhen you listen an event the repl will not prints the data except the event
uncaught-error
example listening to the
line
eventthe event will be called when the user types a line
repl.on("line", (line) => {
console.log("line: " + line);
});
- example listening to the
uncaught-error
event - the event will be called when the repl throws an error
repl.on("uncaught-error", (err) => {
console.log("uncaught-error: " + err);
});
- example listening the
keypress
event - the event will be called when the user press a key
repl.on("keypress", (char, key) => {
console.log("keypress: " + key);
});
- example listening to the
exit
event - this event will be called when the repl is closed
repl.on("exit", () => {
console.log("exit");
});
- example listening to the
cursor-move
event - this event will be called when the cursor moves
repl.on("cursor-move", (cursor) => {
console.log("cursor-move: " + cursor);
});
- example listening to the
command
event - this event will be called when the repl executes a command
repl.on("command", (command, args) => {
console.log("command: " + command);
});
- example listening to the
command-not-found
event - this event will be called when the repl executes a command that doesn't exist
repl.on("command-not-found", (command) => {
console.log("command-not-found: " + command);
});
- example listening to the
did-you-mean
event - this event will be called when repl try to mean the command
repl.on("did-you-mean", (command, didYouMean) => {
console.log("did-you-mean: " + didYouMean);
});
- example listening to the
command-not-specified
event - this event will be called when the repl executes a command that doesn't have a name
repl.on("command-not-specified", () => {
console.log("command-not-specified");
});
- example listening to the
start
event - this event is triggered when the repl starts
- the listener must be before the
repl.start()
call
repl.on("start", () => {
console.log("started!");
});
readline and rl
- the readline is the readline library used by the creply
- the rl is the readline interface used by the creply
- to get readline use
repl.readline
- to get rl use
repl.rl
but you need to start the repl first by usingrepl.start()
view command usage
- the
repl.usage()
method will print the usage of the command
repl.usage("hello"); // hello [name]
- you can type
help [command]
to see the usage of the command on the repl
app> help hello
- output:
hello [name]
common problems
- if
console.log
prints out of the repl, you can use therepl.log()
method - the
repl.log()
method will print out the data to the repl - this methods clear prompt and move the cursor to the start of the line
repl.log("hello");
- output:
cli>
hello
cli>