-
-
Save rust-play/3583d4b24d8cc48332a161d65e5e7297 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
fn main() { | |
let mut args: Vec<String> = env::args().collect(); | |
let mut verbose = false; | |
let mut output_file: Option<String> = None; | |
args.push("-v".to_string()); | |
args.push("-o".to_string()); | |
args.push("test.txt".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 = true; | |
} | |
"--output" | "-o" => { | |
i += 1; | |
if i < args.len() { | |
output_file = Some(args[i].clone()); | |
} else { | |
eprintln!("Error: Missing output file after --output"); | |
std::process::exit(1); | |
} | |
} | |
arg => { | |
println!("Argument: {}", arg); | |
} | |
} | |
i += 1; | |
} | |
println!("Verbose: {}", verbose); | |
println!("Output file: {:}", output_file.unwrap()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment