{"library":"opts","title":"opts Command-Line Argument Parser","description":"`opts` is a lightweight command-line argument parser for Node.js, currently at version 2.0.2. It provides functionalities for parsing short (`-s`) and long (`--long`) options, as well as positional arguments, and automatically generates help text. A key differentiator is its minimal footprint and zero external dependencies, designed to work as a standalone JavaScript file without requiring NPM or other package managers. This makes it suitable for projects prioritizing simplicity and a small bundle size. While primarily a plain JavaScript library, it ships with TypeScript definitions for enhanced development experience. `opts` processes arguments through callbacks associated with each option, rather than returning a structured object of parsed values. Its stable nature suggests a focus on maintenance, offering a robust solution for basic to moderate CLI parsing needs.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install opts"],"cli":null},"imports":["import { parse } from 'opts';","import { Option } from 'opts';","import * as opts from 'opts';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse, Option } from 'opts';\n\nconst options: Option[] = [\n  {\n    short: 'h',\n    long: 'help',\n    description: 'Display this help message.',\n    callback: function () {\n      console.log('Usage: my-cli-tool [options] <command>');\n      process.exit(0);\n    },\n  },\n  {\n    short: 'v',\n    long: 'version',\n    description: 'Show version and exit.',\n    callback: () => {\n      console.log('my-cli-tool v1.0.0');\n      process.exit(0);\n    },\n  },\n  {\n    short: 'p',\n    long: 'port',\n    description: 'Specify the port number for the server.',\n    value: true, // Indicates that this option expects a value\n    required: false,\n    callback: (value) => {\n      if (value && typeof value === 'string') {\n        process.env.APP_PORT = value; // Store value globally or in a local state\n        console.log(`Port set to: ${value}`);\n      }\n    }\n  }\n];\n\n// Positional arguments can be defined in a second array, e.g., [{ name: 'command', required: true }]\n// For this example, we're not formally defining positional arguments but will process them heuristically.\n\nparse(options, [], true); // Parse options, no defined arguments array, enable automatic help text\n\n// Access parsed values from process.env if set by callbacks, or default\nconst serverPort = process.env.APP_PORT ?? '3000';\n\n// Simulate execution based on arguments. If --help or --version caused an exit, we wouldn't reach here.\nconst rawArgs = process.argv.slice(2);\nconst command = rawArgs.find(arg => !arg.startsWith('-') && !arg.includes('=')); // Simple heuristic for a command\n\nif (command === 'start') {\n  console.log(`Starting application server on port ${serverPort}...`);\n  // Add actual server start logic here\n} else if (command === 'stop') {\n  console.log('Stopping application...');\n} else if (command) {\n  console.error(`Error: Unknown command '${command}'`);\n  process.exit(1);\n} else {\n    console.log(`No specific command provided. Application running on default port ${serverPort}.`);\n    // Default application behavior\n}\n","lang":"typescript","description":"This example demonstrates how to define command-line options with short and long forms, descriptions, and callbacks using TypeScript, enabling automatic help generation and basic argument parsing for a CLI tool. It also shows how to handle option values and simple positional commands.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}