Skip to content

Introduce node:util parseArgs for robust command-line argument parsing #57

Description

@aku11i

Enhancement

Currently, the phantom CLI uses manual argument parsing in its command handlers. While functional, this approach could be improved by adopting Node.js's built-in parseArgs utility from node:util.

Benefits

  • More robust parsing: Handles edge cases and complex argument combinations
  • Standard CLI conventions: Automatically supports position-independent options
  • Type safety: Better TypeScript support for parsed arguments
  • Future-proof: Easier to add new options and maintain consistency across commands

Implementation

Replace current manual parsing approach:

export async function createHandler(args: string[]): Promise<void> {
  const name = args[0];
  const openShell = args.includes("--shell");
  // ...
}

With parseArgs:

import { parseArgs } from 'node:util';

export async function createHandler(args: string[]): Promise<void> {
  const { values, positionals } = parseArgs({
    args,
    options: {
      shell: {
        type: 'boolean',
        short: 's'
      }
    },
    strict: true,
    allowPositionals: true
  });
  
  const name = positionals[0];
  const openShell = values.shell;
  // ...
}

Scope

This enhancement should be applied to all command handlers in the CLI layer for consistency.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions