summaryrefslogtreecommitdiff
path: root/core.zig
blob: 087de9b3af628f50ed464f38b28beefd11e06c8c (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const std = @import("std");
const builtin = @import("builtin");

const os = std.os;
const is_windows = builtin.os == Os.windows;

pub fn Param(comptime Id: type) type {
    return struct {
        const Self = this;

        id: Id,
        short: ?u8,
        long: ?[]const u8,
        index: ?usize,
        takes_value: bool,
        required: bool,

        /// Initialize a parameter.
        /// If ::name.len == 0, then it's a value parameter: "some-command value".
        /// If ::name.len == 1, then it's a short parameter: "some-command -s".
        /// If ::name.len > 1, then it's a long parameter: "some-command --long".
        pub fn init(id: Id, name: []const u8) Self {
            return {
                .id = id,
                .short = if (name.len == 1) name[0] else null,
                .long = if (name.len > 1) name else null,
                .index = null,
                .takes_value = false,
                .required = false,
            };
        }

        pub fn with(param: &const Self, comptime field_name: []const u8, value: var) Self {
            var res = *param;
            @field(res, field_name) = value;
            return res;
        }
    };
}

pub fn Arg(comptime Id: type) type {
    return struct {
        id: Id,
        value: ?[]const u8,
    };
}


pub fn args() ArgIterator {
    return ArgIterator.init();
}

pub fn Iterator(comptime Id: type) type {
    return struct {
        const Self = this;
        const Buffer = if (is_windows) [1024 * 2]u8 else void;

        windows_buffer: Buffer,
        params: Param(Id),
        args: os.ArgIterator,
        exe: []const u8,

        pub fn init(params: []const Param(Id)) Self {
            return Self {
                .params = params,
                .
            };
        }

        fn innerNext(iter: &Self) ?[]const u8 {
            //if (builtin.os == Os.windows) {
            //    return iter.args.next(allocator);
            //} else {
            //    return iter.args.nextPosix();
            //}
        }
    }
}