-
Notifications
You must be signed in to change notification settings - Fork 6
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
Mutually exclusive options in a mandatory set. #28
Comments
Would it be possible to work these like this? encoder -input y4m -output webm If so, you can use the If that's not possible/desirable, I would use exclusive or (XOR) logic with the const opts = try main_cmd.getOpts();
defer alloc.free(getOpts); // not needed with the preferred Arena Allocator, but it's good practice I think.
// List of possible names for the Input Option
const xor_in_opts: []const []const u8 = .{ "exclusive", "input", "opts" };
// Run XOR on the given Input Option against the list above
const xor_in_opt = getXorIn: {
var in_opt: ?OptionT = null;
var xor_in_flag = false;
for (opts) |opt| {
if (cova.utils.indexOfEql([]const u8, xor_in_opts, opt)) |_| {
if (xor_in_flag) {
xor_in_flag = false;
break;
}
xor_in_flag = true;
in_opt = opt;
}
}
if (!xor_in_flag) {
log.err("The Input Option must be exclusively one of: {s}\n", .{ xor_in_opts });
return error.NonExclusiveInput;
}
break :getXorIn in_opt.?;
};
// Handle xor_in_opt
// Repeat for xor_out_opt I wrote that up here so it's untested, but it should be the general idea. Admittedly, I could probably make that a smoother process somehow. Maybe wrap it into |
The goal was to a) reduce the number of required options as much as possible (as things get out of hand quickly when it comes to encoding parameters) and b) eliminate the need for passed path to have any specific format or have any specific extension This allows the user to arbitrarily specify filenames as they like without it affecting the format the command line application interprets the read or written data. In other words, the following all work and aren't unclear at all and don't require any additional flags to specify anything: -y4m=/dev/stdin (format: y4m, input path: /dev/stdin) |
I think that all makes sense to me. If I'm understanding correctly, point a rules out my first solution, but I believe my second solution using For your case it would look something like: const in_fmt = (try main_cmd.matchOpts(&.{ "valid", "input", "formats" }, .{ .logic = .XOR }))[0];
const out_fmt = (try main_cmd.matchOpts(&.{ "valid", "output", "formats" }, .{ .logic = .XOR }))[0];
// Handle in_fmt and out_fmt I'll have this integrated and pushed to the working v0.9.0 branch soon. |
Yes, my only constraint is external usage, not internal handling of the mutual-exclusivity, so it would appear that your solution would work just fine (though, I don't completely understand what your code is doing at a glance). |
- Added `checkOpts()`, `matchOpts()`, and `matchOptsAlloc()` to `Command.Custom` to enable customized analysis of a Command's Options. - Closes #28
No worries on trying to understand that code. It was pseudo-code and a little flawed, but the intent was to analyze the Options set by the end user to ensure only 1 from the provided list was set (XOR logic). I've now rolled that into the new const in_fmt = (main_cmd.matchOpts(&.{ "valid", "input", "formats" }, .{ .logic = .XOR }) catch |err| switch (err) {
error.CheckOptionLogicFailXOR => { // Handle the case where the user provides more than one input format },
else => return err,
})[0];
const out_fmt = (main_cmd.matchOpts(&.{ "valid", "output", "formats" }, .{ .logic = .XOR }) catch |err| switch (err) {
error.CheckOptionLogicFailXOR => { // Handle the case where the user provides more than one output format },
else => return err,
})[0];
// Handle in_fmt and out_fmt You can also use AND or OR logic if you want to make sure only specific Options from a given list are set as well. Let me know if that works for you. If not, I'm here to answer any questions and/or tweak the code a little as needed. |
I believe you've created a library thorough enough for me to create external categories of values, and when I have, I'll share my code because I believe option categories are an important feature Cova should internally support. |
This was also brought up in issue #26 by @GobiasSomeCoffeeCo. It's part of the v0.9.0 milestone to implement for sure. I'd love to see your take on the feature as well! |
Closing this here as I don't plan to revisit this for awhile. |
How might I implement mutually exclusive options within a set that must be passed at runtime? For example, every command to the program must include an input and an output option, but there are more than one to select for each kind.
I'm implementing an encoder than supports -mkv, -y4m, or -yuv inputs and -webm or -ivf outputs.
The text was updated successfully, but these errors were encountered: