What is ts-command-line-args?
The ts-command-line-args package is a TypeScript library for parsing command-line arguments. It provides a type-safe way to define and parse command-line arguments, making it easier to handle user inputs in a structured and predictable manner.
What are ts-command-line-args's main functionalities?
Basic Argument Parsing
This feature allows you to define and parse basic command-line arguments. The code sample demonstrates how to define a simple interface for the expected arguments and parse them using the ts-command-line-args package.
const commandLineArgs = require('ts-command-line-args');
interface IArguments {
name: string;
age: number;
}
const args = commandLineArgs<IArguments>({
name: { type: String, alias: 'n', description: 'Your name' },
age: { type: Number, alias: 'a', description: 'Your age' }
});
console.log(`Name: ${args.name}, Age: ${args.age}`);
Optional Arguments
This feature allows you to define optional command-line arguments. The code sample shows how to define an optional age argument and handle its absence gracefully.
const commandLineArgs = require('ts-command-line-args');
interface IArguments {
name: string;
age?: number;
}
const args = commandLineArgs<IArguments>({
name: { type: String, alias: 'n', description: 'Your name' },
age: { type: Number, alias: 'a', description: 'Your age', optional: true }
});
console.log(`Name: ${args.name}, Age: ${args.age ? args.age : 'Not provided'}`);
Default Values
This feature allows you to set default values for command-line arguments. The code sample demonstrates how to set a default age value if the user does not provide one.
const commandLineArgs = require('ts-command-line-args');
interface IArguments {
name: string;
age: number;
}
const args = commandLineArgs<IArguments>({
name: { type: String, alias: 'n', description: 'Your name' },
age: { type: Number, alias: 'a', description: 'Your age', defaultValue: 30 }
});
console.log(`Name: ${args.name}, Age: ${args.age}`);
Help Text Generation
This feature allows you to generate help text for your command-line application. The code sample shows how to define a help argument and generate a help message when the user requests it.
const commandLineArgs = require('ts-command-line-args');
interface IArguments {
name: string;
age: number;
help?: boolean;
}
const args = commandLineArgs<IArguments>({
name: { type: String, alias: 'n', description: 'Your name' },
age: { type: Number, alias: 'a', description: 'Your age' },
help: { type: Boolean, alias: 'h', description: 'Display help message', optional: true }
}, {
helpArg: 'help'
});
if (args.help) {
console.log(commandLineArgs.generateHelp());
} else {
console.log(`Name: ${args.name}, Age: ${args.age}`);
}
Other packages similar to ts-command-line-args
yargs
Yargs is a popular command-line argument parsing library for Node.js. It provides a rich set of features for parsing arguments, generating help text, and handling complex command structures. Compared to ts-command-line-args, yargs is more feature-rich but may require additional type definitions for TypeScript users.
commander
Commander is another widely-used library for building command-line interfaces in Node.js. It offers a simple and intuitive API for defining commands, options, and arguments. While it is not as type-safe as ts-command-line-args, it is very easy to use and integrates well with JavaScript and TypeScript projects.
minimist
Minimist is a lightweight library for parsing command-line arguments. It is very simple and fast, making it suitable for small projects or scripts. However, it lacks the advanced features and type safety provided by ts-command-line-args.