New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

argparse-ts

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argparse-ts

Modern CLI arguments parser for node.js

latest
Source
npmnpm
Version
0.9.0
Version published
Maintainers
1
Created
Source

Argument Parser for TypeScript

npm npm Coverage Status Build and test Minified Size License: MIT

Overview

Modern CLI arguments parser for node.js (TypeScript / JavaScript).

Setup

npm i argparse-ts

Usage example

import { ArgsParser } from "argparse-ts";

const parser = new ArgsParser([
  {
    name: 'container',
    description: "Container name",
    type: 'string',
  },
  {
    name: 'operations',
    description: "Operations to run",
    type: 'string',
    nargs: '+',
    choices: ['build', 'clear', 'sync', 'start', 'stop'],
  },
  {
    name: '--mode',
    description: "Run mode",
    type: 'string',
    nargs: '?',
    choices: ['dev', 'test', 'prod'],
    default: 'prod',
  },
  {
    name: '--cpu',
    description: "CPU cores count to use",
    type: 'number',
    nargs: '?',
    default: 1,
  },
  {
    name: '--use-gpu',
    description: "Use GPU flag",
    type: 'boolean',
    const: true,
    default: false,
  },
  {
    name: '--extra-services',
    alias: '-e',
    description: "Extra services to include",
    type: 'string',
    nargs: '*',
  },
]);

console.log(parser.help);

/*
Positional arguments:

    container <string>  Container name
                        Type: string (not empty)

    operations <string> <string> ...
                        Operations to run
                        Type: Array<string> (not empty)
                        Allowed values: build, clear, sync, start, stop

Options:

    --mode <string>     Run mode
                        Type: string
                        Default value: "prod"
                        Allowed values: dev, test, prod

    --cpu <number>      CPU cores count to use
                        Type: number
                        Default value: 1

    --use-gpu           Use GPU flag
                        Type: boolean
                        Default value: false

    -e <string> <string> ..., --extra-services <string> <string> ...
                        Extra services to include
                        Type: Array<string>
*/

const argv = ['main', 'clear', 'build', 'start', 'sync', '--mode', 'dev', '--use-gpu', '-e', 'logger', 'profiler', 'tester'];
const parsedArgs = parser.parse(argv);

console.log(parsedArgs.positional);
/*
{
  'container': 'main',
  'operations': ['clear', 'build', 'start', 'sync'],
}
*/

console.log(parsedArgs.options);
/*
{
  'mode': 'dev',
  'cpu': 1,
  'use-gpu': true,
  'extra-services': ['logger', 'profiler', 'tester'],
}
*/

const containerName = parsedArgs.get<string>('container');
console.log(containerName); // 'main'

const operations = parsedArgs.get<string[]>('operations');
console.log(operations); // ['clear', 'build', 'start', 'sync']

const mode = parsedArgs.get<string>('--mode');
console.log(mode); // dev

const cpuCount = parsedArgs.get<number>('--cpu');
console.log(cpuCount); // 1

const useGpu = parsedArgs.get<boolean>('--use-gpu');
console.log(useGpu); // true

Api Reference

For detailed documentation and usage examples, please refer to API documentation

Unit testing

npm i
npm run test

License

ArgParse TS is licensed under the MIT License.

Keywords

cli

FAQs

Package last updated on 17 Apr 2025

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