commander.js icon indicating copy to clipboard operation
commander.js copied to clipboard

program remembers "old" arguments

Open DarthJJ opened this issue 6 years ago • 3 comments

I declare const { program } = require( 'commander' ); I add the options: program .option( '-u, --user <USER>', 'The user to interact with, please use the Discord @Tag function' ) .option( '-t, --tickets <AMOUNT>', 'The amount of tickets to add' ) .option( '-d, --date <DATE>', 'the date for the ticket measurement', client.getCurrentDate() ) .option( '-s, --start <DATE>', '' ) .option( '-e, --end <DATE>', '' );

I then call my program with the following args: <PROGRAM> -u @<username> -d <date>

I then again call my program with: <PROGRAM> -d <date> expecting only the -d flag having a value, but on closer inspection of program I see that the -u / --user object still has a value. I looked for a clear option or something related, but couldn't find one. tried it with versions 5.1.0 and 6.0.0-0

my expectation was that calling program.parse(args) would clear all "old" data

If you need more information, I will happily supply it

DarthJJ avatar Jul 16 '20 08:07 DarthJJ

I am guessing your application stays running and you are calling parse twice on the same program object?

program.parse does not clear out the old data. The way to get a clean parse each time is create a new object each time. e.g.

function doParse(args) {
   const program = new Command();
   // configure program
   program.parse(args);
}

shadowspawn avatar Jul 16 '20 09:07 shadowspawn

You are absolutely right. I am (mis-) using commander for a Discord bot, therefore the program keeps running. Your suggestion fixed my issue.
Thanks! you can close this issue ps. i am kinda new to Node, so excuse me if this is like standard practice within Node programming.

DarthJJ avatar Jul 16 '20 10:07 DarthJJ

Glad that solves your problem. 🙂 Thanks for update.

shadowspawn avatar Jul 16 '20 10:07 shadowspawn