I am using yargs to add command line options for the arguments passed to the script. When i issue the help command along with the script name, It does not display the help for add parameter.
const yargs=require('yargs');
const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
title : {
describe : 'title of the file',
demand: 'true'
}
})
.help()
.argv;
root# node mainFile_node.js --help
Options:
--help Show help [boolean]
--version Show version number
node mainFile_node.js add
YARGS ARGV:-{ _: [ 'add' ], '$0': 'mainFile_node.js' }
const yargs = require("yargs");
yargs.command({
command: "add",
describe: "Add new note",
builder: {
title: {
// describe: 'note title',
demandOption: true,
type: "string"
}
},
handler: function(argv) {
console.log("My title: " + argv.title);
}
});
yargs.parse();
Then run in the command line:
node filename.js add --title="title name"
Try with below code
const yargs = require("yargs")
yargs.command({
command: 'add',
describe: 'ADD A NOTE TO A FILE',
handler: function(){
console.log('Adding to a file')
}
})
Then execute with the below command
root# node mainFile_node.js --help
const yargs=require('yargs');
const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
title : {
describe : 'title of the file',
demand: 'true'
}
}).parse()
u have to add the parse() method at the end. parse() will also execute the handler function if u are happen to use in the future.
or under the code
console.log(yargs.argv)
this method is useful if you did not write any code on your file but you are working on command line. let's say
node app.js add --title="somthing" --help
in order to see the help menu u have to type console.log(yargs.argv)
on the file.
First check if node ./main.js --h is working?
Then, just run node ./app.js --help on a seperate js file and then it will start working on the previous file.
const chalk = require('chalk');
const yargs = require('yargs');
//Create add command
yargs.command({
command:'add',
describe:'Add a new note',
handler:function(){
console.log("add a new note");
}
})
//Create remove command
yargs.command({
command:'remove',
describe:'Remove the note',
handler:function(){
console.log("Removing a note");
}
})
//Create list command
yargs.command({
command:'list',
describe:'List all the notes',
handler:function(){
console.log("List notes");
}
})
//Create read command
yargs.command({
command:'read',
describe:'read note',
handler:function(){
console.log("read a note");
}
})
console.log(yargs.argv)
yargs.command({
command: 'add',
showInHelp: true, //add this line here!!!
describe: 'add a note',
builder: {
title: {
describe: 'title of my note',
demandOption: true,
type: 'string',
},
body: {
describe: 'body note',
demandOption: true,
type: 'string',
}
},
handler: function (argv) {
console.log(argv.title,'\n', argv.body, '\n creating the note', argv)
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.