Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-command-line-args

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-command-line-args

A Typescript wrapper around command-line-args with additional support for markdown usage guide generation

  • 2.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

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

Keywords

FAQs

Package last updated on 02 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc