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
90 changes: 24 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ incomplete.
The simplest way to use this library is to just call the `clap.parse` function.

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
Expand All @@ -76,12 +69,12 @@ pub fn main() !void {
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
// care about the extra information `Diagnostics` provides.
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
var res = clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{
.diagnostic = &diag,
.allocator = gpa,
.allocator = init.gpa,
}) catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
};
defer res.deinit();
Expand Down Expand Up @@ -112,14 +105,7 @@ contains a parser that returns `usize`. You can pass in something other than `cl
if you want some other mapping.

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
Expand All @@ -142,15 +128,15 @@ pub fn main() !void {
};

var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, parsers, .{
var res = clap.parse(clap.Help, &params, parsers, init.minimal.args, .{
.diagnostic = &diag,
.allocator = gpa,
.allocator = init.gpa,
// The assignment separator can be configured. `--number=1` and `--number:1` is now
// allowed.
.assignment_separators = "=:",
}) catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
};
defer res.deinit();
Expand Down Expand Up @@ -198,23 +184,16 @@ const main_params = clap.parseParamsComptime(
// get the return type of `clap.parse` and `clap.parseEx`.
const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

var iter = try std.process.ArgIterator.initWithAllocator(gpa);
pub fn main(init: std.process.Init) !void {
var iter = try init.minimal.args.iterateAllocator(init.gpa);
defer iter.deinit();

_ = iter.next();

var diag = clap.Diagnostic{};
var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
.diagnostic = &diag,
.allocator = gpa,
.allocator = init.gpa,

// Terminate the parsing of arguments after parsing the first positional (0 is passed
// here because parsed positionals are, like slices and arrays, indexed starting at 0).
Expand All @@ -223,7 +202,7 @@ pub fn main() !void {
// not fully consumed. It can then be reused to parse the arguments for subcommands.
.terminating_positional = 0,
}) catch |err| {
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
};
defer res.deinit();
Expand All @@ -234,11 +213,11 @@ pub fn main() !void {
const command = res.positionals[0] orelse return error.MissingCommand;
switch (command) {
.help => std.debug.print("--help\n", .{}),
.math => try mathMain(io, gpa, &iter, res),
.math => try mathMain(init.io, init.gpa, &iter, res),
}
}

fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator, main_args: MainArgs) !void {
// The parent arguments are not used here, but there are cases where it might be useful, so
// this example shows how to pass the arguments around.
_ = main_args;
Expand Down Expand Up @@ -284,14 +263,7 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
`args.Iterator` to provide it with arguments lazily.

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
const params = [_]clap.Param(u8){
.{
Expand All @@ -306,7 +278,7 @@ pub fn main() !void {
.{ .id = 'f', .takes_value = .one },
};

var iter = try std.process.ArgIterator.initWithAllocator(gpa);
var iter = try init.minimal.args.iterateAllocator(init.gpa);
defer iter.deinit();

// Skip exe argument.
Expand All @@ -316,7 +288,7 @@ pub fn main() !void {
// This is optional. You can also leave the `diagnostic` field unset if you
// don't care about the extra information `Diagnostic` provides.
var diag = clap.Diagnostic{};
var parser = clap.streaming.Clap(u8, std.process.ArgIterator){
var parser = clap.streaming.Clap(u8, std.process.Args.Iterator){
.params = &params,
.iter = &iter,
.diagnostic = &diag,
Expand All @@ -325,7 +297,7 @@ pub fn main() !void {
// Because we use a streaming parser, we have to consume each argument parsed individually.
while (parser.next() catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
}) |arg| {
// arg.param will point to the parameter which matched the argument.
Expand Down Expand Up @@ -363,29 +335,22 @@ runtime.
is passed to `help` to control how the help message is printed.

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-v, --version Output version information and exit.
\\
);

var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
var res = try clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa });
defer res.deinit();

// `clap.help` is a function that can print a simple help message. It can print any `Param`
// where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
// The last argument contains options as to how `help` should print those parameters. Using
// `.{}` means the default options.
if (res.args.help != 0)
return clap.helpToFile(io, .stderr(), clap.Help, &params, .{});
return clap.helpToFile(init.io, .stderr(), clap.Help, &params, .{});
}

const clap = @import("clap");
Expand All @@ -407,28 +372,21 @@ $ zig-out/bin/help --help
`value` method so it can provide that in the output.

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

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-v, --version Output version information and exit.
\\ --value <str> An option parameter, which takes a value.
\\
);

var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
var res = try clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa });
defer res.deinit();

// `clap.usageToFile` is a function that can print a simple usage string. It can print any
// `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
if (res.args.help != 0)
return clap.usageToFile(io, .stdout(), clap.Help, &params);
return clap.usageToFile(init.io, .stdout(), clap.Help, &params);
}

