Kommandozeilenargumente lesen
Parsen Sie argv mit einem Allocator.
Kommandozeilenargumente lesen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Kommandozeilenargumente lesen“ kostenlos?
Ja — der vollständige Text von „Kommandozeilenargumente lesen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Kommandozeilenargumente lesen“?
Parsen Sie argv mit einem Allocator. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Kommandozeilenargumente lesen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Kommandozeilenargumente lesen
- Dateien und stdin lesen
- Ausgaben und Exit-Codes schreiben
- Die Binärdatei paketieren und veröffentlichen