Yargs argv object is not registering specified command and is treating it as file

39 Views Asked by At

Im trying to build a CLI tool with yargs and I have had a challenge with my tool not registering the command I have specified "summarize" and is instead treating it as a file that cannot be located when I run commands when testing

this is my cli.js file

#!/usr/bin/env node
const yargs = require('yargs');
const { processFile } = require('./script');

// Define command line options and usage
const argv = yargs
  .command(
    'summarize',
    'Summarize data',
    (yargs) => {
      yargs
        .option(
          'input', {
            alias: 'i',
            describe: 'Input CSV file containing data to be processed',
            default: 'input.csv',
            type: 'string'
          }
        )
        .option(
          'output', {
            alias: 'o',
            describe: 'output CSV file containing summarized data',
            default: 'output.csv',
            type: 'string'
          });
    }
  )
  .help()
  .argv;

const command = argv._[0];

// If no command is specified, show help
if (!command) {
  yargs.showHelp();
} else {
  // Execute the command
  if (command === 'summarize') {
    processFile(argv.input, argv.output);
  } else {
    console.error(`Invalid command: ${command}. Use --help for usage.`);
  }
}

command ran

debt-summary-tool summarize --input input.csv --output output.csv

when I check the argv object this is how it looks

{
  _: [ 'summarize' ],
  input: 'input.csv',
  i: 'input.csv',
  output: 'output.csv',
  o: 'output.csv',
  '$0': 'debt-summary-tool'
}

The error encountered

node:events:495
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open 'summarize'
Emitted 'error' event on Interface instance at:
    at ReadStream.onerror (node:internal/readline/interface:246:10)
    at ReadStream.emit (node:events:517:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'summarize'
}

0

There are 0 best solutions below