Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subcommand with no additional arguments? #157

Closed
kartikynwa opened this issue Jan 21, 2023 · 1 comment
Closed

Subcommand with no additional arguments? #157

kartikynwa opened this issue Jan 21, 2023 · 1 comment

Comments

@kartikynwa
Copy link

I am trying to create a program that's supposed to work like this:

$ ./app copy "some text"

$ ./app paste
some text

The paste subcommand is not supposed to take any arguments. But I can't figure out from the docs whether it is possible.

I tried something like this:

// ...

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
enum Subcommand {
    Copy(SubcommandCopy),
    Paste(SubcommandPaste),
}

// ....

#[derive(FromArgs, PartialEq, Debug)]
/// Second subcommand.
#[argh(subcommand)]
struct SubcommandPaste {};

But this doesn't work because SubcommandPaste cannot be an empty or unit struct.

Any pointers are appreciated. Thank you.

@kartikynwa
Copy link
Author

Actually, this works:

use argh::FromArgs;

#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
pub struct Args {
    #[argh(subcommand)]
    pub nested: Subcommand,
}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum Subcommand {
    Copy(SubcommandCopy),
    Paste(SubcommandPaste),
}

#[derive(FromArgs, PartialEq, Debug)]
/// copy command
#[argh(subcommand, name = "copy")]
pub struct SubcommandCopy {
    #[argh(positional)]
    /// the text to be stored
    pub input: String,
}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "paste")]
/// paste command
pub struct SubcommandPaste {}

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

No branches or pull requests

1 participant