Example 10Advanced

Advanced Argument Features v0.2.1

Demonstrates all v0.2.1 advanced argument parsing features

Overview
Learn advanced CLI argument parsing patterns

This example showcases the most advanced features of ZFish's argument parsing system:

  • Positional arguments (<FILE>, [OUTPUT])
  • Variadic positional arguments ([FILES]...)
  • Environment variable fallbacks (.env("VAR_NAME"))
  • Argument dependencies (.requires("other"))
  • Argument conflicts (.conflicts_with("other"))
  • Value delimiters (.value_delimiter(','))
  • Command aliases (.alias("short"))
Complete Code
10_arg_features_v2.rs
// Argument Features v0.2.1 Example // Demonstrates all v0.2.1 advanced argument parsing features use zfish::Color; use zfish::command::{App, Arg, Command}; fn main() { let app = App::new("myapp") .version("2.0.0") .about("Advanced CLI with v0.2.1 features") .arg( Arg::new("config") .short('c') .long("config") .about("Configuration file path") .env("MYAPP_CONFIG") // Falls back to MYAPP_CONFIG environment variable .default_value("config.toml"), ) .arg( Arg::new("verbose") .short('v') .long("verbose") .about("Enable verbose output") .takes_value(false) .conflicts_with("quiet"), // Cannot be used with --quiet ) .arg( Arg::new("quiet") .short('q') .long("quiet") .about("Suppress output") .takes_value(false) .conflicts_with("verbose"), // Cannot be used with --verbose ) .subcommand( Command::new("process") .alias("proc") // Alias: can use "proc" instead of "process" .alias("p") .about("Process files with advanced options") .arg( Arg::new("input") .index(0) // Positional argument at position 0 .about("Input file to process") .required(true), ) .arg( Arg::new("output") .short('o') .long("output") .about("Output file") .requires("format"), // Requires --format to be specified ) .arg( Arg::new("format") .short('f') .long("format") .about("Output format") .possible_values(&["json", "xml", "yaml"]), ) .arg( Arg::new("tags") .short('t') .long("tags") .about("Comma-separated tags") .value_delimiter(','), // Parses "rust,cli,tool" into ["rust", "cli", "tool"] ), ) .subcommand( Command::new("convert") .alias("cv") .about("Convert files with multiple inputs") .arg( Arg::new("files") .index(0) .last(true) // Variadic positional: captures all remaining args .about("Files to convert") .required(true), ) .arg( Arg::new("target") .long("target") .about("Target format") .required(true) .possible_values(&["pdf", "png", "svg"]), ), ); let matches = app.get_matches(); // Check config (from CLI, env var, or default) if let Some(config) = matches.value_of("config") { println!("→ Using config: {}", Color::Cyan.paint(config)); } // Check verbose/quiet if matches.is_present("verbose") { println!("→ Verbose mode enabled"); } else if matches.is_present("quiet") { println!("→ Quiet mode enabled"); } // Handle subcommands match matches.subcommand() { Some(("process", sub_matches)) | Some(("proc", sub_matches)) | Some(("p", sub_matches)) => { println!("\n{} Processing file", Color::Green.paint("āœ“")); if let Some(input) = sub_matches.value_of("input") { println!(" Input: {}", Color::Yellow.paint(input)); } if let Some(output) = sub_matches.value_of("output") { println!(" Output: {}", Color::Yellow.paint(output)); if let Some(format) = sub_matches.value_of("format") { println!(" Format: {}", Color::Yellow.paint(format)); } } if let Some(tags) = sub_matches.values_of("tags") { println!(" Tags: {}", tags.join(", ")); } } Some(("convert", sub_matches)) | Some(("cv", sub_matches)) => { println!("\n{} Converting files", Color::Green.paint("āœ“")); if let Some(files) = sub_matches.values_of("files") { println!(" Files: {}", files.join(", ")); } if let Some(target) = sub_matches.value_of("target") { println!(" Target format: {}", Color::Yellow.paint(target)); } } _ => { println!("\n{} No subcommand specified", Color::Yellow.paint("⚠")); println!("Run with --help for usage information"); } } }
Key Features Explained

1. Environment Variable Fallback

Arg::new("config") .long("config") .env("MYAPP_CONFIG") // Falls back to environment variable .default_value("config.toml")

Priority: CLI argument → Environment variable → Default value

2. Conflicting Arguments

Arg::new("verbose") .long("verbose") .conflicts_with("quiet") // Cannot be used together

Prevents --verbose and --quiet from being used simultaneously

3. Argument Dependencies

Arg::new("output") .long("output") .requires("format") // Must specify --format if --output is used

Ensures required arguments are provided together

4. Value Delimiters

Arg::new("tags") .long("tags") .value_delimiter(',') // Parses "rust,cli,tool" into array

Automatically splits values: --tags=rust,cli,tool → ["rust", "cli", "tool"]

5. Command Aliases

Command::new("process") .alias("proc") .alias("p") // Can use "process", "proc", or "p"

Create short aliases for convenience

6. Variadic Positional Arguments

Arg::new("files") .index(0) .last(true) // Captures all remaining arguments

Accepts multiple files: file1.txt file2.txt file3.txt

Usage Examples

Process a file with tags

cargo run --example 10_arg_features_v2 -- process input.txt -o output.json -f json -t rust,cli,tool
Output
→ Using config: config.toml

āœ“ Processing file
  Input: input.txt
  Output: output.json
  Format: json
  Tags: rust, cli, tool

Convert multiple files

cargo run --example 10_arg_features_v2 -- convert file1.md file2.md file3.md --target pdf
Output
→ Using config: config.toml

āœ“ Converting files
  Files: file1.md, file2.md, file3.md
  Target format: pdf

Use command alias

cargo run --example 10_arg_features_v2 -- p input.txt # "p" is alias for "process"
Output
→ Using config: config.toml

āœ“ Processing file
  Input: input.txt

Environment variable fallback

export MYAPP_CONFIG=/etc/myapp.toml cargo run --example 10_arg_features_v2 -- --help
Output
→ Using config: /etc/myapp.toml