Arguments and exit codes
Quick answer
A CLI receives arguments from the operating system and reports success or failure to its caller through an exit status. Keep machine-readable results on stdout and diagnostic messages on stderr.
Separate parsing from execution
args_os preserves operating-system strings, useful for paths that may not be valid UTF-8. args expects Unicode arguments. The first element conventionally names the executable; do not rely on it for security decisions. Parse flags and positional arguments into a small configuration type, then return an ExitCode from main so ordinary local values can be dropped normally. Numeric exit codes beyond success and failure are application conventions.
fn parse(s: Option<&str>) -> Result<u32, &'static str> { s.ok_or("missing count")?.parse().map_err(|_| "invalid count")}fn main() { println!("{:?}", parse(Some("12"))); println!("{:?}", parse(None));}Expected stdout (not executed):
Ok(12)Err("missing count")Test the parsing function independently from the process.
use std::process::ExitCode;fn main() -> ExitCode { match std::env::args_os().nth(1) { Some(path) => { println!("{}", path.to_string_lossy()); ExitCode::SUCCESS } None => { eprintln!("usage: demo PATH"); ExitCode::FAILURE } }}Manual local Rust/Cargo candidate; not executed. Expected behavior:
With argument sample.txt: stdout sample.txt and success; without argument: stderr usage and failure.The lossy conversion is for display, not for opening a path.
Common mistake
Do not use panic for ordinary missing user arguments.
Try it yourself
Add a parser test for an empty or nonnumeric count.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arguments and exit codes — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A CLI outputs machine-readable results and reports a usage error. Where should the error text go?
Arguments and exit codes — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn parse(s: Option<&str>) -> Result<u32, &'static str> { s.ok_or("missing count")?.parse().map_err(|_| "invalid count") } fn main() { println!("{:?}", parse(Some("12"))); println!("{:?}", parse(None)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Arguments and exit codes — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::process::ExitCode; fn main() -> ExitCode { match std::env::args_os().nth(1) { Some(path) => { println!("{}", path.to_string_lossy()); ExitCode::SUCCESS } None => { eprintln!("usage: demo PATH"); ExitCode::FAILURE } } } ``` Which outcome or interpretation best matches this example?