dommer
dommer

Reputation: 19810

How would I configure yargs to take a single input file and optional output file?

I'm playing with yargs as would like to configure the following command line options:

Usage: myapp <source file> [destination file]

No options, no commamnds, etc. Just takes a file path to read from and an optional one to write to. What's the yarg config that gets me that and gives me all the nice documentation?

Seems like I either need to use an option or a command. Can't I just pass in the file argument on its own?

Couldn't see how to do this with commander either. Trying to trade up from process.argv, but stuck at the first hurdle.

Upvotes: 2

Views: 2949

Answers (1)

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24600

Here is an example how to get the first and second parameters:

var argv=require('yargs').argv
var source=argv._[0]
var dest=argv._[1]

Or even a better way:

const argv = require("yargs").command(
  "$0 <source file> [destination file]",
  "the default command",
  () => {},
  function({ sourcefile, destinationfile }) {
    console.log({ sourcefile, destinationfile })
  },
).argv

$0 is the default command. The output should be:

myapp.js <source file> [destination file]

the default command

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Not enough non-option arguments: got 0, need at least 1

Learn more:

Upvotes: 1

Related Questions