当前位置:网站首页>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

边栏推荐
- 论pyscript使用感想(实现office预览)
- Pytorch GPU and CPU models load each other
- OpenCV环境搭建
- Classes and objects (II)
- Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
- [express connection to MySQL database]
- C语言之基础语法
- Mongo Shell交互式命令窗口
- Dasctf2022.07 empowerment competition
- pyscript无法引入包
猜你喜欢

Idea small settings

redux快速上手

Classes and objects (II)

Record the Niua packaging deployment project

Deploy Jenkins using containers

央企建筑企业数字化转型核心特征是什么?

The third ACM program design competition of Wuhan University of Engineering

Don't stick to it for 68 days. Baboons eat bananas

Webrtc realizes simple audio and video call function

No, just stick to it for 59 days
随机推荐
Exception handling: pyemd or pyemd not found
Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
Pytoch distributed training
Basic grammar of C language
UE plays video in scene or UMG
Opencv environment construction
Makefile(make)常见规则(二)
Mysql:The user specified as a definer (‘root‘@‘%‘) does not exist 的解决办法
异常处理:pyemd或PyEMD找不到
手机工作室网络如何组建?
Oracle update and delete data
Actual combat of flutter - DIO of request encapsulation (II)
The daily life of programmers
Pyqt5 learning pit encounter and pit drainage (3) background picture coverage button style and check button status
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
[QT learning notes] * insert pictures in the window
Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
Integration of Nan causes in pytorch training model
New year's greetings from programmers