commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Question - can f# matching handle verbs?

Open akdor1154 opened this issue 4 years ago • 1 comments

Sorry for question as issue, googled a bit and had no luck.

Is the F# matching support meant to work with verbs? I'm trying

[<Verb("upload")>]
type UploadOptions = {
    [<Value(0, Required=true, MetaName="filename")>] FileName: string;
    [<Value(1, Required=true, MetaName="URL")>] RootUrl: string;
}

[<Verb("test")>]
type OtherOptions = {
    [<Value(0, Required=false)>] Opt: string;
}

let getOpts argv : CLIOptions =
    let result = CommandLine.Parser.Default.ParseArguments<UploadOptions, OtherOptions>(argv)
    match result with
        | :? Parsed<UploadOptions> as p -> p.Value // <-- compile error here
        | :? Parsed<OtherOptions> as p -> p.Value
        | :? NotParsed<CLIOptions> as e ->
            let errs = e.Errors |> Seq.map string |> Seq.toList
            failwith <| String.Join("\n", errs)
        | _  -> failwith "bang"

But this fails to compile:

Type constraint mismatch. The type 
    'Parsed<UploadOptions>'    
is not compatible with type
    'ParserResult<obj>'

I don't have a good understanding of how C# types map to F# sorry, so apologies if I'm missing something obvious.

EDIT: Never mind, found the readme. Consider this a feature request to not need to double match against obj. :)

akdor1154 avatar Sep 13 '21 12:09 akdor1154

I found this pattern useful

let [<EntryPoint>] main args =
  Parser.Default.ParseArguments<Verb0, Verb1>(args)
                                   .MapResult(
                                     runVerb0, // Verb0 -> int
                                     runVerb1, // Verb1 -> int
                                     Seq.map <| fun e -> match e with | :? HelpVerbRequestedError -> 0
                                                                      | e -> eprintfn $"error %O{e}"; 1
                                     >> Seq.max)

nodakai avatar May 02 '23 17:05 nodakai