Type-Safe CLI Argument Parser

1.0.3 · active · verified Wed Apr 22

cli-vir is a command-line interface (CLI) argument parser for Node.js, built with a strong emphasis on type safety for parsed arguments. The current stable version is 1.0.3, indicating active development with frequent patch releases addressing minor issues and dependency updates. Key differentiators include its flexible handling of flag formats (single/double dashes, short/long forms), support for case-insensitive flag names across camelCase, kebab-case, and snake_case variants, and robust options to restrict argument values to specific sets or convert truthy/falsy strings to booleans. It provides comprehensive type inference for parsed values and automatically generates man-page-style documentation for parsing failures. This library streamlines CLI development by abstracting away common parsing complexities and enhancing developer experience with strong type guarantees. It requires Node.js version 22 or higher.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to parse command-line arguments with type safety, defining flags and positional arguments, and handling multiple values for a flag. It shows the basic setup with `parseArgs` and how to access the typed results.

import { FlagRequirement, parseArgs } from 'cli-vir';

// Simulate command-line arguments for demonstration
// Example: node my-cli.js --arg1 --arg3 valueA --arg3 valueB arg2Value
const simulatedArgs = ['node', 'my-cli.js', '--arg1', '--arg3', 'valueA', '--arg3', 'valueB', 'arg2Value'];

const myArgs = parseArgs(
    simulatedArgs, // In a real CLI, use process.argv
    {
        arg1: {
            flag: {
                valueRequirement: FlagRequirement.Blocked,
                description: 'A simple boolean flag.'
            }
        },
        arg2: {
            required: true,
            position: 0,
            description: 'A required positional argument.'
        },
        arg3: {
            flag: {
                aliases: ['-3'],
                allowMultiple: true,
                valueRequirement: FlagRequirement.Required,
                description: 'A flag that can be provided multiple times.'
            }
        },
        rest: {
            position: 'rest',
            description: 'Any remaining positional arguments.'
        }
    },
    {
        binName: 'my-cli.js', // Or the actual binary name if using a 'bin' entry in package.json
        importMeta: import.meta,
    },
);

console.log('Parsed Arguments:');
console.log(`arg1: ${myArgs.arg1} (type: ${typeof myArgs.arg1})`); // boolean
console.log(`arg2: ${myArgs.arg2} (type: ${typeof myArgs.arg2})`); // string
console.log(`arg3: ${JSON.stringify(myArgs.arg3)} (type: ${typeof myArgs.arg3})`); // string[]
console.log(`rest: ${JSON.stringify(myArgs.rest)}`); // string[]

// Example: Accessing a non-existent arg will result in a TypeScript error and runtime undefined
// console.log(myArgs.nonExistentArg); // TS Error: Property 'nonExistentArg' does not exist on type '{ arg1: boolean; arg2: string; arg3: string[]; rest: string[]; }'.

view raw JSON →