F#:使用Argu时获取--help参数的异常

时间:2019-01-26 13:50:00

标签: f# argu

我是F#的新手,我正在尝试创建一个带有参数的控制台应用程序。我找到了Argu库,并一直在尝试使用一个基本示例。如果传递了--commanda--commandb参数,以下内容将达到预期效果,但是如果尝试--help,则会出现异常。

open System
open Argu

type CliArguments =
    | CommandA
    | CommandB
with 
    interface IArgParserTemplate with
        member s.Usage = 
            match s with 
            | CommandA -> "CommandA - Do something" 
            | CommandB -> "CommandB - Do something"

[<EntryPoint>]
let main argv = 

    let parser = ArgumentParser.Create<CliArguments>()
    let results = parser.Parse argv

    results.GetAllResults()
    |> List.iter (fun x ->
                    match x with
                    | CommandA -> printfn "CommandA"
                    | CommandB -> printfn "CommandB")

    0

例外是:

> .\Argu_sample.exe --help

Unhandled Exception: Argu.ArguParseException: USAGE: Argu_sample.exe [--help] [--commanda] [--commandb]

OPTIONS:

    --commanda            CommandA - Do something
    --commandb            CommandB - Do something
    --help                display this list of options.

   at Argu.ExceptionExiter.Argu-IExiter-Exit[a](String msg, ErrorCode errorCode)
   at Argu.ArgumentParser`1.Parse(FSharpOption`1 inputs, FSharpOption`1 configurationReader, FSharpOption`1 ignoreMissing, FSharpOption`1 ignoreUnrecognized, FSharpOption`1 raiseOnUsage)
   at Program.main(String[] argv) in C:\Data\FSharp\Argu-sample\Argu-sample\Program.fs:line 18

我在做什么错了?

谢谢。

使用解决方案更新代码:

基于AMieres的评论,我最终使用了try with

open System
open Argu

type CliArguments =
    | CommandA
    | CommandB
with 
    interface IArgParserTemplate with
        member s.Usage = 
            match s with 
            | CommandA -> "CommandA - Do something" 
            | CommandB -> "CommandB - Do something"

[<EntryPoint>]
let main argv = 

    let parser = ArgumentParser.Create<CliArguments>()

    try 
        let results = parser.Parse argv

        results.GetAllResults()
        |> List.iter (fun x ->
                        match x with
                        | CommandA -> printfn "CommandA"
                        | CommandB -> printfn "CommandB") 
        0
    with
    | :? ArguParseException as ex ->
        printfn "%s" ex.Message
        1
    | ex ->
        printfn "Internal Error:"
        printfn "%s" ex.Message
        2

2 个答案:

答案 0 :(得分:4)

我似乎是一个未记录的功能。 --help被视为异常,您需要使用try with处理它或提供一个处理程序。

在Github中查看示例:https://github.com/fsprojects/Argu/blob/master/samples/Argu.Samples.LS/Program.fs

在其中,解析器使用错误处理程序创建:

let errorHandler = ProcessExiter(colorizer = function ErrorCode.HelpText -> None | _ -> Some ConsoleColor.Red)
let parser = ArgumentParser.Create<LsArguments>(programName = "ls", errorHandler = errorHandler)

如有疑问,请查询代码: https://github.com/fsprojects/Argu/blob/b1569917af314dfd3b77fb79fec2a157a51324c7/src/Argu/Types.fs#L29

/// Error codes reported by Argu
type ErrorCode =
    | HelpText = 0
    | AppSettings = 1
    | CommandLine = 2
    | PostProcess = 3

答案 1 :(得分:4)

您需要提供一个错误处理程序:

let errorHandler = ProcessExiter(colorizer = function ErrorCode.HelpText -> None | _ -> Some ConsoleColor.Red)
let parser = ArgumentParser.Create<CliArguments>(errorHandler = errorHandler)