Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Ensure there are no trailing arguments in Commander.js

I would like to write a CLI app that takes one optional argument.

import { Command } from 'commander';
const program = new Command();
program.argument('[a]');
program.action((a) => console.log(`a = ${a}`));
program.parse();
console.log(program.args);

If I run it with 0 or 1 arguments, it works as expected. However, I can't see a clean way to check that the whole command line was consumed by arguments. I'd like to error out if there are any trailing command line arguments. What's the best way to achieve that?

$ node no-trailing-args.js    
a = undefined
[]
$ node no-trailing-args.js 1  
a = 1
[ '1' ]
$ node no-trailing-args.js 1 2
a = 1
[ '1', '2' ]
$
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

By default it is not an error to pass more arguments than declared, but you can make this an error with .allowExcessArguments(false).

program.allowExcessArguments(false);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda