Search code examples
node.jsyargs

yargs: Require at least one argument without a corresponding flag


I am building a Node CLI app that needs to be passed a single file as an argument.

For example:

myapp.js /Users/jdoe/my_file.txt

I know I can reference /Users/jdoe/my_file.txt through the _ object in yargs but how do I require that it be provided? I see the demandOption() method but I do not know how to demand options that do not have a corresponding flag (name).

I tried the following and it does not work:

.demandOption('_', "A file path is required")

Solution

  • I ended up using .demandCommand(1) which works!