Skip to content

feat: support unit and struct enums - #240

Draft
the-wondersmith wants to merge 5 commits into
google:masterfrom
the-wondersmith:feat/support-unit-and-struct-enums
Draft

feat: support unit and struct enums#240
the-wondersmith wants to merge 5 commits into
google:masterfrom
the-wondersmith:feat/support-unit-and-struct-enums

Conversation

@the-wondersmith

Copy link
Copy Markdown

PR adds support for unit structs and struct-style enums as subcommands to argh::FromArgs.

Note

PR created as draft until #239 lands, as the branch is based off the-wondersmith/argh:feat/skip-field-attribute.

Specifically, this allows argh consumers to directly define simple CLIs as struct-style enums, e.x. something like netavark's plugin contract:

/// An example plugin for netavark
#[derive(Clone, Debug, argh::FromArgs)]
#[argh(help_triggers("-h", "--help", "help"))]
pub enum MyNetavarkPlugin {
    /// report the plugin's version and API version as `netavark`-compatible JSON.
    Info,

    /// create a `podman` network
    Create {
        /// the network definition to be created
        ///
        /// > Note: netavark streams this as JSON on STDIN.
        /// > This positional arg is defined so that manual
        /// > invocations (i.e., testing) are more convenient.
        #[argh(positional, from_str_fn(from_arg_or_stdin))]
        network: serde_json::Value,
    },

    /// plumb a container's veth and reserve its address(es)
    Setup {
        /// the container's network namespace path.
        #[argh(positional)]
        namespace: String,

        /// configuration of the container+network to set up.
        ///
        /// > Note: netavark streams this as JSON on STDIN.
        /// > This positional arg is defined so that manual
        /// > invocations (i.e., testing) are more convenient.
        #[argh(positional, from_str_fn(from_arg_or_stdin))]
        network: serde_json::Value,
    },

    /// release a container's reserved address(es) and unplumb its veth
    Teardown {
        /// the container's network namespace path.
        #[argh(positional)]
        namespace: String,

        /// configuration of the container+network to tear down.
        ///
        /// > Note: netavark streams this as JSON on STDIN.
        /// > This positional arg is defined so that manual
        /// > invocations (i.e., testing) are more convenient.
        #[argh(positional, from_str_fn(from_arg_or_stdin))]
        network: serde_json::Value,
    },
}

Also, subcommands that take no arguments (e.g. #157) become simpler to define:

/// An argumentless subcommand
#[derive(Debug, PartialEq, argh::FromArgs)]
#[argh(subcommand)]
struct SomeSimpleCommand;  // currently, would be required to be `SomeSimpleCommand { }`

PR includes comprehensive tests for added types/variants, test suite is green across the board (checked with cargo hack --feature-powerset test --workspace --all-targets -- --include-ignored [ref: cargo hack]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant