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

Access subcommand from Base Command. #47

Closed
amrx101 opened this issue Aug 4, 2020 · 1 comment
Closed

Access subcommand from Base Command. #47

amrx101 opened this issue Aug 4, 2020 · 1 comment

Comments

@amrx101
Copy link

amrx101 commented Aug 4, 2020

 #[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
struct TopLevel {
  #[argh(subcommand)]
  nested: MySubCommandEnum,
  /// number of connections
  #[argh(option, short = 'c', default = "1")]
  connections: usize,
}

 #[derive(FromArgs, PartialEq, Debug)]
 #[argh(subcommand)]
 enum MySubCommandEnum {
   Pub(PublishConfig),
   Sub(SubConfig),
 }

Here in we have 2 sub commands for a base command.

in main.rs

 let base = argh::from_env()

After that I want to filter out he nested command and use one appropriately.

 let sub_command = tt.nested;
println!("{:?}", sub_command);

match sub_command {
    MySubCommandEnum::Pub => {
        println!("We have to do Publish");
    },
    MySubCommandEnum::Sub => {
        println!("We have to subs");
    },
}

When I try to do something like this, I get following error expected unit struct, unit variant or constant, found tuple variant MySubCommandEnum::Pubdid you meanMySubCommandEnum::Pub`

What am I missing? Thanks in advance.

@benbrittain
Copy link
Contributor

Hi! This is actually unrelated to argh, it's related to how you use enums with match statements with Rust. You need to write something like:

match sub_command {
    MySubCommandEnum::Pub(config) => { // now you can access PublishConfig
        println!("We have to do Publish");
    },
    MySubCommandEnum::Sub(_) => { // if you want to ignore the value use a type hole
        println!("We have to subs");
    },
}

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

2 participants