Command-line Arguments with ard/argv
The ard/argv module provides a simple way to access command-line arguments passed to your Ard program.
The argv module provides:
- Program name access to get the name of the executable
- Argument access to retrieve all command-line arguments after the program name
- Structured access via the
Argvstruct
use ard/argvuse ard/io
fn main() { let args = argv::load() io::print("Program: {args.program}")
for arg in args.arguments { io::print("Argument: {arg}") }}struct Argv
Section titled “struct Argv”Contains parsed command-line arguments.
program: Str- The name of the program/executablearguments: [Str]- List of arguments passed to the program
fn load() Argv
Section titled “fn load() Argv”Load and parse command-line arguments from the operating system.
use ard/argv
let args = argv::load()Examples
Section titled “Examples”Access Program Name and Arguments
Section titled “Access Program Name and Arguments”use ard/argvuse ard/io
fn main() { let args = argv::load() io::print("Running: {args.program}")
if args.arguments.size() == 0 { io::print("No arguments provided") } else { io::print("Got {args.arguments.size().to_str()} arguments:") for arg in args.arguments { io::print(" - {arg}") } }}Process Named Arguments
Section titled “Process Named Arguments”use ard/argvuse ard/io
fn main() { let args = argv::load()
for arg in args.arguments { match arg.starts_with("--") { true => io::print("Flag: {arg}"), false => io::print("Value: {arg}") } }}