Skip to content

Commit

Permalink
Implemented Option Parsing Termination
Browse files Browse the repository at this point in the history
- Added `enable_opt_termination` to `cova.ParseConfig` as a configuration flag for terminating the parsing of Options if `--` (or whichever symbol is used for Option long names) is given by itself.
- Closes #23
  • Loading branch information
00JCIV00 committed Sep 18, 2023
1 parent 97db0a0 commit 243de80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn Typed(comptime SetT: type, comptime config: Config) type {

/// Parse the given argument token (`arg`) to this Value's Type.
pub fn parse(self: *const @This(), arg: []const u8) !ChildT {
if (self.parse_fn != null) return self.parse_fn.?(arg) catch error.CannotParseArgToValue;
if (self.parse_fn) |parseFn| return parseFn(arg) catch error.CannotParseArgToValue;
var san_arg_buf: [512]u8 = undefined;
var san_arg = toLower(san_arg_buf[0..], arg);
return switch (@typeInfo(ChildT)) {
Expand Down Expand Up @@ -146,7 +146,7 @@ pub fn Typed(comptime SetT: type, comptime config: Config) type {
// Single Arg
const parsed_arg = try self.parse(set_arg);
@constCast(self).is_set =
if (self.valid_fn != null) self.valid_fn.?(parsed_arg)
if (self.valid_fn) |validFn| validFn(parsed_arg)
else true;
if (self.is_set) {
switch (self.set_behavior) {
Expand Down
9 changes: 8 additions & 1 deletion src/cova.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub const ParseConfig = struct {
auto_handle_usage_help: bool = true,
/// Decide how to react to parsing errors.
err_reaction: ParseErrorReaction = .Help,
/// Enable Option Termination using `--` per the POSIX standard (or whatever symbol is chosen for Option long names).
enable_opt_termination: bool = true,

/// Reactions for Parsing Errors.
const ParseErrorReaction = enum {
Expand All @@ -152,6 +154,7 @@ pub fn parseArgs(
if (!cmd._is_init) return error.CommandNotInitialized;

var val_idx: u8 = 0;
var opt_term: bool = false;
const OptionT = CommandT.OptionT;

// Bypass argument 0 (the filename being executed);
Expand Down Expand Up @@ -181,7 +184,7 @@ pub fn parseArgs(
log.debug("No Commands Matched for Command '{s}'.", .{ cmd.name });
}
// ...Then for any Options...
if (cmd.opts != null) {
if (cmd.opts != null and !opt_term) {
log.debug("Attempting to Parse Options...", .{});
const short_pf = OptionT.short_prefix;
const long_pf = OptionT.long_prefix;
Expand Down Expand Up @@ -263,6 +266,10 @@ pub fn parseArgs(
}
// - Long Options
else if (mem.eql(u8, arg[0..long_pf.len], long_pf)) {
if (arg.len == long_pf.len) {
opt_term = true;
continue;
}
const split_idx = (mem.indexOfAny(u8, arg[long_pf.len..], OptionT.opt_val_seps) orelse arg.len - long_pf.len) + long_pf.len;
const long_opt = arg[long_pf.len..split_idx];
const sep_arg = if (split_idx < arg.len) arg[split_idx + 1..] else "";
Expand Down

0 comments on commit 243de80

Please sign in to comment.