Last active
July 5, 2024 12:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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