Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!