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.

How to add an optional boolean flag?

See original GitHub issue

I’m writing a little helper that renames files, but sometimes I want to know what the result of the rename is going to be before I do it. So I wanted to add an optional flag --whatif. I’m not sure how to do this in Argu.

I’ve tried

type RenameArgs =
    | WhatIf of bool

type RenameArgs =
    | WhatIf of bool option

type RenameArgs =
    | WhatIf

I’ve tried to get the result with and without a default value. But I get either the error that --whatif is missing, needs an explicit --whatif true|false or returns an null result. Could anybody help me get the syntax right?

I’ve googled this issue and found a lot of basic tutorials, but nothing with this example. I know it’s possible as there is a default --help option.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
bartelinkcommented, Jan 20, 2022

That’s not unreasonable, but DUs very much implement equality unless you ask them not to via NoEquality and participate in normal .NET conventions, so if you’re checking whether a list oriented thing (like the parsed args) contains a value, it’s not unreasonable - i.e. for a List, you’d use List.contains, and the .Contains here is the moral equivalent of that. More importantly for me, I find that it makes the processing of the argument clear and terse. (lots more examples of how to make it hairy in that repo though!)

1reaction
KenBonnycommented, Jan 20, 2022

Ok, fair warning, I’m still learning F# and the functional paradigms. I just figured out what I did wrong: I should’ve used parsedArgs.TryGetResult WhatIf instead of parsedArgs.GetResult WhatIf. The TryGetResult returns a WhatIf option which I can check. That works like a charm.

Thank you so much for telling me what the correct option was, that put me on the right path to figure things out!

This ticket may be closed. Chalk it up to me being a n00b at F#. 😳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Argparse optional boolean - python
For --foo False that means bool("False") , producing True ; all non-empty strings are true! See Why is argparse not parsing my boolean...
Read more >
Taking in optional boolean flags in a macro without expl3
I want to create a macro \verticalStep which will produce a vertical skip of fixed length, and take two optional arguments, negative and...
Read more >
Python Argparse Boolean Flag
When a Boolean flag represents false, it denotes that a certain condition is not true. To determine whether the predicate parse is “True”...
Read more >
Allow for optional boolean flags in sweeps · Issue #1700
I don't see a way for wandb to sweep optional flags like this. My workaround is to make the arg a string like...
Read more >
argparse: boolean option pitfall (beginner - YouTube
today I show how to properly handle boolean options in argparse as well as an easy mistake to avoid!
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