SoFunction
Updated on 2025-03-03

Detailed explanation of how to develop command line tools

Preface

Node has brought great changes to front-end development and promoted the automation of front-end development. We can simplify development work and then use various toolkits to generate production environments. If runningsass src/sass/ dist/css/The Sass file can be compiled.

In actual development, 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 ishello world. New construction in the projectbinDirectory, under which the directory is created ishelperThe specific content of the document is as follows:

#!/usr/bin/env node

('hello world');

Modify the permissions to the helper file:

$ chmod 755 ./bin/helper

implementhelperThe file will be displayed on the terminalhello world

$ ./bin/helper
hello world

Symbol Links

Next we create a symbolic link, globallynode_modulesIn the directory, a symbolic link is generated to point to the local directory of the module, so that we can use it directlyhelperOrder.

In the projectAdd to the filebinFields:

{
 "name": "helper",
 "bin": {
 "helper": "bin/helper"
 }
}

Execute in the current project directorynpm linkCommand to 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 it directlyhelperOrder:

$ helper
hello world

Commander module

In order to write command line tools more efficiently, we use TJ's mastercommanderModule.

$ npm install --save commander

helperThe file content is modified to:

#!/usr/bin/env node

var program = require('commander');

program
 .version('1.0.0')
 .parse();

implementhelper -handhelper -VOrder:

$ helper -h

 Usage: helper [options]

 Options:

 -h, --help  output usage information
 -V, --version output the version number

$ helper -V
1.0.0

commanderModule provision-h, --helpand-V, --versionTwo built-in commands.

Create command

Create ahelper hello <author>command when the user entershelper hello ipluserWhen the terminal displayshello ipluser. RevisehelperFile content:

#!/usr/bin/env node

var program = require('commander');

program
 .version('1.0.0')
 .usage('&lt;command&gt; [options]')
 .command('hello', 'hello the author') // Add hello command .parse();

existbinCreated in the directoryhelper-hellodocument:

#!/usr/bin/env node

('hello author');

implementhelper helloOrder:

$ helper hello ipluser
hello author

Analyze input information

We hopeauthorIt is input by the user and the terminal should display it ashello ipluser. Revisehelper-helloFile content, parse user input information:

#!/usr/bin/env node

var program = require('commander');

();

const author = [0];

('hello', author);

Execute againhelper hello ipluserOrder:

$ helper hello ipluser
hello ipluser

Oh yeah, it's finally done, but as a programmer, that's not enough. When the user does not enterauthorWhen we hope the terminal can remind the user to enter information.

Prompt information

existhelper-helloAdd prompt information to the file:

#!/usr/bin/env node

var program = require('commander');

('&lt;author&gt;');

// When the user enters `helper hello -h` or `helper hello --helper`, the command usage example is displayed('--help', function() {
 (' Examples:');
 (' $ helper hello ipluser');
 ();
});

();
( &lt; 1) &amp;&amp; (); // When the user does not enter information, call the `help` method to display help information
const author = [0];

('hello', author);

implementhelper helloorhelper hello -hCommand, 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

At this point, we wrote a helper command line tool and has the helper hello <author> command. Friends who are just interested should quickly start practicing it yourself. Only by doing it yourself can it be considered true learning. I hope this article will be helpful to everyone.