当前位置:网站首页>Command line interactive tools (latest version) inquirer practical tutorial
Command line interactive tools (latest version) inquirer practical tutorial
2022-07-29 04:37:00 【Chaoyang 39】
inquirer Official website
https://www.npmjs.com/package/inquirer
establish inquirer The demonstration project of
- New folder inquirerDemo
- stay inquirerDemo Open the command line in the folder , perform
npm init -y
It will generate automatically package.json
- install inquirer
cnpm install --save inquirer
nothing cnpm Friends of the first execution npm i cnpm
- stay package.json Add project startup script
"start": "node index.js",

- stay package.json Add a pair of ES6 Modular specification support
So that import Import dependence
"type": "module",

here package.json The complete code is as follows :
( The latest at that time “inquirer” Version is “^9.0.2”)
{
"name": "inquirerdemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"inquirer": "^9.0.2"
}
}
- In the folder inquirerDemo Create file in index.js , The content is :
import inquirer from "inquirer";
inquirer
.prompt([
{
type: "input",
message: " Please enter your name :",
name: "name",
},
])
.then((answers) => {
console.log(answers);
});
- Start project
npm run start

Enter your name and press enter , Here is my nickname The rising sun For example :
The project was created and run successfully !
Import inquirer rely on
import inquirer from "inquirer";
Prompt the user for input input
inquirer
.prompt([
{
type: "input", // Interaction types
message: " Please enter your name :", // introducer
name: "name", // Custom field name
default: " The rising sun ", // Set the default value
},
])
.then((answers) => {
// Print user input
console.log(answers);
});



- Verify the input
inquirer
.prompt([
{
type: "input",
message: " Please enter a number :",
name: "num",
validate: function (val) {
const done = this.async();
if (val > 18) {
done(null, true);
}
done(" The number cannot be less than 18!");
},
},
])
.then((answers) => {
// Print user input
console.log(answers);
});


Confirm with user confirm
inquirer
.prompt([
{
type: "confirm", // Interaction types -- confirm
message: " Is it male ?", // introducer
name: "genderConfirm", // Custom field name
prefix: " System administrator :", // Prefix
suffix: " Will answer ", // suffix
},
])
.then((answers) => {
// Print user input
console.log(answers);
});


Disordered single selection list
inquirer
.prompt([
{
type: "list", // Interaction types -- The radio ( disorder )
message: " Please choose a fruit :", // introducer
name: "fruit", // Custom field name
choices: [" Apple ", " pears ", " Banana "], // A list of options
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

Up and down arrow keys can adjust options , Press enter to confirm the selection 
Ordered single selection rawlist
inquirer
.prompt([
{
type: "rawlist", // Interaction types -- Ordered single selection
message: " Please choose a fruit :", // introducer
name: "fruit", // Custom field name
choices: [" Apple ", " pears ", " Banana "], // A list of options
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

You can directly enter the serial number , Such as 3, To choose bananas
You can also press up and down arrow keys to adjust options , Press enter to confirm the selection


Indexed radio expand
inquirer
.prompt([
{
type: "expand", // Interaction types -- Indexed radio
message: " Please choose a fruit :", // introducer
name: "fruit", // Custom field name
// A list of options
choices: [
{
key: "a",
name: " Apple ",
value: "apple",
},
{
key: "O",
name: " a mandarin orange ",
value: "orange",
},
{
key: "p",
name: " pears ",
value: "pear",
},
],
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

Input a Will choose apples 
Input o Will choose oranges
Input p Will choose pears
Input h After entering, all options will be displayed 
If you enter directly , All options will also be displayed 
Enter the correct index value and press enter to get the final result 
multi-select checkbox
inquirer
.prompt([
{
type: "checkbox", // Interaction types -- multi-select
message: " Choose a color :", // introducer
name: "color", // Custom field name
// A list of options
choices: [
{
name: " Red ",
},
new inquirer.Separator(), // Default separator
{
name: " Blue ",
checked: true, // Selected by default
},
{
name: " green ",
},
new inquirer.Separator("--- Custom separator ---"), // Custom delimiter
{
name: " yellow ",
},
],
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

Default blue is selected
Press the space bar to select
The up and down arrow keys can adjust the arrow in front of the option >
Press enter to print the final selection result 

- Scrollable multi selection
inquirer
.prompt([
{
type: "checkbox", // Interaction types -- multi-select
message: " Choose a color :", // introducer
name: "color", // Custom field name
// A list of options
choices: [" Red ", " Blue ", " green ", " yellow "],
pageSize: 2, // Set the number of lines displayed by the option
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

The up and down arrow keys can adjust the arrow in front of the option >, The option of exceeding the number of trips will be shown in the round robin
Input password password
inquirer
.prompt([
{
type: "password", // Interaction types -- password
message: " Please input a password :", // introducer
name: "pwd", // Custom field name
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

When entering the password , The password input process is not visible
After inputting the password and pressing enter , Print the entered password

Editor ( Multi line input ) editor
inquirer
.prompt([
{
type: "editor", // Interaction types -- Editor ( Multi line input )
message: " Please enter comments :", // introducer
name: "content", // Custom field name
},
])
.then((answers) => {
// Print the results
console.log(answers);
});

Press enter , A temporary Notepad will be created and opened

After entering anything , Close Notepad , Will print out the input

边栏推荐
- [c language] PTA 7-55 query fruit price
- Dasctf2022.07 empowerment competition
- On quotation
- Record the Niua packaging deployment project
- 正确的用户拖拽方式
- Oracle 插入数据
- Dabao and Erbao
- Actual combat of flutter - DIO of request encapsulation (II)
- Classes and objects (II)
- 异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
猜你喜欢

img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)

On the use of pyscript (realizing office preview)

Tower of Hanoi classic recursion problem (C language implementation)

Install the gym corresponding to mujoco in the spinning up tutorial, and the error mjpro150 is reported

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

UE 在场景或UMG中播放视频

14. Haproxy+kept load balancing and high availability

MySQL - 聚簇索引和辅助索引

C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list

Vscode one click compilation and debugging
随机推荐
Basic grammar of C language
11. Backup switch
Record the Niua packaging deployment project
Dabao and Erbao
异常处理:pyemd或PyEMD找不到
spinning up安装完使用教程测试是否成功,出现Library“GLU“ not found和‘from pyglet.gl import *错误解决办法
Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
Configure st-gcn environment record [Google lab]
MySQL - 聚簇索引和辅助索引
Classes and objects (III)
[express connection to MySQL database]
Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary
mujoco和mujoco_py安装以及解决libXcursor.so.1:NO such dictionary
使用容器部署Jenkins
Leetcode 686. KMP method of repeatedly superimposing strings (implemented in C language)
Dasctf2022.07 empowerment competition
Down sampling and up sampling
读懂 互联网巨头 【中台之战】 以及 中台 发展思维
正确的用户拖拽方式
redux快速上手