gpt4 book ai didi

F#/Argu - 如何显示二级命令树的帮助而不引发异常

转载 作者:行者123 更新时间:2023-12-02 03:36:45 25 4
gpt4 key购买 nike

我有一个两级 F#/Argu 命令树。它的缩写版本如下所示:

[<CliPrefix(CliPrefix.Dash)>]
type RunContGenArgs =
| [<Unique>] [<EqualsAssignment>] [<AltCommandLine("-ql")>] MaxQueueLength of int

with
interface IArgParserTemplate with
member this.Usage =
match this with
| MaxQueueLength _ -> "max queue length."
and
[<CliPrefix(CliPrefix.None)>]
ContGenArguments =
| [<Unique>] [<AltCommandLine("run")>] RunContGen of ParseResults<RunContGenArgs>
with
interface IArgParserTemplate with
member this.Usage =
match this with
| RunContGen _ -> "run Continuous Generation."

然后我使用如下:

[<EntryPoint>]
let main argv =
let parser = ArgumentParser.Create<ContGenArguments>(programName = "ContGen.exe")
let results = parser.Parse argv

match results.GetAllResults() |> ContGenTask.tryCreate with
| Some task -> task.run()
| None ->
printfn "%s" (parser.PrintUsage())
-1

如果我运行 ContGen.exe run help 这样的命令,那么它会正确显示命令 run 的帮助。然而,它随后崩溃并出现丑陋的异常:

Unhandled Exception: Argu.ArguParseException: USAGE: ContGen.exe runcontgen [help] [-maxqueuelength=<int>]
OPTIONS:
-maxqueuelength, -ql=<int>
max queue length.
help display this list of options.
at Argu.ExceptionExiter.Argu-IExiter-Exit[a](String msg, ErrorCode errorCode) in C:\Users\eirik.tsarpalis\devel\public\Argu\src\Argu\Types.fs:line 62
at Argu.ArgumentParser\`1.Parse(FSharpOption\`1 inputs, FSharpOption\`1 configurationReader, FSharpOption\`1 ignoreMissing, FSharpOption\`1 ignoreUnrecognized, FSharpOption\`1 raiseOnUsage) in C:\Users\eirik.tsarpalis\devel\public\Argu\src\Argu\ArgumentParser.fs:line 180
at Program.main(String[] argv) in C:\GitHub\ClmFSharp\Clm\ContGen\Program.fs:line 8

如果我将 let results = parser.Parse argv 更改为 let results = parser.Parse(argv, raiseOnUsage = false),那么它不会崩溃,但不会崩溃显示任何帮助消息。然后,由于命令 run 可以在没有任何二级参数的情况下运行,因此程序会继续运行,而不是显示帮助并退出。

但是,我需要 ContGen.exe run help 只是显示帮助消息,然后退出。我怎样才能做到这一点?谢谢。

最佳答案

这是阿古的一种有点奇怪的行为;您需要提供自己的退出程序以避免在那里抛出异常。

大致如下:

type NonThrowingExiter() =
interface IExiter with
member __.Name = "Exiter" // I don't know what this is used for; I have never seen it appear anywhere
member __.Exit (msg, code) =
if code = ErrorCode.HelpText then
printfn "%s" msg
exit 0
else
printfn "%s" msg // Maybe have code to color the console output red here
exit 1

To use this, create your `ArgumentParser` like this:

let argumentParser =
Argu.ArgumentParser.Create<ContGenArguments>(helpTextMessage = "Help requested",
errorHandler = NonThrowingExiter())

(当然,您实际上不需要为此创建一个类;对象表达式也可以做到这一点。)

关于F#/Argu - 如何显示二级命令树的帮助而不引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54091873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com