Created
October 12, 2022 08:49
-
-
Save simonplend/fd6f45282f33891987886664759088f5 to your computer and use it in GitHub Desktop.
Examples for: Command-line argument parsing with Node.js core
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
node script.mjs --name "Budgie" --verbose |
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 minimist from "minimist"; | |
const args = minimist(process.argv.slice(2), { | |
string: ["name"], | |
boolean: ["verbose"], | |
alias: { | |
name: "n", | |
verbose: "v", | |
}, | |
}); | |
console.log(args); | |
// > { _: [], verbose: true, v: true, name: 'Budgie', n: 'Budgie' } |
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 { parseArgs } from "node:util"; | |
const args = parseArgs({ | |
options: { | |
name: { | |
type: "string", | |
short: "n", | |
}, | |
verbose: { | |
type: "boolean", | |
short: "v", | |
}, | |
}, | |
}); | |
console.log(args); | |
// > { | |
// > values: { name: 'Budgie', verbose: true }, | |
// > positionals: [] | |
// > } |
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 { parseArgs } from "@pkgjs/parseargs"; | |
const args = parseArgs({ | |
options: { | |
message: { | |
type: "string", | |
default: "Oh hai there!", | |
}, | |
} | |
}); | |
console.log(args); | |
// > { | |
// > values: [Object: null prototype] { message: 'Oh hai there!' }, | |
// > positionals: [] | |
// > } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment