CLI Argument Parsing
For this capstone, you'll build a URL health checker — a CLI that hits a list of URLs, times the round-trip, and spits out a formatted report.
What We Are Building
$ urlcheck http://example.com http://example.org --timeout 5 --format jsonThe tool will:
- Accept one or more URLs as positional arguments
- Support
--timeoutand--formatoptions - Make HTTP requests to each URL concurrently
- Report status codes and response times
- Output in plain text or JSON format
Parsing Arguments Manually
use std::env;
struct Args { urls: Vec<String>, timeout: u64, format: OutputFormat,}
enum OutputFormat { Plain, Json,}
fn parse_args() -> Result<Args, String> { let args: Vec<String> = env::args().skip(1).collect(); let mut urls = Vec::new(); let mut timeout = 10u64; let mut format = OutputFormat::Plain;
let mut i = 0; while i < args.len() { match args[i].as_str() { "--timeout" => { i += 1; timeout = args.get(i) .ok_or("--timeout requires a value")? .parse() .map_err(|_| "Invalid timeout value")?; } "--format" => { i += 1; format = match args.get(i).map(|s| s.as_str()) { Some("json") => OutputFormat::Json, Some("plain") => OutputFormat::Plain, _ => return Err("--format must be 'plain' or 'json'".into()), }; } url if url.starts_with("http") => { urls.push(url.to_string()); } other => return Err(format!("Unknown argument: {}", other)), } i += 1; }
if urls.is_empty() { return Err("At least one URL is required".into()); }
Ok(Args { urls, timeout, format })}The runner has no command line to hand over, so this copy of parse_args takes the arguments as a slice and main feeds it test cases. Separating where the input comes from and how it's parsed is also what makes the parser unit-testable.
Hand-rolled argument parsing pulls together everything you've already met: enums for the output mode, Result for error paths, an indexed loop for stepping over the slice. Real projects reach for clap for the declarative DSL — but doing it by hand once shows you what clap is generating under its hood.
Project Structure
urlcheck/ src/ main.rs - entry point, Args and parse_args (lessons 1 and 4) checker.rs - CheckResult, check_url, check_urls (lesson 2) output.rs - OutputFormat and the formatters (lesson 3) Cargo.tomlThe OutputFormat enum above is shown next to parse_args so the snippet reads on its own, but in the project it lives in output.rs, since that's the module that matches on it. main.rs pulls it in with use output::OutputFormat;, as the complete version in lesson 4 does.
main.rs
mod checker;mod output;
use output::OutputFormat;
fn main() { let args = match parse_args() { Ok(args) => args, Err(e) => { eprintln!("Error: {}", e); eprintln!("Usage: urlcheck <url>... [--timeout N] [--format plain|json]"); std::process::exit(1); } };
if let Err(e) = run(args) { eprintln!("Error: {}", e); std::process::exit(1); }}Splitting main (process exit codes, stderr) from run (which returns Result) is a stock Rust idiom. It keeps the logic testable without dragging std::process::exit into your unit tests.