Skip to content

Instantly share code, notes, and snippets.

@simonplend
Created October 12, 2022 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonplend/fd6f45282f33891987886664759088f5 to your computer and use it in GitHub Desktop.
Save simonplend/fd6f45282f33891987886664759088f5 to your computer and use it in GitHub Desktop.
Examples for: Command-line argument parsing with Node.js core
node script.mjs --name "Budgie" --verbose
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' }
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: []
// > }
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