0Pricing
Zig Academy · 课时

读取命令行参数

使用分配器解析 argv

读取命令行参数 是 CoddyKit 上的免费 Zig Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Zig Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Zig Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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. 🎯

常见问题解答

「读取命令行参数」课时是免费的吗?

是的 — 「读取命令行参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Zig Academy 课程的其余内容,请升级到 CoddyKit PRO。 Zig Academy 课程共包含 4 节课。

「读取命令行参数」这节课中我会学到什么?

使用分配器解析 argv 你通过在浏览器中直接运行的动手代码来练习 Zig Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Zig Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Zig Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「读取命令行参数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Zig Academy 课中编写并运行代码吗?

能。每节 Zig Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 读取命令行参数
  2. 读取文件和 stdin
  3. 写入输出与退出代码
  4. 打包并发布二进制文件
← 返回 Zig Academy