ArgMate
Your go-to companion for lightning-fast CLI parameter parsing, seasoned with convenient features to make your development experience much more smooth.
While developing things like AlaSQL and RexReplace I've always been caught between two types of CLI parsers. On one hand, there are feature-rich options like yargs and commander, which, despite their heavy startup time, provide useful help like easy defaults, smooth validation, and well-structured CLI help text output. On the other hand, more simple alternatives like nopt and mri excel in performance but lack in development experience. After yet again uncovering a performance hit from using a heavyweight parser, I decided to solve this issue once and for all.
Benchmark:
argMate 9,089,813 ops/sec ±2.15% (98 runs sampled) 1x
nopt 2,070,397 ops/sec ±1.21% (94 runs sampled) 4x
mri 1,832,768 ops/sec ±0.13% (99 runs sampled) 5x
minimist 706,265 ops/sec ±1.05% (94 runs sampled) 13x
yargs-parser 67,417 ops/sec ±0.39% (97 runs sampled) 135x
Meet ArgMate: a CLI parameter parser that's not just fast—it's 4-5 times faster than other parsers focused on speed, while being feature-rich. But how?!? A computer processes instructions at a set pace. To get results faster the only option is to ask the computer to do less work. By minimising how many times variables are touched and keeping those operations close together, the implementation enables efficient caching of data, resulting in fewer CPU cycles to get the result.
Installation
yarn add argmate
npm install argmate
Usage
argMate(arguments, [parameters [, config ]]);
Examples
Getting Started
import argMate from 'argmate';
const args = process.argv.slice(2);
const params = {
loops: 10,
help: false
};
const config = {
Defaults to true.
allowUnknown: false,
error: msg => {
console.error('There was a problem:', msg);
process.exit(1);
},
};
const argv = argMate(args, params, config);
A More Complete Example
import argMate, {helpText} from 'argmate';
const args = process.argv.slice(2);
const params = {
start: {
default: 0,
alias: ['s'],
},
steps: {
type: 'number',
mandatory: true,
alias: ['l', 'loops'],
valid: v => v > 0,
},
help: {
alias: ['h'],
},
};
const argv = argMate(args, params);
if (argv.help) {
console.log(helpText());
process.exit();
}
for (let i = argv.start; i < argv.start + argv.steps; i++) {
console.log(i);
}
Default Behavior
import argMate from 'argmate';
let argv;
argv = argMate(['--foo', 'bar']);
argv = argMate(['--foo', 'bar'], {foo: {type: 'string'}});
Configuration
Params
const params = {
foo: {
type: 'string',
default: 'val',
mandatory: true,
alias: [],
conflict: [],
valid: () => {},
describe: 'Description here',
},
};
Config
const config = {
error: msg => {},
panic: msg => {},
allowUnknown: true,
no: true,
intro: 'Intro Text',
outro: 'Outro Text',
};
Help Text
You can call helpText()
after invoking argMate()
to get a CLI-friendly description of the options.
import argMate, {helpText} from 'argmate';
const argv = argMate(
process.argv.slice(2),
{
foo: {type: 'string'},
foo2: {type: 'string'},
},
{
intro: 'Introduction here',
outro: 'See you later!',
}
);
console.log(
helpText({
width: 100,
format: 'cli',
voidIntro: false,
voidOutro: false,
})
);
Please note that argMate is an OPEN open source software project.
This means that individuals making significant and valuable contributions are given commit access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
License