A flexible, zero-dependency TypeScript class for parsing command-line arguments with support for multi-value flags, default values, required arguments, and rich help output.
- Define your own argument structure using
ArgSpec
- Support for required flags and multiple values
- Auto-generated terminal help with examples
- Simple access to argument values
This is a self-contained utility. Just include ArgvParser.ts
and ArgSpec.ts
in your project.
Or install from npm:
npm install parsemate
import { ArgvParser } from "parsemate";
( () => {
const parser = new ArgvParser({
folder: {
flags: ['-f', '--folder'],
description: 'Folder to scan',
required: true,
multiple: true,
},
tech: {
flags: ['-t', '--tech'],
description: 'Technologies to process',
multiple: true,
default: ['html', 'css'],
},
output: {
flags: ['-o', '--output'],
description: 'Output folder',
default: 'dist',
},
});
for (const [key, value] of parser.entries) {
console.log(`${key}:`, value);
}
})();
export type ArgSpec = {
flags: string[]; // e.g., ['-f', '--folder']
description: string;
required?: boolean;
multiple?: boolean;
default?: any;
};
-
constructor(appName: String, defs: ArgDefinition)
– Initialize with a map of argument specs. -
getArg(name: string)
– Get the parsed value for a specific argument. -
getAll()
– Get all parsed arguments as an object. -
keys
– Array of all argument names. -
values
– Array of all parsed values. -
entries
– Array of[key, value]
pairs. -
definitionList
– Array of defined arguments and specs. -
generateHelp(appName?: string)
– Returns CLI help string.
MIT – Use freely in personal or commercial projects.