Skip to content

Mandatory arguments in nested subcommands #116

Closed
@b0wter

Description

@b0wter

I am trying to parse something like this:

./esp device status
./esp device status $myId
./esp device add $myId
./esp device remove $myId

To do so I am using this definition:

type DeviceIdArgs =
    | [<Mandatory>] [<MainCommand>] Id of string
    with
        interface IArgParserTemplate with
            member this.Usage =
                match this with
                | Id _ -> "Id of the device to remove"

[<RequireSubcommand>]
type DeviceArgs =
    | [<CliPrefix(CliPrefix.None)>] Status of ParseResults<DeviceIdArgs>
    | [<CliPrefix(CliPrefix.None)>] Remove of ParseResults<DeviceIdArgs>
    | [<CliPrefix(CliPrefix.None)>] Add of ParseResults<DeviceIdArgs>
    with
        interface IArgParserTemplate with
            member this.Usage =
                match this with
                | Status _ -> "lists all devices currently associated with your account"
                | Remove _ -> "removes the device with the given id from your account, use * to remove all"
                | Add _ -> "adds a new device with the given id, will return a registration token"

[<RequireSubcommand>]
type EspArgs =
    | Version
    | [<CliPrefix(CliPrefix.None)>] [<First>] Account of ParseResults<AccountArgs>
    | [<CliPrefix(CliPrefix.None)>] [<First>] Device of ParseResults<DeviceArgs>
    | [<CliPrefix(CliPrefix.None)>] [<First>] Firmware of ParseResults<FirmwareArgs>
    with
        interface IArgParserTemplate with
            member this.Usage =
                match this with
                | Version -> "echos the version of this tool"
                | Account _ -> "create a backend account, or login to one"
                | Device _ -> "list/remove/... devices from your account"
                | Firmware _ -> "upload and apply firmware"

This almost works like I want it to. I want to make sure that the user needs to supply an id with the commands. That why I used the MandatoryArgument for the id in the DeviceIdArgs.
Currently ./esp device add is a valid command.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions