How to make a default subcommand with clap and derive

13.2k Views Asked by At

I am trying to have a subcommand launched if there is no argument that has been supplied by the user and I could not find any way to do this.

If there is no subcommand supplied the help will show up when I want some action instead to be passed.

2

There are 2 best solutions below

1
On

Based on the official clap documentation.

Modified by wrapping the Subcommand in Option, which makes it optional:

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Adds files to myapp
    Add { name: Option<String> },
}

fn main() {
    let cli = Cli::parse();

    // You can check for the existence of subcommands, and if found use their
    // matches just as you would the top level cmd
    match &cli.command {
        Some(Commands::Add { name }) => {
            println!("'myapp add' was used, name is: {:?}", name)
        }
        None => {
            println!("Default subcommand");
        }
    }
}
0
On

While it's possible to mark the subcommand as optional and use pattern matching to use None as the default command, I didn't like that it pushes that responsibility outside of the Cli.

A solution I've found was to keep the command field private, and use a public method on Cli to return it (or a default value). That way the implementation can all live inside the cli:

#[derive(Subcommand, Clone, Debug)]
pub enum Command {
    Compile,
    Format
}

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

impl Cli {
    pub fn command(&self) -> Command {
        self.command.clone().unwrap_or(Command::Compile)
    }
}