Adding a single subcommand makes it mandatory? #324
-
This is not communicated clearly in documentation, and I've tried searching discussions and issues for clarification but unsuccessful. Using the derive API, I had a simple program with an arg and switch: use bpaf::Bpaf;
#[derive(Bpaf)]
#[bpaf(options, version)]
struct Args {
/// Port to bind
#[bpaf(short, long, argument("PORT"), fallback(80), display_fallback)]
port: u16,
/// Capability awareness enables the effective capability when permitted
aware: bool,
} When I added a subcommand with: struct Args {
//...
#[bpaf(external)]
inspect: Inspect,
}
#[derive(Bpaf, Debug)]
#[bpaf(command("inspect"))]
/// Show the capabilities available to the process
struct Inspect {
#[bpaf(external(set), collect, group_help("Capability sets to display"))]
sets: std::collections::HashSet<Set>,
} I found I could not run my program without the subcommand now, and the help text indicated it was mandatory. So I tried to make it optional like the docs suggest with error[E0308]: mismatched types
--> src/main.rs:19:3
|
19 | inspect: Option<Inspect>,
| ^^^^^^^ expected `Option<Inspect>`, found `Inspect`
|
= note: expected enum `Option<Inspect>`
found struct `Inspect`
help: try wrapping the expression in `Some`
|
19 | inspect: Some(inspect): Option<Inspect>,
| ++++++++++++++ + Attempted to convert that to an enum if that would have made it happy: struct Args {
//...
#[bpaf(external)]
inspect: Option<Inspect>,
}
#[derive(Bpaf, Debug)]
// Doc comment moved with command,
// otherwise a doc comment here functions like `group_help`
// replacing the default "Available commands:" section heading
enum Inspect {
/// Show the capabilities available to the process
#[bpaf(command("inspect"))]
Sets(
#[bpaf(external(set), collect, group_help("Capability sets to display"))]
std::collections::HashSet<Set>
),
} but no luck: error[E0308]: mismatched types
--> src/main.rs:19:3
|
19 | inspect: Option<Inspect>,
| ^^^^^^^ expected `Option<Inspect>`, found `Inspect`
|
= note: expected enum `Option<Inspect>`
found enum `Inspect`
help: try wrapping the expression in `Some`
|
19 | inspect: Some(inspect): Option<Inspect>,
| ++++++++++++++ + The suggestion isn't valid so ignore that. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Solved: The That section does demonstrate // Option wrapping the type is not sufficient here
// you also need to add the `optional` annotation,
// probably because it's annotated with `external`?
#[bpaf(external, optional)]
inspect: Option<Inspect>, Now it's optional 🥳
|
Beta Was this translation helpful? Give feedback.
Solved: The
optional
annotation docs don't touch on it, perhaps it's assumed you've already read the top of this docs page for derive-specific considerations?That section does demonstrate
optional
as a valid annotation for derive API, and that it is necessary in some contexts:Now it's optional 🥳