Getting started with Node CLI

What is CLI

The full name of CLI is Command Line Interface, which is a type of terminal tool that interacts through the command line. In web development, we use CLI all the time to assist development and improve operational efficiency, such as git, npm, webpack, vite, etc.

Why you need CLI

CLI can help us operate computer systems more efficiently. During work, we can integrate regular, repetitive, tedious, and templated work into CLI tools. One command can quickly complete a series of operations

Compared to GUI tools (graphical user interface)

  • GUI: Focuses more on ease of use. Users can complete related tasks by clicking on the graphical interface.
  • CLI: focuses more on operational efficiency, combining automated operations, batch operations, etc. through command combinations

Node CLI development core steps

Preliminary description: Skip npm init -y Initialize project and other operations

1. Write the program to be executed

To define a command file in the project, the file header must have#!/usr/bin/env node, indicating that this file needs to be used node to execute, students who know python should have seen #!/usr/bin/env python, which means the same thing

#!/usr/bin/env node

console.log('Start to Dev Node CLI');
2. Define terminal execution commands

package.json Sentence middle, statement bin Character; formality:

<command>: <JS file>
{
    
    
    "bin": {
    
    
        "my-cli": "./bin/index.js"
    }
}
3. Debugging CLI tools

There are two ways, local packaging or publishing to npm. The way of publishing to npm is the way everyone usually packages. Here is the local method.

  1. Package project terminal execution command
npm pack
  1. Global installation
比如你的项目成叫my-cli
npm i my-cli-1.0.0.tgz -g
  1. Terminal execution

Insert image description here

Node CLI best practices

Follow POSIX parameters

The POSIX syntax has been widely accepted as the standard for command line tools, and we should abide by it when developing command line tools. Examples of common rules are as follows:

  • You can mark option parameters or options in directive help or examples with square brackets ( [] ) to indicate that they are optional, or with angle brackets ( <> ) to indicate that they are required.
  • Allows short-form single-letter arguments to be used as aliases for long-form arguments (see GNU Coding Standards)
  • Options specified using the abbreviated singular form - can contain one alphanumeric character.
  • Specifying multiple options without a value can be grouped, for example myCli -abc is equivalent to myCli -a -b -c

Command line power users expect your command line applications to have similar conventions as other Unix applications

Follow Semver version

Version format X.Y.Z-[state]

  • X represents the major version number: functions are added or removed, and the API is not backward compatible.
  • Y represents the minor version number: new functions, API backward compatibility
  • Z stands for revision number: bug fixes, API backward compatibility
  • [state] The status is explained as follows
describe illustrate
alpha Internal beta version: lots of bugs
beta Public beta version: There are bugs and defects.
gamma A fairly mature beta version: almost the same as the release version (generally skip this version)
rc Release countdown version: All functions have been implemented, most bugs have been fixed, and it is close to the release countdown. Sometimes it is further subdivided into rc.1 and rc.2.

Learn the commander.js terminal command framework in the next section

Guess you like

Origin blog.csdn.net/weixin_40639095/article/details/134835955