summaryrefslogtreecommitdiff
path: root/src/parser.zig
blob: 49094645c45d22d79fa6d298d7f29786c53c5a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const std = @import("std");

pub const ParsedCommand = struct {
    name: []const u8,
    args: ?[]const u8,
};

pub fn parseCommand(input: []const u8) ParsedCommand {
    const space_pos = std.mem.indexOfScalar(u8, input, ' ');
    if (space_pos) |pos| {
        return .{ .name = input[0..pos], .args = input[pos + 1 ..] };
    }
    return .{ .name = input, .args = null };
}

pub fn parseArgs(allocator: std.mem.Allocator, cmd_name: []const u8, args_str: ?[]const u8) ![]const []const u8 {
    var args_list = std.ArrayList([]const u8){};
    try args_list.ensureTotalCapacity(allocator, 8);
    errdefer args_list.deinit(allocator);

    try args_list.append(allocator, cmd_name);

    if (args_str) |args| {
        var it = std.mem.tokenizeScalar(u8, args, ' ');
        while (it.next()) |arg| {
            try args_list.append(allocator, arg);
        }
    }

    return args_list.toOwnedSlice(allocator);
}