file_processor/
file_processor.rs

1//! A file processing tool example using cli-command for argument parsing.
2//!
3//! This example demonstrates how to use cli-command to parse command line arguments
4//! for a file processing tool with multiple subcommands and various options.
5
6use cli_command::{parse_command_line, Command};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let cmd = parse_command_line()?;
10
11    match cmd.name.as_str() {
12        "compress" => {
13            println!("Compressing files...");
14            compress_files(&cmd)?;
15        }
16        "extract" => {
17            println!("Extracting files...");
18            extract_files(&cmd)?;
19        }
20        "list" => {
21            println!("Listing files...");
22            list_files(&cmd)?;
23        }
24        "help" | "--help" | "-h" => {
25            print_help();
26        }
27        _ => {
28            println!("Unknown command: {}", cmd.name);
29            print_help();
30        }
31    }
32
33    Ok(())
34}
35
36fn compress_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
37    let input_files = cmd.get_argument_mandatory_all("input")?;
38    let output_file = cmd.get_argument_mandatory("output")?;
39
40    let compression_level: u8 = cmd.get_argument_or_default("level", 6)?;
41    let algorithm = cmd.get_argument_or_default("algorithm", "gzip".to_string())?;
42    let recursive = cmd.contains_argument("recursive") || cmd.contains_argument("r");
43    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");
44
45    println!("Compression settings:");
46    println!("  Input files: {:?}", input_files);
47    println!("  Output file: {}", output_file);
48    println!("  Compression level: {}", compression_level);
49    println!("  Algorithm: {}", algorithm);
50    println!("  Recursive: {}", recursive);
51    println!("  Verbose: {}", verbose);
52
53    println!(
54        "\nCompressing {} files to {}...",
55        input_files.len(),
56        output_file
57    );
58
59    Ok(())
60}
61
62fn extract_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
63    let archive_file = cmd.get_argument_mandatory("archive")?;
64    let output_dir = cmd.get_argument_or_default("output-dir", ".".to_string())?;
65    let overwrite = cmd.contains_argument("overwrite");
66    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");
67
68    println!("Extraction settings:");
69    println!("  Archive file: {}", archive_file);
70    println!("  Output directory: {}", output_dir);
71    println!("  Overwrite existing: {}", overwrite);
72    println!("  Verbose: {}", verbose);
73
74    println!("\nExtracting {} to {}...", archive_file, output_dir);
75
76    Ok(())
77}
78
79fn list_files(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
80    let archive_file = cmd.get_argument_mandatory("archive")?;
81    let detailed = cmd.contains_argument("detailed") || cmd.contains_argument("l");
82    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");
83
84    println!("Listing files in: {}", archive_file);
85    println!("Detailed view: {}", detailed);
86    println!("Verbose: {}", verbose);
87
88    println!("\nArchive contents:");
89    println!("  file1.txt (1024 bytes)");
90    println!("  file2.txt (2048 bytes)");
91    println!("  subdir/file3.txt (512 bytes)");
92
93    Ok(())
94}
95
96fn print_help() {
97    println!("File Processor Tool");
98    println!();
99    println!("Usage:");
100    println!("  processor <COMMAND> [OPTIONS]");
101    println!();
102    println!("Commands:");
103    println!("  compress    Compress files into an archive");
104    println!("  extract     Extract files from an archive");
105    println!("  list        List files in an archive");
106    println!("  help        Show this help message");
107    println!();
108    println!("Compress options:");
109    println!("  --input <FILE>...        Input files to compress (required)");
110    println!("  --output <FILE>          Output archive file (required)");
111    println!("  --level <0-9>            Compression level (default: 6)");
112    println!("  --algorithm <NAME>       Compression algorithm (default: gzip)");
113    println!("  --recursive, -r          Process directories recursively");
114    println!();
115    println!("Extract options:");
116    println!("  --archive <FILE>         Archive file to extract (required)");
117    println!("  --output-dir <DIR>       Output directory (default: current)");
118    println!("  --overwrite              Overwrite existing files");
119    println!();
120    println!("List options:");
121    println!("  --archive <FILE>         Archive file to list (required)");
122    println!("  --detailed, -l           Show detailed file information");
123    println!();
124    println!("Common options:");
125    println!("  --verbose, -v            Enable verbose output");
126    println!("  --help, -h               Show this help message");
127    println!();
128    println!("Examples:");
129    println!("  processor compress --input file1.txt file2.txt --output archive.gz");
130    println!("  processor extract --archive archive.gz --output-dir ./extracted");
131    println!("  processor list --archive archive.gz --detailed");
132}