Skip to content

Instantly share code, notes, and snippets.

@cmaster11
Last active July 5, 2024 12:03
Show Gist options
  • Save cmaster11/e61550477c44a29d14c873564023b16e to your computer and use it in GitHub Desktop.
Save cmaster11/e61550477c44a29d14c873564023b16e to your computer and use it in GitHub Desktop.
A yargs/commander.js little comparison on how they handle multiple CLI arguments
import yargs from "yargs";
import {hideBin} from "yargs/helpers";
import {inspect} from 'node:util'
import {Command} from "commander";
// Compare yargs and commander.js multiple CLI args array handling
// Run:
//
// node yargs-commander-compare.js --str "a string" --arr "array string 1"
// node yargs-commander-compare.js --str "a string" --str "another string" --arr "array string 1" --arr "array string 2"
{
console.log('yargs')
const argv = yargs(hideBin(process.argv))
.option('str', {type: 'string', array: false})
.option('arr', {type: 'string', array: true})
.parse()
console.log(`args: ${inspect(argv)}`)
}
{
console.log('commander')
const program = new Command()
.option('--str <string>')
.option('--arr <string...>')
program.parse(process.argv);
console.log(`args: ${inspect(program.opts())}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment