{"library":"option-parser","title":"Command-line Option Parser","description":"OptionParser is a JavaScript library designed for parsing command-line options, mimicking the functionality of POSIX `getopt`. It supports both short (`-x`) and long (`--long`) options, including combined short options (`-xxxyxxz`), and handles both required and optional argument values (e.g., `-x=Value`, `--long Value`). Key features include nearly automatic help message generation, flexible option handling through callbacks or direct access to option objects, and the ability to return any unparsed arguments. As of version 1.0.2, the library appears to be in a maintenance state, with its last update occurring in August 2021. While robust for its intended use, its release cadence is low, distinguishing it from more actively developed, modern CLI parsing solutions that may offer broader ecosystem support or different API paradigms.","language":"javascript","status":"maintenance","last_verified":"Tue Apr 21","install":{"commands":["npm install option-parser"],"cli":null},"imports":["const OptionParser = require('option-parser');","parser.addOption('h', 'help', 'Display help')","const unparsedArgs = parser.parse();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const OptionParser = require('option-parser');\n\n// Simulate command line arguments for testing\n// In a real application, these would come directly from `process.argv`\nconst originalArgv = process.argv;\nprocess.argv = ['node', 'script.js', '-h', '--input', 'my_file.txt', '-f', '--debug=10', 'extra-arg1', 'extra-arg2'];\n\ntry {\n    const parser = new OptionParser();\n\n    let flagWasSet = false;\n    let inputFile = '/dev/stdin';\n    let debugLevel = 0;\n\n    // Add a standard help option\n    parser.addOption('h', 'help', 'Display this help message')\n        .action(() => {\n            console.log(parser.help());\n            process.exit(0);\n        });\n\n    // Toggle a flag with a callback\n    parser.addOption('f', null, 'Toggle a flag')\n        .action(function () {\n            flagWasSet = true;\n        });\n\n    // Pass a required value\n    parser.addOption('i', 'input', 'Specify an input file')\n        .argument('FILE')\n        .action(function (value) {\n            inputFile = value;\n        });\n\n    // Optional value using the returned option object\n    const debugOption = parser.addOption(null, 'debug',\n         'Sets the debug level (default is 5 if set without value)')\n        .argument('Level', false); // 'false' makes the argument optional\n\n    // Parse the command line options from process.argv\n    const unparsed = parser.parse();\n\n    // Retrieve debug level after parsing\n    if (debugOption.count()) {\n        debugLevel = debugOption.value() ? parseInt(debugOption.value(), 10) : 5;\n    }\n\n    console.log('--- Parsing Results ---');\n    console.log(`Help requested (-h/--help): ${parser.getOption('h').count() > 0}`);\n    console.log(`Flag was set (-f): ${flagWasSet}`);\n    console.log(`Input file (-i/--input): ${inputFile}`);\n    console.log(`Debug level (--debug): ${debugLevel}`);\n    console.log(`Unparsed arguments: ${JSON.stringify(unparsed)}`);\n    console.log('-----------------------');\n\n    // Optionally display help if not explicitly requested\n    if (parser.getOption('h').count() === 0) {\n        console.log('\\n--- Generated Help (truncated) ---');\n        console.log(parser.help().split('\\n').slice(0, 5).join('\\n')); // Show first 5 lines\n        console.log('...');\n    }\n\n} catch (error) {\n    console.error('An error occurred during parsing:', error.message);\n} finally {\n    // Restore original process.argv to avoid side effects in tests/other scripts\n    process.argv = originalArgv;\n}","lang":"javascript","description":"This quickstart demonstrates how to initialize the OptionParser, define various types of options (help, flag, required argument, optional argument), and parse simulated command-line input. It shows how to access parsed values through callbacks and option objects, and how to retrieve unparsed arguments.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}