npm.io
1.1.3 • Published 1 week ago

cli-vir

Licence
(MIT OR CC0-1.0)
Version
1.1.3
Deps
2
Size
60 kB
Vulns
0
Weekly
0

cli-vir

Type safe CLI argument parser.

  • Allows single (-f, -force) or double (--f, --force) dashes for both short and long flag arguments.
  • Accepts camelCase, kebab-case, or snake_case flag name variants with case insensitivity.
  • Allows restricting arg values to a set of known values.
  • Can convert truthy and falsy strings to booleans ('t', 'true', 1, 'f', 'false', 0).
  • Provides type safe values.
  • Prints man page styled documentation when parsing fails.
  • Automatically excludes the bin name or script path from raw arguments.
  • and many more features...

Reference docs: https://electrovir.github.io/cli-vir

Install

npm i cli-vir

Usage

Use parseArgs:

import {resolve} from 'node:path';
import {FlagRequirement, parseArgs} from 'cli-vir';

const myArgs = parseArgs(
    process.argv,
    {
        arg1: {
            flag: {
                valueRequirement: FlagRequirement.Blocked,
            },
        },
        arg2: {
            required: true,
            position: 0,
        },
        arg3: {
            flag: {
                aliases: ['-3'],
                allowMultiple: true,
                valueRequirement: FlagRequirement.Required,
            },
        },
    },
    {
        /** The binName for your package, if relevant. */
        binName: 'cli-vir',
        /** Your package's package.json, used for the `--version` flag. Can be `undefined`. */
        packageJsonPath: resolve(import.meta.dirname, '..', 'package.json'),
        /**
         * Make sure this `import.meta` is accessed in your top level JavaScript/TypeScript file
         * that is being executed.
         */
        importMeta: import.meta,
    },
);

console.info(myArgs.arg1); // boolean
console.info(myArgs.arg2); // string
console.info(myArgs.arg3); // string[]

Keywords