Loading pageâŚ
Rust walkthroughs
Loading pageâŚ
clap::Command::arg_required_else_help and disable_help_flag for help display control?arg_required_else_help automatically displays help and exits when no arguments are provided, while disable_help_flag removes the automatic -h/--help flag entirelyâtwo orthogonal controls for different help behavior scenarios. arg_required_else_help is useful for CLI tools that require at least one argument to function, ensuring users see helpful guidance instead of cryptic errors. disable_help_flag is useful when you want to handle help yourself or when -h/--help conflicts with your application's flags. Both affect help display but serve distinct purposes: one controls what happens with no input, the other controls whether help flags exist at all.
use clap::{Command, Arg};
fn basic_arg_required_else_help() {
let app = Command::new("myapp")
.arg_required_else_help(true)
.subcommand(Command::new("init"))
.subcommand(Command::new("build"));
// Running with no arguments:
// $ myapp
// Prints help and exits with status 0
// Running with arguments:
// $ myapp init
// Executes the init subcommand
}arg_required_else_help shows help when the user provides no arguments.
use clap::{Command, Arg};
fn basic_disable_help_flag() {
let app = Command::new("myapp")
.disable_help_flag(true)
.subcommand(Command::new("init"))
.subcommand(Command::new("build"));
// This removes -h and --help flags
// $ myapp --help
// Error: unexpected argument '--help' found
// $ myapp -h
// Error: unexpected argument '-h' found
// No automatic help flags exist
}disable_help_flag prevents clap from adding -h/--help flags automatically.
use clap::{Command, Arg};
fn default_behavior() {
let app = Command::new("myapp")
.subcommand(Command::new("init"))
.subcommand(Command::new("build"));
// Default behavior:
// 1. -h and --help are automatically added (help flags present)
// 2. No arguments: proceeds with empty matches (no help shown)
// $ myapp
// No error, no help - program continues with empty args
// $ myapp --help
// Prints help and exits
}By default, clap adds help flags but doesn't show help for empty arguments.
use clap::{Command, Arg};
fn use_case_subcommands() {
// CLI with subcommands: user must specify which subcommand
let app = Command::new("git")
.arg_required_else_help(true)
.subcommand(Command::new("clone").about("Clone a repository"))
.subcommand(Command::new("commit").about("Record changes"))
.subcommand(Command::new("push").about("Update remote"));
// $ git
// git 0.1
//
// Commands:
// clone Clone a repository
// commit Record changes
// push Update remote
//
// Usage: git [COMMAND]
// User sees available commands instead of:
// "error: a subcommand is required"
}
fn use_case_required_files() {
// Tool that requires input file(s)
let app = Command::new("cat")
.arg_required_else_help(true)
.arg(
Arg::new("file")
.required(false) // Not marked required
.action(clap::ArgAction::Append)
);
// $ cat
// Prints help (shows file argument usage)
// Without arg_required_else_help:
// $ cat
// Would run with empty file list, probably error later
}arg_required_else_help is ideal for CLIs where doing nothing isn't useful.
use clap::{Command, Arg};
fn use_case_custom_help() {
// You want to implement your own help behavior
let app = Command::new("myapp")
.disable_help_flag(true)
.arg(
Arg::new("help")
.short('h')
.long("help")
.action(clap::ArgAction::SetTrue)
);
// Now -h/--help are YOUR flags, not clap's
// You control the help output format
let matches = app.get_matches_from(&["myapp", "--help"]);
if matches.get_flag("help") {
// Custom help logic here
println!("My custom help message");
}
}
fn use_case_conflicting_flags() {
// Your app uses -h for something else (like "host")
let app = Command::new("myapp")
.disable_help_flag(true)
.arg(
Arg::new("host")
.short('h') // -h now means host, not help
.long("host")
.value_name("HOST")
)
.arg(
Arg::new("help")
.long("help") // Only long form for help
.action(clap::ArgAction::SetTrue)
);
// $ myapp -h localhost
// Uses -h for host
// $ myapp --help
// Shows help (via --help only)
}disable_help_flag is for custom help handling or flag conflicts.
use clap::{Command, Arg};
fn combined_usage() {
let app = Command::new("myapp")
.arg_required_else_help(true) // Show help on no args
.disable_help_flag(true) // But remove -h/--help flags
.arg(
Arg::new("config")
.short('c')
.long("config")
);
// $ myapp
// Prints help (arg_required_else_help)
// $ myapp --help
// Error: unexpected argument '--help' found (disable_help_flag)
// This combination is unusual - shows help automatically
// but doesn't let user request help explicitly
}Both can be combined, though this specific combination is uncommon.
use clap::{Command, Arg};
fn compare_with_required() {
// Using arg_required_else_help:
let app1 = Command::new("myapp")
.arg_required_else_help(true)
.arg(Arg::new("file"));
// $ myapp
// Prints help and exits cleanly
// Using required argument:
let app2 = Command::new("myapp")
.arg(
Arg::new("file")
.required(true)
);
// $ myapp
// error: the following required arguments were not provided:
// <file>
//
// Usage: myapp <file>
//
// For more information, try '--help'.
// arg_required_else_help is friendlier: shows full help
// required shows error message, which is less helpful
}arg_required_else_help shows full help; required arguments show error messages.
use clap::{Command, Arg};
fn help_message_comparison() {
// With arg_required_else_help (no args):
let app1 = Command::new("myapp")
.arg_required_else_help(true)
.about("My awesome application")
.arg(Arg::new("input").short('i'))
.arg(Arg::new("output").short('o'));
// $ myapp
// myapp 0.1
// My awesome application
//
// Usage: myapp [OPTIONS]
//
// Options:
// -i <input>
// -o <output>
// -h, --help Print help
// With required argument missing:
let app2 = Command::new("myapp")
.arg(Arg::new("input").short('i').required(true));
// $ myapp
// error: the following required arguments were not provided:
// -i <input>
//
// Usage: myapp -i <input>
//
// For more information, try '--help'.
}arg_required_else_help shows full help; missing required arguments show partial error.
use clap::{Command, Arg};
fn compare_subcommand_required() {
// subcommand_required: requires a subcommand, shows error if missing
let app1 = Command::new("git")
.subcommand_required(true)
.subcommand(Command::new("clone"))
.subcommand(Command::new("commit"));
// $ git
// error: 'git' requires a subcommand but one was not provided
//
// Usage: git <COMMAND>
//
// For more information, try '--help'.
// arg_required_else_help: shows help if no args
let app2 = Command::new("git")
.arg_required_else_help(true)
.subcommand(Command::new("clone"))
.subcommand(Command::new("commit"));
// $ git
// git
//
// Commands:
// clone
// commit
//
// Usage: git <COMMAND>
}subcommand_required shows error; arg_required_else_help shows help.
use clap::{Command, Arg};
fn disable_help_options() {
// disable_help_flag: removes -h/--help
let app1 = Command::new("myapp")
.disable_help_flag(true)
.subcommand(Command::new("sub1"));
// $ myapp --help
// error: unexpected argument '--help' found
// disable_help_subcommand: removes 'help' subcommand
let app2 = Command::new("myapp")
.disable_help_subcommand(true)
.subcommand(Command::new("sub1"));
// $ myapp help sub1
// error: unrecognized subcommand 'help'
// Both can be combined:
let app3 = Command::new("myapp")
.disable_help_flag(true)
.disable_help_subcommand(true)
.subcommand(Command::new("sub1"));
// No help mechanism at all
}disable_help_flag removes -h/--help; disable_help_subcommand removes the help subcommand.
use clap::{Command, Arg};
fn custom_help() {
let app = Command::new("myapp")
.disable_help_flag(true)
.disable_help_subcommand(true)
.arg(
Arg::new("help")
.long("help")
.action(clap::ArgAction::SetTrue)
)
.arg(
Arg::new("verbose")
.long("verbose")
.short('v')
);
let matches = app.clone().get_matches();
if matches.get_flag("help") {
// Custom help output
println!("MY APPLICATION - Custom Help");
println!("");
println!("Usage: myapp [OPTIONS]");
println!("");
println!("Options:");
println!(" --help Show this help message");
println!(" --verbose Enable verbose output");
return;
}
// Normal program execution
if matches.get_flag("verbose") {
println!("Verbose mode enabled");
}
}Disable the built-in help and implement your own with full control.
use clap::{Command, Arg};
fn exit_status() {
let app = Command::new("myapp")
.arg_required_else_help(true);
// When no args provided:
// - Prints help to stdout
// - Exits with status 0 (success)
// This is friendlier than error exit codes
// User can redirect help to file: $ myapp > usage.txt
// Contrast with required args missing:
// - Prints error to stderr
// - Exits with status 1 (or 2 for clap errors)
}arg_required_else_help exits with status 0; errors exit with non-zero status.
use clap::{Command, Arg};
fn with_version() {
let app = Command::new("myapp")
.version("1.0.0")
.arg_required_else_help(true);
// $ myapp
// Shows help (because no args)
// $ myapp --version
// myapp 1.0.0
// Even with arg_required_else_help, -V/--version still works
// Version flags are separate from help flags
}Version flags work independently of arg_required_else_help.
use clap::{Command, Arg};
fn disable_version() {
let app = Command::new("myapp")
.version("1.0.0")
.disable_version_flag(true);
// $ myapp --version
// error: unexpected argument '--version' found
// Similar to disable_help_flag but for -V/--version
// Useful when -V conflicts with your flags
}disable_version_flag is the version equivalent of disable_help_flag.
use clap::{Command, Arg};
fn manual_check() {
let app = Command::new("myapp")
.subcommand(Command::new("init"))
.subcommand(Command::new("build"));
let matches = app.clone().get_matches();
// Manual check for no arguments
if matches.subcommand_name().is_none() {
// Could print help manually
println!("No subcommand provided. Available commands:");
println!(" init Initialize project");
println!(" build Build project");
println!();
println!("Run 'myapp --help' for more information.");
return;
}
// But arg_required_else_help is cleaner:
// Just add .arg_required_else_help(true) instead
}You could check manually, but arg_required_else_help is simpler.
use clap::{Command, Arg};
fn subcommand_effect() {
let app = Command::new("myapp")
.arg_required_else_help(true)
.subcommand(
Command::new("init")
.arg(Arg::new("force").short('f'))
);
// arg_required_else_help applies to the root command
// $ myapp
// Shows help for 'myapp'
// $ myapp init
// Runs init subcommand (no args needed for subcommand)
// If init needs arg_required_else_help:
let app2 = Command::new("myapp")
.arg_required_else_help(true)
.subcommand(
Command::new("init")
.arg_required_else_help(true) // Separate setting
.arg(Arg::new("name").required(false))
);
// $ myapp init
// Shows help for 'init' subcommand
}arg_required_else_help must be set on each command/subcommand independently.
use clap::{Command, Arg};
fn git_like_cli() {
let app = Command::new("mygit")
.version("1.0")
.arg_required_else_help(true) // Show help if no subcommand
.subcommand_required(true) // Require a subcommand
.subcommand(
Command::new("clone")
.about("Clone a repository")
.arg(Arg::new("url").required(true))
)
.subcommand(
Command::new("commit")
.about("Record changes to the repository")
.arg(Arg::new("message").short('m').required(true))
)
.subcommand(
Command::new("push")
.about("Update remote refs")
.arg(Arg::new("remote"))
);
// $ mygit
// Shows full help with all subcommands
// $ mygit clone
// error: the following required arguments were not provided:
// <url>
// $ mygit clone https://example.com/repo.git
// Executes clone
}A well-designed CLI uses arg_required_else_help for good user experience.
use clap::{Command, Arg};
fn app_with_host_flag() {
let app = Command::new("server")
.disable_help_flag(true) // Disable built-in -h/--help
.arg(
Arg::new("host")
.short('h') // -h for host
.long("host")
.default_value("localhost")
)
.arg(
Arg::new("port")
.short('p')
.long("port")
.default_value("8080")
)
.arg(
Arg::new("help")
.long("help") // --help for help (no short)
.action(clap::ArgAction::SetTrue)
);
let matches = app.get_matches();
if matches.get_flag("help") {
println!("Usage: server [OPTIONS]");
println!("");
println!("Options:");
println!(" -h, --host <HOST> Server host [default: localhost]");
println!(" -p, --port <PORT> Server port [default: 8080]");
println!(" --help Show this help");
return;
}
let host = matches.get_one::<String>("host").unwrap();
let port = matches.get_one::<String>("port").unwrap();
println!("Starting server on {}:{}", host, port);
}When your app needs -h for something else, disable the help flag and implement your own.
Key differences:
// arg_required_else_help: Controls behavior with NO arguments
// - Shows help when user provides nothing
// - User-friendly for CLIs that need input
// - Still allows -h/--help flags
// disable_help_flag: Controls existence of help FLAGS
// - Removes -h and --help from flags
// - --help becomes an error (unexpected argument)
// - Doesn't affect what happens with no arguments
// They control different things:
// - arg_required_else_help: What happens on empty input
// - disable_help_flag: Whether help flags existWhen to use each:
use clap::{Command, Arg};
// Use arg_required_else_help when:
// - Your CLI needs at least one argument to do anything useful
// - You want to show helpful info instead of errors
// - Common for CLIs with subcommands
let app = Command::new("tool")
.arg_required_else_help(true)
.subcommand(Command::new("init"))
.subcommand(Command::new("build"));
// Use disable_help_flag when:
// - -h conflicts with your application's flags
// - You want custom help output
// - You're embedding clap in a larger application
let app = Command::new("tool")
.disable_help_flag(true)
.arg(Arg::new("host").short('h')) // Use -h for host
.arg(Arg::new("help").long("help").action(clap::ArgAction::SetTrue));
// Can combine both:
let app = Command::new("tool")
.arg_required_else_help(true) // Show help on empty
.disable_help_flag(true); // But don't add -h/--help flagsThe fundamental distinction: arg_required_else_help affects the empty-argument caseâwhat happens when the user just runs your binary with nothing else. disable_help_flag affects the help mechanism itselfâwhether -h/--help flags exist at all. These are orthogonal: you can show help on empty arguments while still having help flags (default), show help on empty while removing flags (unusual), not show help on empty but have flags (default), or neither. Choose arg_required_else_help for better UX on empty input, and disable_help_flag when you need custom help handling or -h conflicts with your flags.