Command Line Arguments
This page is under construction. Please come back later.
When running programs from the command line we have the option of supplying additional information in the form of arguments on the command line. Command line arguments take the form of strings separated by spaces. For example the following command line program invokation has three additional arguments.
$ Program.rs Fred 123 Flintstone
In our programs we can access each of these arguments using the variables
mod utils;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
println!("Number of arguments: {}", (args.len() - 1));
println!("Program Name: {}", utils::program_name().expect("program name should not be empty!"));
println!("Arg 1: {}", args[1]);
println!("Arg 2: {}", args[2]);
println!("Arg 3: {}", args[3]);
println!("Arg 4: {}", args[4]);
}
Output
$ rustc CmdLineArgs1.rs
warning: function `isCustomErrorType` should have a snake case name
--> utils.rs:41:8
|
41 | pub fn isCustomErrorType(ex:Arc<dyn std::error::Error>, which:CustomError) -> Option<Arc<dyn std::error::Error>> {
| ^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `is_custom_error_type`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `cError` should have a snake case name
--> utils.rs:42:14
|
42 | if let Some(cError) = ex.downcast_ref::<CustomError>() {
| ^^^^^^ help: convert the identifier to snake case: `c_error`
warning: 2 warnings emitted
$ ./CmdLineArgs1 Fred Barney Wilma Betty
Number of arguments: 4
Program Name: CmdLineArgs1
Arg 1: Fred
Arg 2: Barney
Arg 3: Wilma
Arg 4: Betty
$ rustc CmdLineArgs1.rs
warning: function `isCustomErrorType` should have a snake case name
--> utils.rs:41:8
|
41 | pub fn isCustomErrorType(ex:Arc<dyn std::error::Error>, which:CustomError) -> Option<Arc<dyn std::error::Error>> {
| ^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `is_custom_error_type`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `cError` should have a snake case name
--> utils.rs:42:14
|
42 | if let Some(cError) = ex.downcast_ref::<CustomError>() {
| ^^^^^^ help: convert the identifier to snake case: `c_error`
warning: 2 warnings emitted
$ ./CmdLineArgs1 Φρειδερίκος Барнеи ウィルマ 贝蒂
Number of arguments: 4
Program Name: CmdLineArgs1
Arg 1: Φρειδερίκος
Arg 2: Барнеи
Arg 3: ウィルマ
Arg 4: 贝蒂
If we want to treat a command line argument as a number we first need to convert from the string representation passed on the command line to a numeric type. This is where conversion functions come in handy. The example below illustrates how to convert command line arguments to integers.
mod utils;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let a:isize = Utils.stoiWithDefault(args[1], 0);
let b:isize = Utils.stoiWithDefault(args[2], 0);
let c:isize = a + b;
println!("{}", format!("{0:d} + {1:d} = {2:d}", a, b, c));
}
Output
$ rustc CmdLineArgs2.rs
error: unknown format trait `d`
--> CmdLineArgs2.rs:9:29
|
9 | println!("{}", format!("{0:d} + {1:d} = {2:d}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `d`
--> CmdLineArgs2.rs:9:37
|
9 | println!("{}", format!("{0:d} + {1:d} = {2:d}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `d`
--> CmdLineArgs2.rs:9:45
|
9 | println!("{}", format!("{0:d} + {1:d} = {2:d}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error[E0425]: cannot find value `Utils` in this scope
--> CmdLineArgs2.rs:6:16
|
6 | let a:isize = Utils.stoiWithDefault(args[1], 0);
| ^^^^^ not found in this scope
error[E0425]: cannot find value `Utils` in this scope
--> CmdLineArgs2.rs:7:16
|
7 | let b:isize = Utils.stoiWithDefault(args[2], 0);
| ^^^^^ not found in this scope
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0425`.
The example below converts the command line arguments to floating point values.
mod utils;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let a:f64 = Utils.stodWithDefault(args[1], 0);
let b:f64 = Utils.stodWithDefault(args[2], 0);
let c:f64 = a + b;
println!("{}", format!("{0:f} + {1:f} = {2:f}", a, b, c));
}
Output
$ rustc CmdLineArgs3.rs
error: unknown format trait `f`
--> CmdLineArgs3.rs:9:29
|
9 | println!("{}", format!("{0:f} + {1:f} = {2:f}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `f`
--> CmdLineArgs3.rs:9:37
|
9 | println!("{}", format!("{0:f} + {1:f} = {2:f}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `f`
--> CmdLineArgs3.rs:9:45
|
9 | println!("{}", format!("{0:f} + {1:f} = {2:f}", a, b, c));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error[E0425]: cannot find value `Utils` in this scope
--> CmdLineArgs3.rs:6:14
|
6 | let a:f64 = Utils.stodWithDefault(args[1], 0);
| ^^^^^ not found in this scope
error[E0425]: cannot find value `Utils` in this scope
--> CmdLineArgs3.rs:7:14
|
7 | let b:f64 = Utils.stodWithDefault(args[2], 0);
| ^^^^^ not found in this scope
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0425`.
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]