Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The simplest way to use this library is to just call the `clap.parse` function.

```zig
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

// First we specify what parameters our program can take.
Expand Down Expand Up @@ -109,7 +109,7 @@ if you want some other mapping.

```zig
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

// First we specify what parameters our program can take.
Expand Down Expand Up @@ -190,7 +190,7 @@ const main_params = clap.parseParamsComptime(
const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);

pub fn main() !void {
var gpa_state = std.heap.GeneralPurposeAllocator(.{}){};
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

Expand Down Expand Up @@ -347,7 +347,7 @@ is passed to `help` to control how the help message is printed.

```zig
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

const params = comptime clap.parseParamsComptime(
Expand Down Expand Up @@ -389,7 +389,7 @@ $ zig-out/bin/help --help

```zig
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

const params = comptime clap.parseParamsComptime(
Expand Down
19 changes: 8 additions & 11 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ pub fn parseParams(allocator: std.mem.Allocator, str: []const u8) ![]Param(Help)
/// Takes a string and parses it into many Param(Help). Returned is a newly allocated slice
/// containing all the parsed params. The caller is responsible for freeing the slice.
pub fn parseParamsEx(allocator: std.mem.Allocator, str: []const u8, end: *usize) ![]Param(Help) {
var list = std.ArrayList(Param(Help)).init(allocator);
errdefer list.deinit();
var list = std.ArrayList(Param(Help)){};
errdefer list.deinit(allocator);

try parseParamsIntoArrayListEx(&list, str, end);
return try list.toOwnedSlice();
try parseParamsIntoArrayListEx(allocator, &list, str, end);
return try list.toOwnedSlice(allocator);
}

/// Takes a string and parses it into many Param(Help) at comptime. Returned is an array of
Expand Down Expand Up @@ -131,9 +131,7 @@ fn countParams(str: []const u8) usize {
/// is returned, containing all the parameters parsed. This function will fail if the input slice
/// is to small.
pub fn parseParamsIntoSlice(slice: []Param(Help), str: []const u8) ![]Param(Help) {
var null_alloc = std.heap.FixedBufferAllocator.init("");
var list = std.ArrayList(Param(Help)){
.allocator = null_alloc.allocator(),
.items = slice[0..0],
.capacity = slice.len,
};
Expand All @@ -146,14 +144,13 @@ pub fn parseParamsIntoSlice(slice: []Param(Help), str: []const u8) ![]Param(Help
/// is returned, containing all the parameters parsed. This function will fail if the input slice
/// is to small.
pub fn parseParamsIntoSliceEx(slice: []Param(Help), str: []const u8, end: *usize) ![]Param(Help) {
var null_alloc = std.heap.FixedBufferAllocator.init("");
var null_allocator = std.heap.FixedBufferAllocator.init("");
var list = std.ArrayList(Param(Help)){
.allocator = null_alloc.allocator(),
.items = slice[0..0],
.capacity = slice.len,
};

try parseParamsIntoArrayListEx(&list, str, end);
try parseParamsIntoArrayListEx(null_allocator.allocator(), &list, str, end);
return list.items;
}

Expand All @@ -164,13 +161,13 @@ pub fn parseParamsIntoArrayList(list: *std.ArrayList(Param(Help)), str: []const
}

/// Takes a string and parses it into many Param(Help), which are appended onto `list`.
pub fn parseParamsIntoArrayListEx(list: *std.ArrayList(Param(Help)), str: []const u8, end: *usize) !void {
pub fn parseParamsIntoArrayListEx(allocator: std.mem.Allocator, list: *std.ArrayList(Param(Help)), str: []const u8, end: *usize) !void {
var i: usize = 0;
while (i != str.len) {
var end_of_this: usize = undefined;
errdefer end.* = i + end_of_this;

try list.append(try parseParamEx(str[i..], &end_of_this));
try list.append(allocator, try parseParamEx(str[i..], &end_of_this));
i += end_of_this;
}

Expand Down
2 changes: 1 addition & 1 deletion example/help.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

const params = comptime clap.parseParamsComptime(
Expand Down
2 changes: 1 addition & 1 deletion example/simple-ex.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

// First we specify what parameters our program can take.
Expand Down
2 changes: 1 addition & 1 deletion example/simple.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

// First we specify what parameters our program can take.
Expand Down
2 changes: 1 addition & 1 deletion example/subcommands.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const main_params = clap.parseParamsComptime(
const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);

pub fn main() !void {
var gpa_state = std.heap.GeneralPurposeAllocator(.{}){};
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

Expand Down
2 changes: 1 addition & 1 deletion example/usage.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();

const params = comptime clap.parseParamsComptime(
Expand Down