const clap = @import("clap");
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = .clap,
.version = "0.11.0",
.minimum_zig_version = "0.16.0-dev.1859+212968c57",
.minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a",
.fingerprint = 0x65f99e6f07a316a0,
.paths = .{
"clap",
Expand Down
5 changes: 3 additions & 2 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,17 @@ pub fn parse(
comptime Id: type,
comptime params: []const Param(Id),
comptime value_parsers: anytype,
arguments: std.process.Args,
opt: ParseOptions,
) !Result(Id, params, value_parsers) {
var arena = std.heap.ArenaAllocator.init(opt.allocator);
errdefer arena.deinit();

var iter = try std.process.ArgIterator.initWithAllocator(arena.allocator());
var iter = try arguments.iterateAllocator(arena.allocator());
const exe_arg = iter.next();

const result = try parseEx(Id, params, value_parsers, &iter, .{
// Let's reuse the arena from the `ArgIterator` since we already have it.
// Let's reuse the arena from the `Args.Iterator` since we already have it.
.allocator = arena.allocator(),
.diagnostic = opt.diagnostic,
.assignment_separators = opt.assignment_separators,
Expand Down
13 changes: 3 additions & 10 deletions example/help.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
pub fn main() !void {
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-v, --version Output version information and exit.
\\
);

var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
var res = try clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa });
defer res.deinit();

// `clap.help` is a function that can print a simple help message. It can print any `Param`
// where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
// The last argument contains options as to how `help` should print those parameters. Using
// `.{}` means the default options.
if (res.args.help != 0)
return clap.helpToFile(io, .stderr(), clap.Help, &params, .{});
return clap.helpToFile(init.io, .stderr(), clap.Help, &params, .{});
}

const clap = @import("clap");
Expand Down
15 changes: 4 additions & 11 deletions example/simple-ex.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
pub fn main() !void {
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
Expand All @@ -28,15 +21,15 @@ pub fn main() !void {
};

var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, parsers, .{
var res = clap.parse(clap.Help, &params, parsers, init.minimal.args, .{
.diagnostic = &diag,
.allocator = gpa,
.allocator = init.gpa,
// The assignment separator can be configured. `--number=1` and `--number:1` is now
// allowed.
.assignment_separators = "=:",
}) catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
};
defer res.deinit();
Expand Down
15 changes: 4 additions & 11 deletions example/simple.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
pub fn main() !void {
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
Expand All @@ -20,12 +13,12 @@ pub fn main() !void {
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
// care about the extra information `Diagnostics` provides.
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
var res = clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{
.diagnostic = &diag,
.allocator = gpa,
.allocator = init.gpa,
}) catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
};
defer res.deinit();
Expand Down
15 changes: 4 additions & 11 deletions example/streaming-clap.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
pub fn main() !void {
var gpa_state = std.heap.DebugAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();

pub fn main(init: std.process.Init) !void {
// First we specify what parameters our program can take.
const params = [_]clap.Param(u8){
.{
Expand All @@ -20,7 +13,7 @@ pub fn main() !void {
.{ .id = 'f', .takes_value = .one },
};

var iter = try std.process.ArgIterator.initWithAllocator(gpa);
var iter = try init.minimal.args.iterateAllocator(init.gpa);
defer iter.deinit();

// Skip exe argument.
Expand All @@ -30,7 +23,7 @@ pub fn main() !void {
// This is optional. You can also leave the `diagnostic` field unset if you
// don't care about the extra information `Diagnostic` provides.
var diag = clap.Diagnostic{};
var parser = clap.streaming.Clap(u8, std.process.ArgIterator){
var parser = clap.streaming.Clap(u8, std.process.Args.Iterator){
.params = &params,
.iter = &iter,
.diagnostic = &diag,
Expand All @@ -39,7 +32,7 @@ pub fn main() !void {
// Because we use a streaming parser, we have to consume each argument parsed individually.
while (parser.next() catch |err| {
// Report useful error and exit.
try diag.reportToFile(io, .stderr(), err);
try diag.reportToFile(init.io, .stderr(), err);
return err;
}) |arg| {
// arg.param will point to the parameter which matched the argument.
Expand Down
Loading