Skip to content

Default value for an option is always there even if flag isn't used  #1394

@simonwep

Description

@simonwep

First of all, great library!

Consider this code:

const {program} = require('commander');

program
    .option('-p, --pizza [type]', 'Pizza name', 'pepperoni')
    .parse(process.argv)


console.log({pizza: program.pizza});

Usage:

$ script.js 
# Output: { pizza: 'pepperoni' }

$ script.js --pizza
# Output: { pizza: 'pepperoni' }

$ script.js --pizza salami
# Output: { pizza: 'salami' }

Is there a way to see if the --pizza flag is actually used? Currently the default-value is always present and used, even if the flag itself is not even present in the arguments. This makes it impossible to pre-process values and making the flag optional as at this point the flag is neither optional or mandatory, it is always there, even if not specified.

Related: #440 #1036

Do I miss something or how can I achieve that using commanderjs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions