Nodejs command line, custom

Source: Internet
Author: User

First, the necessary plug-in

1. BABEL:ES6 syntax support, requires babel-perset-es2015 (convert to ES5 execution), Babel, Babel-core (program execution)

2. Commander: Custom Command Plug-in, provide parameter delivery, etc.

3. Liftoff: Implements input monitoring of the command window, as well as the command format specification.

4. Minimist: Read command line parameter plug-in

The above plugins can be installed directly via NPM.

Second, the sample code analysis:

2.1. Cli.js

var liftOff = require ("LiftOff");
Import app from "./app";
var argv = require ("Minimist") (Process.argv.slice (2));
var cli =new liftOff ({
Name: "Sam",
Processtitle: ' Samlong ',
ModuleName: "Sam",
extensions:{
'. js ': null
}
});
Cli.launch ({
CWD:ARGV.R | | Argv.root,
CONFIGPATH:ARGV.F | | Argv.file
},function (env) {
Console.log ("command name:" + argv["_"]);
Console.log ("Execution path:" + ENV.CWD);
var commander = require (' Commander ');
var _app = new app ();
_app.register (commander);
});

1. Liftoff class: Parsing command-line arguments by importing liftoff third-party plug-ins, and capturing by liftoff instance method launch

2. Require ("Minimist") (Process.argv.slice (2)): Convert command-line arguments to Objects

3../app: Import command Execution class

2.2. App.js

class app{    Register (commander)    {        Commander            . Option ('-D--date ', "Show Time")            . Parse (PROCESS.ARGV);         if (commander.date) {            console.log (' Date command is executed ');        }     default APP;

A simple command execution class that is implemented through commander.

2.3. Index.js

Require ("Babel-core/register");//import Babel-core, used to convert my Es6require ("./src/app") in code; require ('./ Src/cli ');

2.4. Execution Effect:

Third, git address

[Email Protected]:cqhaibin/commandertest.git

Nodejs command line, custom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.