-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Whether subcommands are supported ? #90
Comments
This is not a feature this library has yet and I've not had much motivation to implement this feature myself. It is not really a feature I need this library to have for my projects, so I've had little motivation to come up with a good API and implement it. Here are some examples of how people have implemented subcommands and then used |
I'm trying to implement subcommands with different parsers, but it appears that you can only use const Subcommand = enum { client, server };
const parsers = comptime .{
.int = clap.parsers.int,
.SUBCOMMAND = clap.parsers.enumeration(Subcommand),
};
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit
\\--version Prints version information
\\<SUBCOMMAND> Either "client" or "server"
\\
);
const client_params = comptime clap.parseParamsComptime(
\\--cid <int> The remote endpoint CID
\\--port <int> The remote endpoint port
\\
);
const server_params = comptime clap.parseParamsComptime(
\\--port <int> The local port to listen on
\\
);
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, ¶ms, parsers, .{
.diagnostic = &diag,
}) catch |err| {
diag.report(std.io.getStdErr().writer(), err) catch {};
show_usage(clap.Help, ¶ms) catch {};
return;
};
defer res.deinit();
if (res.positionals.len != 1) {
std.io.getStdErr().writer().print("Missing subcommand\n", .{}) catch {};
show_usage(clap.Help, ¶ms) catch {};
return;
}
if (res.args.help) {
const stdout = std.io.getStdOut();
try clap.help(stdout.writer(), clap.Help, ¶ms, .{});
try stdout.writer().print("\nclient subcommand options:\n", .{});
try clap.help(stdout.writer(), clap.Help, &client_params, .{});
try stdout.writer().print("\nserver subcommand options:\n", .{});
try clap.help(stdout.writer(), clap.Help, &server_params, .{});
return;
}
if (res.args.version) {
const stdout = std.io.getStdOut();
try stdout.writer().print("v0.0.0", .{});
return;
}
switch (res.positionals[0]) {
.client => {
var client_res = clap.parse(clap.Help, &client_params, parsers, .{
.diagnostic = &diag,
}) catch |err| {
diag.report(std.io.getStdErr().writer(), err) catch {};
show_usage(&client_params) catch {};
return;
};
defer client_res.deinit();
},
.server => {
unreachable;
},
} Could you add a |
I've tried your code and it does compile fine (after changing The problem now is when you do:
What happens here, is that Another resource is the PR #93. There is an example for how you can use |
No description provided.
The text was updated successfully, but these errors were encountered: