fsprojects / Argu

A declarative CLI argument parser for F#
https://fsprojects.github.io/Argu
MIT License
451 stars 75 forks source link

How to parse optional arguments? #235

Closed irfancharania closed 5 months ago

irfancharania commented 5 months ago

Description

I would like to have an argument that the user can either pass in or leave out entirely. I'm unclear on how best to parse individual optional arguments.

Repro steps

I created a repository here showing the different ways I've tried to parse optional arguments: https://github.com/irfancharania/ArguTest/blob/main/Program.fs

Can someone clarify for me which of these is the correct way?

Expected behavior

I'm expecting that I can mark the argument optional as on line 26. Then use GetResult as on line 68 but have it not complain that the argument has not been supplied because it's already marked as "option".

Actual behavior

image Using GetResult generates an error instead of creating an optional object.

Related information

bartelink commented 5 months ago

GetResult(arg, defaultValue) substitutes a default (or can call a function to compute it) TryGetResult will return an Option Making the type an option is definitely not what drives the help / optionality; the Unique and Mandatoyr attributes do that

In general I'd recommend looking at the tests to see the main usage patterns.

irfancharania commented 5 months ago

I had a look at the docs and tests, and it wasn't clear to me. That's why I put together the sample repo to ask the question. I took a clue from your comment to set "GetResult(..., None)" and that gives me what I'm after. Thanks!

bartelink commented 5 months ago

GetResult(..., None) is not something I would have come up with, but if it solves your problem, that's a good start. I've never used an option as the parameter type myself (does one of the tests illustrate some behavior wrt that, I wonder; my basic understanding is that optionality at that level is based on the Mandatory or Unique attributes?)

irfancharania commented 5 months ago

I was looking at the "Optional" argument here: https://github.com/fsprojects/Argu/blob/5fb339fc4bdd20f6e9d4e0c6d860f4aabd0bc63c/tests/Argu.Tests/Tests.fs#L106

But I was a bit confused looking at the test calling it. It seems to work that way. https://github.com/fsprojects/Argu/blob/5fb339fc4bdd20f6e9d4e0c6d860f4aabd0bc63c/tests/Argu.Tests/Tests.fs#L606-L609

There wasn't a test that had "mandatory" set to false.