Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active March 12, 2025 19:50
Show Gist options
  • Save RandyMcMillan/8b5ec5b2c39ea43bfa372a37f5f8f41d to your computer and use it in GitHub Desktop.
Save RandyMcMillan/8b5ec5b2c39ea43bfa372a37f5f8f41d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::env;
fn main() {
let mut args: Vec<String> = env::args().collect();
let mut verbose_count = 0;
let mut output_count = 0;
let mut output_files: Vec<String> = Vec::new();
// Simulating command-line arguments (for testing)
args.push("-v".to_string());
args.push("-v".to_string());
args.push("-o".to_string());
args.push("test1.txt".to_string());
args.push("-o".to_string());
args.push("test2.txt".to_string());
args.push("extra_arg".to_string());
let mut i = 1; // Start from 1 to skip the executable path
while i < args.len() {
match args[i].as_str() {
"-v" | "--verbose" => {
verbose_count += 1;
}
"--output" | "-o" => {
i += 1;
if i < args.len() {
output_files.push(args[i].clone());
output_count += 1;
} else {
eprintln!("Error: Missing output file after --output");
std::process::exit(1);
}
}
arg => {
println!("Argument: {}", arg);
}
}
i += 1;
}
println!("Verbose count: {}", verbose_count);
println!("Output files: {:?} {} ", output_files, output_count );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment