コマンドライン引数の読み取り
Allocatorを使ってargvを解析します
「コマンドライン引数の読み取り」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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. 🎯
よくある質問
「コマンドライン引数の読み取り」レッスンは無料ですか?
はい。「コマンドライン引数の読み取り」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「コマンドライン引数の読み取り」で何を学びますか?
Allocatorを使ってargvを解析します ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「コマンドライン引数の読み取り」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- コマンドライン引数の読み取り
- ファイルとstdinの読み取り
- 出力と終了コードの書き込み
- バイナリのパッケージ化とリリース