Argument Parsing

Command-line argument parsing with flags, options, and subcommands

Basic Argument Parsing
Parse command-line arguments with Args
Basic Args Example
use zfish::Args; // Parse command-line arguments let args = Args::parse(); println!("Command: {}", args.command); // Check for flags if args.has_flag("verbose") || args.has_flag("v") { println!("Verbose mode enabled"); } if args.has_flag("help") || args.has_flag("h") { println!("Help requested"); } // Get positional arguments if !args.positional.is_empty() { println!("Files: {:?}", args.positional); }
Output
Command: myapp
Verbose mode enabled
Files: ["input.txt", "output.txt"]
Options with Values
Parse options that accept values
Options Example
use zfish::Args; let args = Args::parse(); // Get option values if let Some(output) = args.get_option("output").or_else(|| args.get_option("o")) { println!("Output file: {}", output); } if let Some(config) = args.get_option("config") { println!("Config: {}", config); } if let Some(count) = args.get_option("count") { let count: usize = count.parse().unwrap(); println!("Count: {}", count); } // Usage: myapp --output file.txt --count 5
Output
Output file: file.txt\nCount: 5
Subcommands
Create CLI apps with subcommands like git
Subcommands Example
use zfish::command::{App, Command}; let app = App::new("myapp") .version("0.1.0") .about("My awesome CLI tool"); // Add subcommands let build_cmd = Command::new("build") .about("Build the project") .arg("--release", "Build in release mode"); let run_cmd = Command::new("run") .about("Run the application") .arg("--debug", "Run in debug mode"); app.command(build_cmd) .command(run_cmd) .parse(); match app.matches()?.subcommand() { Some(("build", matches)) => { let release = matches.get_flag("release"); println!("Building in {} mode", if release { "release" } else { "debug" }); } Some(("run", matches)) => { println!("Running application..."); } _ => println!("No subcommand specified"), }
Output
Building in release mode
Short Flags
Support short single-letter flags
Short Flags Example
use zfish::args::Args; let mut args = Args::new(); // Add flags with short versions args.add_flag_with_short("verbose", 'v', "Verbose output"); args.add_flag_with_short("quiet", 'q', "Quiet mode"); args.add_flag_with_short("force", 'f', "Force operation"); let matches = args.parse()?; // Can use either --verbose or -v if matches.get_flag("verbose") { println!("Verbose mode"); } // Can combine: -vf for --verbose --force if matches.get_flag("force") { println!("Force mode"); }
Output
Verbose mode\nForce mode
Help Generation
Automatic help text generation
Help Generation
use zfish::command::App; let app = App::new("myapp") .version("0.1.0") .author("Your Name <email@example.com>") .about("Description of your app") .arg("--config", "Configuration file path") .arg("--verbose", "Enable verbose output") .arg("-h, --help", "Print help information"); // Running with --help will print: app.parse();
Output
myapp 0.1.0
Your Name <email@example.com>
Description of your app

USAGE:
    myapp [OPTIONS]

OPTIONS:
    --config <CONFIG>    Configuration file path
    --verbose            Enable verbose output
    -h, --help           Print help information
    -V, --version        Print version information