简体   繁体   中英

F# library to parse nested command line arguments

I have an app that is running a task with the specified parameters. It is running 1 task at a time.

type MagicOptions = {
  Param1: string
  Param2: string
}

type NoMagicOptions = {
  Param3: string
  Param4: string
}

app.exe -task magic -param1 val -param2 val
app.exe -task no-magic -param3 val -param4 val

or it can be:
app.exe magic -param1 val -param2 val
app.exe no-magic -param3 val -param4 val

Whenever the user specifies the task type, it is expected to parse exact params described by Magic/NoMagic types.

I've tried Argu and expected to use DUs, but it is not supported.

Is there a ready-to-use solution?

I think that subcommands in Argu 3.0 are, in fact, exactly what you are asking for.

The example (from the docs, with help removed) defines a git command that can be used as git clean or git commit with different options for these two represented. The choice is represented by a DU:

[<CliPrefix(CliPrefix.Dash)>]
type CleanArgs =
    | D
    | F
    | X

and CommitArgs =
    | Amend
    | [<AltCommandLine("-p")>] Patch
    | [<AltCommandLine("-m")>] Message of msg:string

and GitArgs =
    | Version
    | [<AltCommandLine("-v")>] Verbose
    | [<CliPrefix(CliPrefix.None)>] Clean of ParseResults<CleanArgs>
    | [<CliPrefix(CliPrefix.None)>] Commit of ParseResults<CommitArgs>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM