structstd.process.ArgIterator[src]

Cross-platform command line argument iterator.

Fields

inner: InnerType

Values

ConstantInitError[src]

Source Code

Source code
pub const InitError = InnerType.InitError

Functions

Functioninit[src]

pub fn init() ArgIterator

Initialize the args iterator. Consider using initWithAllocator() instead for cross-platform compatibility.

Source Code

Source code
pub fn init() ArgIterator {
    if (native_os == .wasi) {
        @compileError("In WASI, use initWithAllocator instead.");
    }
    if (native_os == .windows) {
        @compileError("In Windows, use initWithAllocator instead.");
    }

    return ArgIterator{ .inner = InnerType.init() };
}

FunctioninitWithAllocator[src]

pub fn initWithAllocator(allocator: Allocator) InitError!ArgIterator

You must deinitialize iterator's internal buffers by calling deinit when done.

Parameters

allocator: Allocator

Source Code

Source code
pub fn initWithAllocator(allocator: Allocator) InitError!ArgIterator {
    if (native_os == .wasi and !builtin.link_libc) {
        return ArgIterator{ .inner = try InnerType.init(allocator) };
    }
    if (native_os == .windows) {
        const cmd_line = std.os.windows.peb().ProcessParameters.CommandLine;
        const cmd_line_w = cmd_line.Buffer.?[0 .. cmd_line.Length / 2];
        return ArgIterator{ .inner = try InnerType.init(allocator, cmd_line_w) };
    }

    return ArgIterator{ .inner = InnerType.init() };
}

Functionnext[src]

pub fn next(self: *ArgIterator) ?([:0]const u8)

Get the next argument. Returns 'null' if we are at the end. Returned slice is pointing to the iterator's internal buffer. On Windows, the result is encoded as WTF-8. On other platforms, the result is an opaque sequence of bytes with no particular encoding.

Parameters

Source Code

Source code
pub fn next(self: *ArgIterator) ?([:0]const u8) {
    return self.inner.next();
}

Functionskip[src]

pub fn skip(self: *ArgIterator) bool

Parse past 1 argument without capturing it. Returns true if skipped an arg, false if we are at the end.

Parameters

Source Code

Source code
pub fn skip(self: *ArgIterator) bool {
    return self.inner.skip();
}

Functiondeinit[src]

pub fn deinit(self: *ArgIterator) void

Call this to free the iterator's internal buffer if the iterator was created with initWithAllocator function.

Parameters

Source Code

Source code
pub fn deinit(self: *ArgIterator) void {
    // Unless we're targeting WASI or Windows, this is a no-op.
    if (native_os == .wasi and !builtin.link_libc) {
        self.inner.deinit();
    }

    if (native_os == .windows) {
        self.inner.deinit();
    }
}

Source Code

Source code
pub const ArgIterator = struct {
    const InnerType = switch (native_os) {
        .windows => ArgIteratorWindows,
        .wasi => if (builtin.link_libc) ArgIteratorPosix else ArgIteratorWasi,
        else => ArgIteratorPosix,
    };

    inner: InnerType,

    /// Initialize the args iterator. Consider using initWithAllocator() instead
    /// for cross-platform compatibility.
    pub fn init() ArgIterator {
        if (native_os == .wasi) {
            @compileError("In WASI, use initWithAllocator instead.");
        }
        if (native_os == .windows) {
            @compileError("In Windows, use initWithAllocator instead.");
        }

        return ArgIterator{ .inner = InnerType.init() };
    }

    pub const InitError = InnerType.InitError;

    /// You must deinitialize iterator's internal buffers by calling `deinit` when done.
    pub fn initWithAllocator(allocator: Allocator) InitError!ArgIterator {
        if (native_os == .wasi and !builtin.link_libc) {
            return ArgIterator{ .inner = try InnerType.init(allocator) };
        }
        if (native_os == .windows) {
            const cmd_line = std.os.windows.peb().ProcessParameters.CommandLine;
            const cmd_line_w = cmd_line.Buffer.?[0 .. cmd_line.Length / 2];
            return ArgIterator{ .inner = try InnerType.init(allocator, cmd_line_w) };
        }

        return ArgIterator{ .inner = InnerType.init() };
    }

    /// Get the next argument. Returns 'null' if we are at the end.
    /// Returned slice is pointing to the iterator's internal buffer.
    /// On Windows, the result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
    /// On other platforms, the result is an opaque sequence of bytes with no particular encoding.
    pub fn next(self: *ArgIterator) ?([:0]const u8) {
        return self.inner.next();
    }

    /// Parse past 1 argument without capturing it.
    /// Returns `true` if skipped an arg, `false` if we are at the end.
    pub fn skip(self: *ArgIterator) bool {
        return self.inner.skip();
    }

    /// Call this to free the iterator's internal buffer if the iterator
    /// was created with `initWithAllocator` function.
    pub fn deinit(self: *ArgIterator) void {
        // Unless we're targeting WASI or Windows, this is a no-op.
        if (native_os == .wasi and !builtin.link_libc) {
            self.inner.deinit();
        }

        if (native_os == .windows) {
            self.inner.deinit();
        }
    }
}