System.CommandLine F# 友好吗?

问题描述 投票:0回答:2

对于 F# 应用程序,我传统上使用不同的功能友好的命令行解析器,例如 ArguCommandLineParser

既然 Microsoft 已经推出了 System.CommandLine(可能会带来更好的支持和文档),它可以在 F# 中使用吗?

f# command-line-interface system.commandline
2个回答
4
投票

功能友好性是主观的,但我发现 API 对于 F# 来说相当方便:

open System.CommandLine
open System.CommandLine.Invocation

[<EntryPoint>]
let main argv =
    let option1 = Option<string> "--option1"
    let option2 = Option<string> "--option2"
    let option3 = Option<string> "--option3"
    let option4 = Option<string> "--option4"

    let command1 = Command "command 1"
    command1.AddOption option1
    command1.AddOption option2
    command1.Handler <- CommandHandler.Create (fun option1 option2 -> Operation1.run option1 option2)

    let command2 = Command "command 2"
    command2.AddOption option3
    command2.AddOption option4
    command2.Handler <- CommandHandler.Create (fun option3 option4 -> Operation1.run option3 option4)

    let rootCommand = RootCommand "My tool."
    rootCommand.AddCommand command1
    rootCommand.AddCommand command2

    rootCommand.Invoke argv

为了让代码更有趣,一些简单的助手可以提供帮助:

open System.CommandLine

let addCommand subCommand (command: Command) =
    command.AddCommand subCommand
    command

let addOption option (command: Command) =
    command.AddOption option
    command

let setHandler handler (command: Command) =
    command.Handler <- handler
    command

用法如下:

open System.CommandLine
open System.CommandLine.Invocation

[<EntryPoint>]
let main argv =
    let option1 = Option<string> "--option1"
    let option2 = Option<string> "--option2"
    let option3 = Option<string> "--option3"
    let option4 = Option<string> "--option4"

    let command1 =
        Command "command 1"
        |> addOption option1
        |> addOption option2
        |> setHandler (CommandHandler.Create (fun option1 option2 -> Operation1.run option1 option2))

    let command2 =
        Command "command 2"
        |> addOption option3
        |> addOption option4
        |> setHandler (CommandHandler.Create (fun option3 option4 -> Operation2.run option3 option4))

    let rootCommand =
        RootCommand "My tool."
        |> addCommand command1
        |> addCommand command2

    rootCommand.Invoke argv

这似乎也是一个专用的 ,用于以 F# 方式拥有

System.CommandLine

F# 可能是在库设计过程中考虑到的,尽管有一些警告,例如这个 用于命令处理程序绑定。

一般来说,该库仍处于预览阶段,因此 API 将来可能会发生重大变化。


0
投票

我发现 psfinaki 的答案非常有帮助,并根据 System.CommandLine 的更改对其进行了更新。

open System.CommandLine
open System.CommandLine.Invocation

[<EntryPoint>]
let main argv =
    
    let command1 = Command "command1"
    command1.AddAlias("com1")
    
    let exampleHandler () = printf "You ran command 1"
    
    command1.SetHandler(exampleHandler)

    let command2 = Command "command2"
    
    let argument1 = Argument<string> "nameArg"
    command2.AddArgument(argument1)
    
    let exampleHandler2 (arg) = printf $"You ran command 2, with arg {arg}"
    
    command2.SetHandler(exampleHandler2, argument1)

    let rootCommand = RootCommand "My tool."
    rootCommand.AddCommand command1
    rootCommand.AddCommand command2

    rootCommand.Invoke argv
© www.soinside.com 2019 - 2024. All rights reserved.