cliopts
Simple Typescript CLI options parser.
Usage
Parse process arguments into options.
const options = parseArgs(process.argv.slice(2), {
true: { type: true },
false: { type: false },
string: { type: String },
number: { type: Number, alias: 'b' },
});
Read the options.
options.get('true');
options.get('false');
options.getAll('string');
options.getRequired('number');
options.names;
options.positional;
The get method returns the last value if the argument is repeated.
const options = parseArgs(['--foo=1', '--foo=2'], {
foo: { type: Number },
});
options.get('foo');
Errors are thrown...
- When unknown named options are encountered.
- When an option requires a value but a value is not given.
- When an option does not accept a value but a value is given.
const options = parseArgs(['--foo'], {
bar: { type: String },
baz: { type: true },
});
const options = parseArgs(['--bar'], {
bar: { type: String },
baz: { type: true },
});
const options = parseArgs(['--baz=1'], {
bar: { type: String },
baz: { type: true },
});
All arguments following -- are treated as positional (unnamed) options, even if they start with hyphens.
const options = parseArgs(['--foo', '--', '--bar'], {
foo: { type: true },
bar: { type: true }
});
options.has('bar');
options.names;
options.positional;