Skip to content

Commit

Permalink
Implemented Nullable Prefixes for Options
Browse files Browse the repository at this point in the history
- Changed the short and long prefixes in `Option.Config` to be nullable, allowing only one to be specified if desired.
- Closes #24
  • Loading branch information
00JCIV00 committed Sep 20, 2023
1 parent 5164ddf commit 8d1fe6d
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 699 deletions.
2 changes: 1 addition & 1 deletion docs/data-astNodes.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/data-decls.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/data-exprs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/data-types.js

Large diffs are not rendered by default.

241 changes: 121 additions & 120 deletions docs/src/cova/Option.zig.html

Large diffs are not rendered by default.

898 changes: 450 additions & 448 deletions docs/src/cova/cova.zig.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/covademo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const ex_structs = @import("example_structs.zig");
pub const CommandT = Command.Custom(.{
.global_help_prefix = "CovaDemo",
.vals_mandatory = false,
.opt_config = .{
.short_prefix = null,
.long_prefix = "-",
},
});
pub const ValueT = CommandT.ValueT;

Expand Down
17 changes: 9 additions & 8 deletions src/Option.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ pub const Config = struct {
/// Format for the Usage message.
///
/// Must support the following format types in this order:
/// 1. Character (Short Prefix)
/// 2. Optional Character "{?c} (Short Name)
/// 1. Optional Character (Short Prefix)
/// 2. Optional Character "{?c}" (Short Name)
/// 3. String (Long Prefix)
/// 4. Optional String "{?s}" (Long Name)
/// 5. String (Value Name)
/// 6. String (Value Type)
usage_fmt: []const u8 = "[{c}{?c},{s}{?s} \"{s} ({s})\"]",

/// Prefix for Short Options.
short_prefix: u8 = '-',
short_prefix: ?u8 = '-',
/// Prefix for Long Options.
long_prefix: []const u8 = "--",
long_prefix: ?[]const u8 = "--",

/// During parsing, allow there to be no space ' ' between Options and Values.
/// This is allowed per the POSIX standard, but may not be ideal as it interrupts the parsing of chained booleans even in the event of a misstype.
Expand All @@ -62,6 +62,7 @@ pub fn Base() type { return Custom(.{}); }

/// Create a Custom Option type from the provided Config (`config`).
pub fn Custom(comptime config: Config) type {
if (config.short_prefix == null and config.long_prefix == null) @compileError("Either a Short or Long prefix must be set for Option Types!");
return struct {
/// The Custom Value type used by this Custom Option type.
const ValueT = Value.Custom(config.val_config);
Expand Down Expand Up @@ -121,10 +122,10 @@ pub fn Custom(comptime config: Config) type {
/// Creates the Usage message for this Option and Writes it to the provided Writer (`writer`).
pub fn usage(self: *const @This(), writer: anytype) !void {
try writer.print(usage_fmt, .{
short_prefix,
self.short_name,
long_prefix,
self.long_name,
short_prefix orelse 0,
if (short_prefix != null) self.short_name else 0,
long_prefix orelse "",
if (long_prefix != null) self.long_name else "",
self.val.name(),
self.val.valType(),
});
Expand Down
240 changes: 121 additions & 119 deletions src/cova.zig

Large diffs are not rendered by default.

0 comments on commit 8d1fe6d

Please sign in to comment.