Detailed node.js how to develop command line tools _node.js

Source: Internet
Author: User

Objective

Node brings a lot of change to front-end development, and facilitates the automation of front-end development, we can simplify development work, and then build production environment with various toolkit. You can compile the Sass file if you run it sass src/sass/main.scss dist/css/main.css .

In the actual development process, we may have our own specific needs,

Then we have to learn how to create a node command line tool.

Hello World

The first procedure of the ritual is hello world . In the project, create a new bin directory under which a file named Helper is created, as follows:

#!/usr/bin/env node

console.log (' Hello World ');

To modify the permissions for a helper file:

$ chmod 755./bin/helper

Execute the helper file, and the terminal will display hello world :

$/bin/helper
Hello World

Symbolic Links

Next we create a symbolic link in the global node_modules directory, generating a symbolic link that points to the module's local directory so that we can use the helper command directly.

Add the bin field to the project's package.json file:

{
 ' name ': ' Helper ',
 ' bin ': {
 ' helper ': ' Bin/helper '
 }
}

Executes the command under the current engineering directory to npm link create a symbolic link for the current module:

$ NPM Link

/node_path/bin/helper->/node_path/lib/node_modules/mymodule/bin/helper
/node_path/lib/ Node_modules/mymodule->/users/ipluser/mymodule

Now we can use the helper command directly:

$ Helper
Hello World

Commander Module

To more efficiently write command-line tools, we use the Commander module of TJ Great God.

$ NPM Install--save Commander

The contents of the helper file are modified to:

#!/usr/bin/env node

var program = require (' Commander ');

Program
 . Version (' 1.0.0 ')
 . Parse (PROCESS.ARGV);

Execution helper -h and helper -V command:

$ helper-h

 usage:helper [options]

 options:-

 H,--help  output Usage information
 -V,--version Output the version number

$ helper-v
1.0.0

Commander module provides -h , --help and -V , --version two built-in commands.

Create command

Create a helper hello <author> command that when the user enters helper hello ipluser , the terminal displays hello ipluser . To modify the contents of a helper file:

#!/usr/bin/env node

var program = require (' Commander ');

Program
 . Version (' 1.0.0 ')
 . Usage (' <command> [options] ')
 . Command (' hello ', ' hello ' author ') Add Hello command
 . Parse (PROCESS.ARGV);

Create a new Helper-hello file in the bin directory:

#!/usr/bin/env node

console.log (' Hello author ');

To execute a helper hello command:

$ helper Hello ipluser
Hello author

Parsing input Information

We want the author to be entered by the user and the terminal should be displayed as hello ipluser . Modify helper-hello the contents of the file to resolve user input information:

#!/usr/bin/env node

var program = require (' Commander ');

Program.parse (PROCESS.ARGV);

Const Author = program.args[0];

Console.log (' Hello ', author);

To execute the helper hello ipluser command again:

$ helper Hello ipluser
Hello Ipluser

Oh yes, it's finally done, but as a programmer, that's far from enough. When the user does not enter author , we want the terminal to remind the user to enter the information.

Hint Information

To add a message to the helper-hello file:

#!/usr/bin/env node

var program = require (' Commander ');

Program.usage (' <author> ');

When the user enters the ' helper hello-h ' or ' helper Hello--helper ', display the command usage example
program.on ('--help ', function () {
 Console.log (' Examples: ');
 Console.log (' $ helper Hello Ipluser ');
 Console.log ();
});

Program.parse (PROCESS.ARGV);
(Program.args.length < 1) && program.help (); When the user does not enter information, call the ' Help ' method to display the aid information

const author = program.args[0];

Console.log (' Hello ', author);

Execution helper hello or helper hello -h command, the terminal will display Help information:

$ helper Hello

 usage:helper-hello <author>

 Options:-

 H,--help output Usage information

 Examples:
 $ helper Hello ipluser

$ helper hello-h

 Usage:helper-hello <author>

 Options:

 -H, --help Output usage information

 examples:
 $ helper Hello Ipluser

Summarize

Here we have written a helper command-line tool with helper Hello <author> commands. Just interested friends quickly hands-on practice, only to do their own to calculate the real learning, I hope this article can help.

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.