0Pricing
Zig Academy · Lesson

Reading Command-Line Arguments

Parse argv with an allocator.

Reading Command-Line Arguments is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Arguments Matter

A real CLI tool reacts to what the user types after its name. Those words are the command-line arguments, and reading them is step one. 🛠️

Zig Hands You Nothing Hidden

Other languages give you a magic argv variable. Zig stays explicit: you ask the std.process namespace for the arguments yourself.

const std = @import("std");

Arguments Need an Allocator

Decoding arguments may allocate memory, so Zig asks you for an allocator up front. No allocation happens behind your back.

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const a = gpa.allocator();

argsAlloc Gives a Slice

Call std.process.argsAlloc with your allocator. It returns a slice of strings, one per argument, that you fully own.

const args = try std.process.argsAlloc(a);

Free What You Allocated

Because you own that slice, you must release it with argsFree. A defer right after the call keeps cleanup automatic.

defer std.process.argsFree(a, args);

The First Argument Is the Program

By convention args[0] is the path to your own program, not user input. The real values you care about start at index 1.

const program = args[0];

Count How Many You Got

The slice carries its own length, so args.len tells you how many arguments arrived. You check it before reaching for any index.

if (args.len < 2) return error.MissingArgument;

Loop Over the Inputs

Iterate the user values by slicing past the program name. A for loop over args[1..] visits each argument in order.

for (args[1..]) |arg| {
    std.debug.print("{s}\n", .{arg});
}

Each Argument Is Bytes

Every argument is a []const u8, a slice of bytes. Comparing or parsing one is just normal Zig string work from here.

if (std.mem.eql(u8, arg, "--help")) {}

Turn Text into Numbers

Arguments arrive as text. To get a number, parse it with std.fmt.parseInt, which fails clearly if the text is not a valid integer.

const n = try std.fmt.parseInt(i32, args[1], 10);

An Iterator Alternative

Prefer streaming? std.process.argsWithAllocator gives an iterator you advance with next(), handy when you process arguments one at a time.

var it = try std.process.argsWithAllocator(a);
defer it.deinit();

Quick Check

Think about what sits at the front of the arguments slice.

Recap

You read arguments with argsAlloc, freed them with argsFree, skipped args[0], and parsed text into values. Your CLI now hears its user. 🎯

Frequently asked questions

Is the “Reading Command-Line Arguments” lesson free?

Yes — the full text of “Reading Command-Line Arguments” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading Command-Line Arguments”?

Parse argv with an allocator. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading Command-Line Arguments” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading Command-Line Arguments
  2. Reading Files and stdin
  3. Writing Output and Exit Codes
  4. Package and Release the Binary
← Back to Zig Academy