[](https://badge.fury.io/js/%40wcauchois%2Fprogram-builder) [](https://circleci.com/gh/wcauchois/p
npm install @wcauchois/program-builder


This is a TypeScript-first library for building type safe command-line interfaces.
You define your arguments and keyword arguments using a fluent builder, which gives you a Program object. You can then define a main function in terms of the strongly typed arguments of that Program, and finally execute the main function against your program which will parse and provide commandline arguments.
Documentation Website | Examples | API Docs
```
npm install @wcauchois/program-builder
- Positional arguments (required and optional).
- Boolean flags (both "positive", like --unroll-loops; and "inverted", like --no-unroll-loops).--path foo.txt
- Keyword flags like . These can be strings, integers, or floats - or you can provide-h
a custom conversion function.
- Validations like ensuring that all required arguments are specified (aka: types should not lie).
- Automatic generation of help text and handling of a help argument (, --help).main
- Executes Promise-returning functions and correctly exits the process.
- Subcommands.
`typescriptA file name
const program = ProgramBuilder.newBuilder()
.arg('filename', { description: })An additional optional file name
.optionalArg('extraFilename', { description: })A count
.intFlag('--optionalCount,-c', { dest: 'optionalCount', default: null, description: })A count that is required
.intFlag('--requiredCount', { dest: 'requiredCount', description: })
.build();
function main(args: Arguments
console.log(filename is: ${args.filename}); // args.filename: stringextraFilename is: ${args.extraFilename}
console.log(); // args.extraFilename: string | nulloptionalCount is: ${args.count}
console.log(); // args.optionalCount: number | nullrequiredCount is: ${args.requiredCount}
console.log(); // args.requiredCount: number
}
program.exec(main);
`
An example invocation of this program would be:
``
$ ts-node main.ts file1.txt file2.txt -c 1 --requiredCount 2
You can also view autogenerated help text by executing the program with an -h parameter.
`
$ ts-node main.ts -h
Usage: main.ts [options]
Options:
--count, -c [count] A count
--requiredCount [requiredCount] A count that is required
``
As of January 2020, Program Builder is a very young library! That said, I think it occupies a valuable
niche offering better type safety than yargs or
commander with less verbosity than
ts-command-line.
The argument parsing library that gives me the most joy is Python's argparse
and I'm striving to create something similarly lightweight but powerful for TypeScript.
At this stage your feedback is extremely valuable, and if you have
anything to say, _please_ file an issue!