日本語のREADMEはこちらです: README.ja.md
A minimalist argument parser for command-line interfaces.
This library is the guts of optimist's argument parser without all the fanciful decoration.
- Parses arguments into a convenient object.
- Supports long options (
--foo) and short options (-f). - Handles combined short options (
-abcis equivalent to-a -b -c). - Converts numeric and boolean values automatically.
- Supports option-value pairs with or without an equals sign (
--foo=bar,-f bar). - Populates
_with positional arguments. - Stops parsing after a
--argument. - Supports aliases and default values for options.
- Handles dotted notation to create nested objects (
--foo.bar=baz).
import parse from "https://code4fukui.github.io/minimist/index.js";
const argv = parse(Deno.args);
console.log(argv);First, install minimist using npm:
npm install minimistThen, use it in your script:
import parse from 'minimist';
// Or: const parse = require('minimist');
const argv = parse(process.argv.slice(2));
console.log(argv);$ deno run example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ deno run example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{
_: [ 'foo', 'bar', 'baz' ],
x: 3,
y: 4,
n: 5,
a: true,
b: true,
c: true,
beep: 'boop'
}
Parses the given args array of strings using the provided opts configuration object.
The returned argv object contains parsed options as keys. Any arguments that are not associated with an option are collected in the _ array.
Options (opts):
string: A string or an array of strings to always treat as string arguments.boolean: A string or an array of strings to always treat as boolean arguments. Iftrue, all--style arguments will be treated as booleans.alias: An object mapping string argument names to string or array of string aliases.default: An object mapping string argument names to default values.stopEarly: Iftrue, parsing stops at the first non-option argument (e.g., a positional argument), and all subsequent arguments are added to the_array.--: Iftrue, all arguments after a standalone--are treated as positional arguments and collected in a['--']property.unknown: A function that is called for any argument that is not defined inopts. If the function returnsfalse, the unknown argument is not parsed.
MIT License — see LICENSE.