Home > Article > Web Front-end > Introduction to creating the quick-shell.js library
This article mainly introduces the use of creating quick-shell.js library. It has certain reference value. Now I share it with everyone. Friends in need can refer to
I have always wanted to try publishing an npm package myself. I happened to have just finished learning the operating system and wrote a lot of small shell-type programs. I was thinking about encapsulating a package on nodejs to quickly create simple shell applications. library, so quick-shell.js was born
Using quick-shell, you can quickly build a simple shell-type application, which is very suitable for using js for course design or some small demos
The package has been published on npm. You can directly use npm to install it into the project dependency
npm install quick-shell
let shell = require('quick-shell'); shell .welcome('welcome to my shell program') .prompt('$ ') .listen('echo', (params) => { console.log(params); }) .listen('add', (params) => { let temp = params.split(' '); console.log( (parseInt(temp[0]) + parseInt(temp[1])).toString() ); }) .start();
You can simply build a shell type like this Application, the above code will look like this when running:
welcome to my shell program $
When you type 'echo hello world':
welcome to my shell program $ echo hello world hello world
When you type 'add 7 9':
welcome to my shell program $ add 7 9 16
Just like this, whenever you want to add a command to your own shell application, just add its response
Installation:
npm install quick-shell
Chain Type call:
shell .//... .//... .start();
Set welcome text:
shell .welcome('your welcome text');
Set prompt:
shell .prompt('# ');
Custom error prompt:
shell .error({ inputNothing: 'you input nothing', noMatchedInstruction: 'have no matched instruction' });
Create a command listener:
// 这里的 params 以 'param param param' 的形式存在 shell .listen('echo', (params) => { console.log(params); });
If you are still not satisfied with the existing functions, you can use internally defined events to perform customized operations:
shell .onStart(() => { // do something on shell start }) .onExit(() => { // do something on shell exit }) .onLine((line) => { // do something when a line inputed }) .onCaught((instruction, params) => { // do something when a instruction was caught });
Start running the program:
shell .start();
Above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The above is the detailed content of Introduction to creating the quick-shell.js library. For more information, please follow other related articles on the PHP Chinese website!