question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle boolean array arguments better

See original GitHub issue

This is justified since Boolean types already get a bit of special treatment.

In the case I want multiple levels of verbosity, for example, the following should work:

// ./my-program -vvvv

const arg = require('arg');

const args = arg({
    '-v': [Boolean]
});

console.log(args);
/*
    {
        _: [],
        '-v': 4
    }
*/

Currently, you have to do -v -v -v -v separately, and in turn you get [true, true, true, true], which isn’t exactly elegant.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
pacocourseycommented, Dec 6, 2018

I have completed this, but I could use a few more use-cases to ensure it serves the correct purpose.

Using your example above, should ./my-program -vvvv -v output '-v': 5?

What if the flag '-vvv' exists? Should the following return case 1 or case 2?

// ./my-program -vvv -vvv

const args = arg({
    '-vvv': Boolean,
    '-v': [Boolean]
});

console.log(args);

/*
    {
        _: [],

        // case 1...
        '-vvv': true,
        '-v': 3

        // or case 2...
        '-vvv': true
    }
*/

Don’t want to miss an edge case 😅

1reaction
pacocourseycommented, Dec 10, 2018

Agreed that returning a number for [Boolean] flags seems strange. Seems like the natural solution is to return an array of booleans [true, true, true, ...] and let the user call .length on it themselves.

The core of the issue still exists though: repeating single-hyphen flags should push additional true values onto that array.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Boolean arrays — Coding for Data - 2020 edition
Now we know about function arguments, and arrays, we can simplify the list version, ... are_three is a Boolean array because it contains...
Read more >
How to improve logic to check whether 4 boolean values ...
I would make 2 improvements: 1: assign values to scenarioX with an explicit indication of boolean values used, e.g. SCENARIO_2 = true <<...
Read more >
Boolean Array Example - YouTube
Declare and instantiate a boolean array. Access and modify the array by indices. Print an array using a for-loop.
Read more >
Better Boolean Variable Names | SamanthaMing.com
Coming up with good variable names can always be a challenge. So there is some convention you can follow that makes the process...
Read more >
Solved: How to use Pass/Fail tests with an Array of Boolean in ...
Re: How to use Pass/Fail tests with an Array of Boolean in which each boolean has an expected value of true/false? You can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found