Unopinionated, no-frills CLI argument parser for Node.js applications
Overall
score
99%
Build a command-line argument validator that demonstrates proper error handling when parsing CLI arguments.
Your task is to implement a CLI argument parser wrapper that validates arguments and handles errors gracefully. The system should:
Define a specification for command-line arguments that includes:
--port option that accepts numbers--name option that accepts strings--verbose boolean flag-p for --port-n for --name-v for --verboseImplement error handling that catches parsing errors and returns structured error information including:
Create a parser function that:
Handle at least these error scenarios:
@generates
/**
* Parses command-line arguments according to a predefined specification.
*
* @param {string[]} argv - Array of command-line arguments to parse
* @returns {Object} Result object with either parsed arguments or error details
* - On success: { success: true, args: { port: number, name: string, verbose?: boolean, _: string[] } }
* - On error: { success: false, error: { code: string, message: string, arg?: string } }
*/
function parseArguments(argv) {
// IMPLEMENTATION HERE
}
module.exports = { parseArguments };['--port', '8080', '--name', 'myapp'] returns a success result with port: 8080 and name: 'myapp' @test['-p', '3000', '-n', 'server', '-v'] returns a success result with port: 3000, name: 'server', and verbose: true @test['--unknown', 'value'] returns an error result with an error code indicating an unknown option @test['--port'] without a value returns an error result with an error code indicating a missing required value @testProvides command-line argument parsing functionality.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-argdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10