master
1const std = @import("std");
2const mem = std.mem;
3
4pub const list = @import("clang_options_data.zig").data;
5
6pub const CliArg = struct {
7 name: []const u8,
8 syntax: Syntax,
9
10 zig_equivalent: @import("main.zig").ClangArgIterator.ZigEquivalent,
11
12 /// Prefixed by "-"
13 pd1: bool = false,
14
15 /// Prefixed by "--"
16 pd2: bool = false,
17
18 /// Prefixed by "/"
19 psl: bool = false,
20
21 pub const Syntax = union(enum) {
22 /// A flag with no values.
23 flag,
24
25 /// An option which prefixes its (single) value.
26 joined,
27
28 /// An option which is followed by its value.
29 separate,
30
31 /// An option which is either joined to its (non-empty) value, or followed by its value.
32 joined_or_separate,
33
34 /// An option which is both joined to its (first) value, and followed by its (second) value.
35 joined_and_separate,
36
37 /// An option followed by its values, which are separated by commas.
38 comma_joined,
39
40 /// An option which consumes an optional joined argument and any other remaining arguments.
41 remaining_args_joined,
42
43 /// An option which is which takes multiple (separate) arguments.
44 multi_arg: u8,
45 };
46
47 pub fn matchEql(self: CliArg, arg: []const u8) u2 {
48 if (self.pd1 and arg.len >= self.name.len + 1 and
49 mem.startsWith(u8, arg, "-") and mem.eql(u8, arg[1..], self.name))
50 {
51 return 1;
52 }
53 if (self.pd2 and arg.len >= self.name.len + 2 and
54 mem.startsWith(u8, arg, "--") and mem.eql(u8, arg[2..], self.name))
55 {
56 return 2;
57 }
58 if (self.psl and arg.len >= self.name.len + 1 and
59 mem.startsWith(u8, arg, "/") and mem.eql(u8, arg[1..], self.name))
60 {
61 return 1;
62 }
63 return 0;
64 }
65
66 pub fn matchStartsWith(self: CliArg, arg: []const u8) usize {
67 if (self.pd1 and arg.len >= self.name.len + 1 and
68 mem.startsWith(u8, arg, "-") and mem.startsWith(u8, arg[1..], self.name))
69 {
70 return self.name.len + 1;
71 }
72 if (self.pd2 and arg.len >= self.name.len + 2 and
73 mem.startsWith(u8, arg, "--") and mem.startsWith(u8, arg[2..], self.name))
74 {
75 return self.name.len + 2;
76 }
77 if (self.psl and arg.len >= self.name.len + 1 and
78 mem.startsWith(u8, arg, "/") and mem.startsWith(u8, arg[1..], self.name))
79 {
80 return self.name.len + 1;
81 }
82 return 0;
83 }
84};
85
86/// Shortcut function for initializing a `CliArg`
87pub fn flagpd1(name: []const u8) CliArg {
88 return .{
89 .name = name,
90 .syntax = .flag,
91 .zig_equivalent = .other,
92 .pd1 = true,
93 };
94}
95
96/// Shortcut function for initializing a `CliArg`
97pub fn flagpsl(name: []const u8) CliArg {
98 return .{
99 .name = name,
100 .syntax = .flag,
101 .zig_equivalent = .other,
102 .psl = true,
103 };
104}
105
106/// Shortcut function for initializing a `CliArg`
107pub fn joinpd1(name: []const u8) CliArg {
108 return .{
109 .name = name,
110 .syntax = .joined,
111 .zig_equivalent = .other,
112 .pd1 = true,
113 };
114}
115
116/// Shortcut function for initializing a `CliArg`
117pub fn jspd1(name: []const u8) CliArg {
118 return .{
119 .name = name,
120 .syntax = .joined_or_separate,
121 .zig_equivalent = .other,
122 .pd1 = true,
123 };
124}
125
126/// Shortcut function for initializing a `CliArg`
127pub fn sepd1(name: []const u8) CliArg {
128 return .{
129 .name = name,
130 .syntax = .separate,
131 .zig_equivalent = .other,
132 .pd1 = true,
133 };
134}
135
136/// Shortcut function for initializing a `CliArg`
137pub fn m(name: []const u8) CliArg {
138 return .{
139 .name = name,
140 .syntax = .flag,
141 .zig_equivalent = .m,
142 .pd1 = true,
143 };
144}