Tiny Opts Parser
raw JSON →tiny-opts-parser is an extremely lightweight and minimal command-line interface (CLI) options parser for JavaScript and TypeScript applications. Currently at version 0.0.3, it focuses on providing core argument parsing functionality without the overhead or extensive feature sets found in more comprehensive libraries like Commander, Minimist, or Yargs. It takes an array of strings (typically `process.argv.slice(2)` from Node.js) and an optional configuration object, returning a structured object representing parsed arguments and options. Its design prioritizes simplicity and a small footprint, making it suitable for projects where bundle size or minimal dependency count is critical. Given its very early version number, its release cadence is likely slow and focused on stability rather than rapid feature additions, and users should anticipate a deliberate pace of development. It aims to produce an output format consistent with popular CLI parsers, simplifying migration or usage for developers familiar with other tools.
Common errors
error TypeError: parser is not a function ↓
const parser = require('tiny-opts-parser');. For ESM, use import parser from 'tiny-opts-parser';. error Argument 'myOption' is a string, expected number. ↓
const myOption = parseInt(parsedOptions.myOption, 10);. Warnings
gotcha Early-stage development: Version 0.0.3 indicates that `tiny-opts-parser` is in early development. While functional, users should be aware that future minor releases may introduce breaking changes without explicit major version bumps, as stability guarantees are typically established in higher versions. ↓
gotcha Limited feature set: As a 'tiny' parser, `tiny-opts-parser` deliberately omits advanced CLI features such as built-in command chaining, extensive type validation, or auto-generated help messages. It focuses solely on basic argument parsing. ↓
gotcha Implicit type handling: The parser might treat all option values as strings by default, even if they appear to be numbers or booleans (e.g., `--count 10` might result in `count: '10'`). Explicit type coercion may be required in application logic. ↓
gotcha Potential for dormant maintenance: The very low version number (0.0.3) and limited community activity could indicate that the project might not receive frequent updates, bug fixes, or security patches, which could be a concern for long-term projects. ↓
Install
npm install tiny-opts-parser yarn add tiny-opts-parser pnpm add tiny-opts-parser Imports
- parser wrong
import { parser } from 'tiny-opts-parser';correctimport parser from 'tiny-opts-parser'; - parser wrong
const { parser } = require('tiny-opts-parser');correctconst parser = require('tiny-opts-parser');
Quickstart
import parser from 'tiny-opts-parser';
// Simulate command line arguments as if `node myapp.js -a -bcd def xyz --abcd --foo=bar --num 123` was run
const argsFromCLI = ['-a', '-bcd', 'def', 'xyz', '--abcd', '--foo=bar', '--num', '123'];
// Parse the arguments array
const parsedOptions = parser(argsFromCLI);
console.log('Parsed Options:', parsedOptions);
/* Example output for the argsFromCLI above:
{
_: ['xyz'],
a: true,
b: true,
c: true,
d: 'def',
abcd: true,
foo: 'bar',
num: '123' // Note: '123' might be a string, not a number, depending on internal logic
}
*/
// A more practical example demonstrating access to parsed values
const myAppArgs = ['--env', 'production', '-p', '8080', 'serve', '--verbose'];
const parsedAppArgs = parser(myAppArgs);
console.log('Environment:', parsedAppArgs.env); // Expected: production
console.log('Port:', parsedAppArgs.p); // Expected: 8080
console.log('Command:', parsedAppArgs._[0]); // Expected: serve
console.log('Verbose flag present:', parsedAppArgs.verbose); // Expected: true
// Accessing an environment variable (simulated) as part of an app's logic
const apiKey = parsedAppArgs.apiKey ?? process.env.APP_API_KEY ?? 'default-key';
console.log('API Key source:', apiKey === 'default-key' ? 'default' : 'CLI/Env');