creply create read–eval–print-loop (repl) programs
npm install 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
``js`
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:
`json`
{
"name": "app",
"description": "simple node.js repl app",
"version": "v1.0.0",
"history": "app.history",
"prompt": "app> ",
"prefix": "!"
}
`js`
repl.start();
- this will start the repl and output the following:
`sh`
app>
- commands are added with the repl.addCommand() methodrepl.commands
- commands are saved in the object
`js`
repl.addCommand("hello",{
description: "hello world",
exec: (args) => {
console.log("hello world")
},
usage: () => "hello [name]"
})
- this will add a command with the following options:
`json`
{
"description": "say hello",
"exec": "[Function: exec]",
"usage": "[Function: usage]"
}
- commands are removed with the repl.removeCommand() method
`js`
repl.removeCommand("hello");
- this will remove the command with the name hellorepl.commands
- the command will be removed from the object
- options are updated with the repl.set() methodrepl.options
- options are saved in the object
- example updating the prompt
`js`
repl.set({
prompt: "cli> "
});
- this will update the prompt to cli>
- output will be:
`sh`
cli>
- options are retrieved with the repl.get() method
`js`
console.log(repl.get("prompt"));
- this will output the prompt
- output will be:
`sh`
cli>
- events are listen with the repl.on() methoduncaught-error
- when you listen an event the repl will not prints the data except the event
- example listening to the line event
- the event will be called when the user types a line
`js`
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
`js`
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
`js`
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
`js`
repl.on("exit", () => {
console.log("exit");
});
- example listening to the cursor-move event
- this event will be called when the cursor moves
`js`
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
`js`
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
`js`
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
`js`
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
`js`
repl.on("command-not-specified", () => {
console.log("command-not-specified");
});
- example listening to the start eventrepl.start()
- this event is triggered when the repl starts
- the listener must be before the call
`js`
repl.on("start", () => {
console.log("started!");
});
- 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.readlinerepl.rl
- to get rl use but you need to start the repl first by using repl.start()
- the repl.usage() method will print the usage of the command
`js`
repl.usage("hello"); // hello [name]
- you can type help [command] to see the usage of the command on the repl
`sh`
app> help hello
- output:
`sh`
hello [name]
- if console.log prints out of the repl, you can use the repl.log() methodrepl.log()
- the method will print out the data to the repl
- this methods clear prompt and move the cursor to the start of the line
`js`
repl.log("hello");
- output:
`sh``
cli>
hello
cli>