Joao Victor
Joao Victor

Reputation: 1171

Yargs does not validate required parameters

I am trying to validate some arguments with yargs, like this:

var args = require('yargs')
        .command('comando',
            'comprimenta o usuário', 
            function (yargs){
                yargs.options({
                    comando: {
                        demand: true
                    }
                });
        })
        .argv;

And then I am running my program like this:

node app2.js

or like this:

node app2.js -comando

But I am not getting any error message from the program. What am i doing wrong?

Upvotes: 5

Views: 5823

Answers (2)

Mo Sa
Mo Sa

Reputation: 161

try this:

yargs.command({
    command: 'Add',
    describe: 'Add function',
    builder: {
        noteTitle: {            // first argument
            type: 'string',
            demandOption : true,
            describe: 'Note Title'
        },
        noteBody:                       // second argument
        {
            describe: 'Note body',      // description
            demandOption: true,         // optional param or not
            type: 'string'              // param type
        }
    },
    handler: function (e){
        console.log("New Note Title:" +  e.noteTitle);
        console.log("New Note Body:" +  e.noteBody);
    }
});

Upvotes: 1

bolav
bolav

Reputation: 6998

If all you want to do is make the --comando argument required try this:

var args = require('yargs')
        .command('comando', 'comprimenta o usuário')
        .demand('comando')
        .argv;

Upvotes: 4

Related Questions