Lesson Details

Right above our Args struct, we’ll add a clap helper: #[clap(version)].

use clap::Parser;

/// Scaffold a new post for your blog
#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    /// The layout the post should use
    #[clap(short, long, default_value = "post")]
    layout: String,

    /// Tags to include
    #[clap(short, long = "tag")]
    tags: Vec<String>,

    /// The title of the post.
    ///
    /// If not provided, the filename will be generated
    #[clap(short = 'T', long, default_value = "A Post")]
    title: String,

    /// Should this post be published?
    #[clap(short, long, default_value = "draft")]
    status: String,

    /// Where to put the file
    #[clap(short, long, default_value = "content")]
    output_dir: String,
}
fn main() {
    let args = Args::parse();
    dbg!(args);
}

This takes the version from our Cargo.toml and uses it as the --version flag in our CLI.

If we published this CLI to crates.io or homebrew or anywhere else, our CLI version information is now in sync with the information in Cargo.toml.