CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-arg

Unopinionated, no-frills CLI argument parser for Node.js applications

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-1/

CLI Argument Parser Error Handler

Build a command-line argument validator that demonstrates proper error handling when parsing CLI arguments.

Requirements

Your task is to implement a CLI argument parser wrapper that validates arguments and handles errors gracefully. The system should:

  1. Define a specification for command-line arguments that includes:

    • A required --port option that accepts numbers
    • A required --name option that accepts strings
    • An optional --verbose boolean flag
    • A short alias -p for --port
    • A short alias -n for --name
    • A short alias -v for --verbose
  2. Implement error handling that catches parsing errors and returns structured error information including:

    • The error code
    • A descriptive error message
    • The problematic argument (if applicable)
  3. Create a parser function that:

    • Accepts an array of command-line arguments
    • Returns a result object with either the parsed arguments or error details
    • Distinguishes between configuration errors and runtime parsing errors
  4. Handle at least these error scenarios:

    • Unknown options being passed
    • Missing required values for options
    • Invalid specification configurations

Implementation

@generates

API

/**
 * 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 };

Test Cases

  • Parsing valid arguments ['--port', '8080', '--name', 'myapp'] returns a success result with port: 8080 and name: 'myapp' @test
  • Parsing with short aliases ['-p', '3000', '-n', 'server', '-v'] returns a success result with port: 3000, name: 'server', and verbose: true @test
  • Passing an unknown option ['--unknown', 'value'] returns an error result with an error code indicating an unknown option @test
  • Passing ['--port'] without a value returns an error result with an error code indicating a missing required value @test

Dependencies { .dependencies }

arg { .dependency }

Provides command-line argument parsing functionality.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-arg

tile.json