0Pricing
Zig Academy · Ders

Komut Satırı Değişkenlerini Okuma

argv'yi bir ayırıcıyla ayrıştırın.

Komut Satırı Değişkenlerini Okuma, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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

Sıkça Sorulan Sorular

“Komut Satırı Değişkenlerini Okuma” dersi ücretsiz mi?

Evet — “Komut Satırı Değişkenlerini Okuma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.

“Komut Satırı Değişkenlerini Okuma” dersinde ne öğreneceğim?

argv'yi bir ayırıcıyla ayrıştırın. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Zig Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“Komut Satırı Değişkenlerini Okuma” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Komut Satırı Değişkenlerini Okuma
  2. Dosyaları ve stdin'i Okuma
  3. Çıktı Yazma ve Çıkış Kodları
  4. İkili Dosyayı Paketleme ve Yayımlama
← Zig Academy Sayfasına Dön