Skip to content
allx edited this page Jul 25, 2022 · 5 revisions

A lazy multiple differs from a greedy multiple in that it requires you to specify the option name each time a value is passed.

Example

Lazy multiples are useful in cases where specifying an option multiple times increases its "strength" in some way. Here, we have a --verbose option which can be amplified by setting it multiple times.

const commandLineArgs = require('command-line-args')
const optionDefinitions = [
  {
    name: 'verbose',
    alias: 'v',
    type: Boolean,
    lazyMultiple: true
  }
]
const options = commandLineArgs(optionDefinitions)
console.log(options)

In application code, you could set a varying level of verbosity depending on how many times the option was set. For example, you could interpret -vvv to mean "very very verbose".

$ node example.js -vvv
{ verbose: [ true, true, true ] }