This is the 13th day of my participation in the August More Text Challenge

commander

Gui-graphical User Interface: Office, VScode, Browser, player…… Cli-command-line Interface: The command line interface, also known as CUI, is not as intuitive as GUI, Babel, TSC/webpack/Vue – CLI Server – Service provision (Web Server, IM…) CLI command [subCommand] [options] [arguments] command: commands, such as vue [subCommand] : subcommands, such as vue create [options] : Vue -h, vue -v [arguments] : parameter, which is used by some commands, for example, vue create myApp Options are built-in commands that the user selects, and the parameter is usually the value that the user decides to pass in. Options can be either full or abbreviated (depending on the command help), such as –version = -v full: starts with — / abbreviated: Options that start with a – can also accept a value, written after the option, separated by a space. Multiple short options can be joined with a hyphen (-) at the beginning. Note that options that accept a value should be placed at the end, for example: Vue create -d -r <-r value > myApp vue create -dr <-r value > myApp Commander Command line development tool chalk Command line style controller Inquirer Interactive command line tool .parse(argv: String []) parses the argv command string passed in for execution. Normally, the command string is derived from the user’s input on the command line. Process. argv commander also creates a -h, –help option by default. -v, –version option STR: version number flags: specifies the option. The default value is “-v, –version”. Option (flags, description? , fn? , defaultValue?) Flags: indicates the flag name of the option, “-v, –version” description: indicates the description of the option. Fn: indicates the defaultValue. The function returns defaultValue, which has a higher priority than defaultValue. -n, –name [val] -n, –name [] Optional <> Required After the setting is successful, Action (fn) specifies the action to be performed by the command object. The function will receive at least one argument if the command has an argument, Commander instance. Command (name, desc?, opts?) name: specifies the name of the command. The value can also be ‘create [appName]’ desc: brief opts: Configuration.description (STR) Description of the command. Alias (STR) Alias of the command. Usage (STR) Sets or gets the usage description of the current command

npm i commander

Const commander=require('commander') // Sets the version of the current command // commander. Version ('1.0.1') // Sets the case of the current command line Commander. Version (' 1.0.1 ', '- v - version) / / [] said optional < > said required / / commander. The option (' -n, - the name < val > ', 'print name', 'CCCC') commander. Option (' -n, --name[val]',' print name ',function(val){console.log(val,'000000') return val}) Log (commander) console.log('hi '+ commander.rawargs.name)}) // Parse data from process.argv commander.parse(process.argv) // console.log(commander);Copy the code

ls

Const commander=require('commander'); const commander=require('commander'); Commander. Version ('v1.0.0','-v, --version') // The subcommand has no name,  // const subcommander=commander.command('<path>') // subcommander.option('-n, Commander. Action ((path)=>{// The path parameter is the path console.log(path) defined in the command; // Try {const files= fs.readdirsync (path) console.log(files); }catch(e){ console.log(e); }}) if(process.argv.length<3){process.argv.push(__dirname)} // Argv commander. Parse (process.argv)Copy the code

Implement the -l command

Const commander=require('commander'); const commander=require('commander'); Const fs=require('fs') // Set the current version of the command tool commander. Version ('v1.0.0','-v, ') --version') commander. Option ('-p,--path [path]',' set the directory to display ',__dirname) Use commander. Option ('-l,-list [path]',' display as a list ') // to implement the specific logic of the command commander. Action (()=>{ Try {const files= fs.readdirsync (commander.path); // The variable in option will be attached to the same property of the current commander object. console.log(commander.List); if(commander.List){ let output=files.map(file =>{ let stat=fs.statSync(commander.path+'/'+file); Let type= stat.isdirectory ()? 'the directory' : 'file' return ` [${type}] ${file} \ r \ n `}). Join ("); console.log(output); }else{ console.log('00000000000000000000000000') console.log(files); } }catch(e){ console.log(e); } //console.log(commander); }) if(process.argv.length<3){process.argv.push(__dirname)} // Argv commander. Parse (process.argv)Copy the code



Chalk’s case is used in LS

Chalk handles colors

chalk chalk.<style>[.<style>...] (string, [string...] Text Colors: red, green, yellow, blue, cyan Background Colors BgRed, bgGreen, bgYellow, bgBlue, bgCyan chalk Colors. Hex ('#DEADED'). Keyword ('orange'). RGB (15, 100, 204) Background colors .hex('#DEADED') .keyword('orange') .rgb(15, 100, NPM I chalk/yarn add chalk use const chalk = require('chalk') to get a chalk object Just like CSSCopy the code

Inquirer

NPM I inquirer install NPM I inquirer use require(‘ inquirer ‘) inquirer.prompt(questions). Then (answers=>{… })

q.js

Const inquirer=require('inquirer') Question type, INPUT, confirm, list, rawlist, expand, checkbox, password, Editor name: problem name, used by the program for subsequent use message: problem text, shown to users default: Default values: choices: validate: input validation filter: Prompt ([{type:'input', name:'username', message:' please input name', default:'wanzi', Validate (val){if(val.trim()==''){return '; else{return true}}, Filter (val){return val.tolowerCase ()}}, {type:'confirm', name:'xingbie', message:' gay ', Default :false},{type:'list', name:'gongzi', message:' your salary range ', choices:['100-1000', '1000-2000'], Default :1},{type:'rawlist', name:'gongzi2', message:' your salary range ', choices:['100-1000', '1000-2000'], Default :1},{type:'checkbox', name:'tools', message:' topic you are interested in ', choices:[{name:' beauty ', value:'meizhuang', Checked :true},{name:' mingxing',{name:' bagua', value:'bagua'}]}]). Then (Answers =>{console.log(answers); })Copy the code