Home > Web Front-end > JS Tutorial > body text

Introduction to creating the quick-shell.js library

不言
Release: 2018-07-11 15:49:09
Original
2935 people have browsed it

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

quick-shell.js Introduction

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

Installation

The package has been published on npm. You can directly use npm to install it into the project dependency

npm install quick-shell
Copy after login
Copy after login

Basic use

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();
Copy after login

You can simply build a shell type like this Application, the above code will look like this when running:

welcome to my shell program
$
Copy after login

When you type 'echo hello world':

welcome to my shell program
$ echo hello world
hello world
Copy after login

When you type 'add 7 9':

welcome to my shell program
$ add 7 9
16
Copy after login

Just like this, whenever you want to add a command to your own shell application, just add its response

API

Installation:

npm install quick-shell
Copy after login
Copy after login

Chain Type call:

shell
    .//...
    .//...
    .start();
Copy after login

Set welcome text:

shell
    .welcome('your welcome text');
Copy after login

Set prompt:

shell
    .prompt('# ');
Copy after login

Custom error prompt:

shell
    .error({
        inputNothing: 'you input nothing',
        noMatchedInstruction: 'have no matched instruction'
    });
Copy after login

Create a command listener:

// 这里的 params 以 'param param param' 的形式存在
shell
    .listen('echo', (params) => {
        console.log(params);
    });
Copy after login

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
    });
Copy after login

Start running the program:

shell
    .start();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template