How to add an optional boolean flag?
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!)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 ofparsedArgs.GetResult WhatIf
. TheTryGetResult
returns aWhatIf 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#. 😳