#[command(flatten)] on optional field makes it required
Author: smessmerCreated Aug 27, 2023Updated Sep 9, 2026
LabelsC-bugM-breaking-changeA-validatorsS-waiting-on-design
Please complete the following tasks
- I have searched the discussions
- I have searched the open and rejected issues
Rust Version
1.72
Clap Version
4.3.4
Minimal reproducible code
use clap::{Args, Parser};
fn main() {
MainArgs::parse();
}
#[derive(Parser, Debug)]
pub struct MainArgs {
#[arg(short = 'V', long)]
pub version: bool,
#[command(flatten)]
pub specific_args: Option<SpecificArgs>,
}
#[derive(Args, Debug)]
pub struct SpecificArgs {
pub positional: String,
}Steps to reproduce the bug with the above code
cargo r -- --versionActual Behaviour
error: the following required arguments were not provided:
<POSITIONAL>
Usage: test-executable --version <POSITIONAL>
For more information, try '--help'.Expected Behaviour
No error because the SpecificArgs is optional.
Additional Context
I want to use this to have a top level Parser with a few global args (e.g. --version but also some others), and I want to reuse this across different CLI tools, each with their own ConcreteArgs. The ConcreteArgs are usually written as mandatory in their corresponding #[derive(Args)] parser, but they should only be mandatory if none of the global flags are present. To do this, I made SpecificArgs optional in the #[command(flatten)] but that doesn't seem to have the intended effect.
Similar discussion: https://github.com/clap-rs/clap/discussions/4954
Debug Output
No response
Source: clap-rs/clap