This is a proof-of-concept to show what AI-generated code looks like.
Original readme
A modern, fluent, and strongly-typed command-line argument parser for TypeScript/JavaScript.
This library is designed to provide a seamless and type-safe experience for defining complex command-line interfaces. With a fluent builder pattern and powerful type inference, you can define your CLI's arguments and subcommands with confidence, knowing that TypeScript will catch errors at compile time.
- Strongly-Typed: Leverages advanced TypeScript features to infer the shape of your parsed arguments.
- Fluent API: A chainable, builder-style API makes defining arguments and subcommands intuitive and readable.
- Subcommands: Easily define nested commands, each with its own set of arguments.
- Help Messages: Automatically generates user-friendly help messages.
- Internationalization (i18n): Support for multiple languages in help messages, with an easy way to add new translations.
npm install argparser(Note: This package is not yet published to npm.)
Create a simple argument parser and access your arguments in a type-safe way.
import { ArgumentParser } from './src';
const parser = new ArgumentParser({ description: 'A simple example.' })
.addArgument('--name', { defaultValue: 'World', help: 'The name to greet.' })
.addArgument('files', { nargs: '+', help: 'Input files' });
async function main() {
const args = await parser.parseArgs();
// args is typed as: { name: string; files: string[] }
console.log(`Hello, ${args.name}!`);
console.log('Files to process:', args.files);
}
main();The true power of this library comes from its handling of subcommands. The parsed arguments object is a discriminated union, allowing you to safely access arguments for each subcommand.
import { ArgumentParser } from './src';
const parser = new ArgumentParser()
.addSubparsers({ dest: 'command', description: 'The command to run.' })
.addParser('install', {
builder: (p) => p.addArgument('package', { help: 'The package to install' }),
})
.addParser('fetch', {
builder: (p) => p.addArgument('--remote', { defaultValue: 'origin' }),
});
async function main() {
const args = await parser.parseArgs();
// TypeScript knows that if `command` is 'install', `package` must exist.
if (args.command === 'install') {
// args is typed as: { command: 'install'; package: string }
console.log(`Installing ${args.package}...`);
}
// And if `command` is 'fetch', `remote` must exist.
if (args.command === 'fetch') {
// args is typed as: { command: 'fetch'; remote: string }
console.log(`Fetching from remote ${args.remote}...`);
}
}
main();You can easily configure the parser to use different languages for generated help messages.
import { ArgumentParser, addTranslations } from './src';
// Add new translations for French
addTranslations({
fr: {
'usage': 'utilisation:',
'optional arguments': 'arguments optionnels',
'show this help message and exit': "afficher ce message d'aide et quitter",
},
});
const parser = new ArgumentParser({
locale: 'fr',
prog: 'mon-app',
description: 'Un exemple de description.'
});
parser.addArgument('--input', { help: 'le fichier d\'entrée' });
parser.formatHelp(); // Will be in French