From c564b168785e740c37f47da4942825b25cb8b4ec Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 14 Nov 2018 14:06:20 +0100 Subject: Restructured and make StreamingClap simpler * Also added a ComptimeClap --- src/index.zig | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/index.zig (limited to 'src/index.zig') diff --git a/src/index.zig b/src/index.zig new file mode 100644 index 0000000..dde4748 --- /dev/null +++ b/src/index.zig @@ -0,0 +1,105 @@ +const std = @import("std"); + +const debug = std.debug; + +pub const @"comptime" = @import("comptime.zig"); +pub const args = @import("args.zig"); +pub const streaming = @import("streaming.zig"); + +test "clap" { + _ = @"comptime"; + _ = args; + _ = streaming; +} + +pub const ComptimeClap = @"comptime".ComptimeClap; +pub const StreamingClap = streaming.StreamingClap; + +/// The names a ::Param can have. +pub const Names = struct { + /// '-' prefix + short: ?u8, + + /// '--' prefix + long: ?[]const u8, + + /// Initializes no names + pub fn positional() Names { + return Names{ + .short = null, + .long = null, + }; + } + + /// Initializes a short name + pub fn short(s: u8) Names { + return Names{ + .short = s, + .long = null, + }; + } + + /// Initializes a long name + pub fn long(l: []const u8) Names { + return Names{ + .short = null, + .long = l, + }; + } + + /// Initializes a name with a prefix. + /// ::short is set to ::name[0], and ::long is set to ::name. + /// This function asserts that ::name.len != 0 + pub fn prefix(name: []const u8) Names { + debug.assert(name.len != 0); + + return Names{ + .short = name[0], + .long = name, + }; + } +}; + +/// Represents a parameter for the command line. +/// Parameters come in three kinds: +/// * Short ("-a"): Should be used for the most commonly used parameters in your program. +/// * They can take a value three different ways. +/// * "-a value" +/// * "-a=value" +/// * "-avalue" +/// * They chain if they don't take values: "-abc". +/// * The last given parameter can take a value in the same way that a single parameter can: +/// * "-abc value" +/// * "-abc=value" +/// * "-abcvalue" +/// * Long ("--long-param"): Should be used for less common parameters, or when no single character +/// can describe the paramter. +/// * They can take a value two different ways. +/// * "--long-param value" +/// * "--long-param=value" +/// * Positional: Should be used as the primary parameter of the program, like a filename or +/// an expression to parse. +/// * Positional parameters have both names.long and names.short == null. +/// * Positional parameters must take a value. +pub fn Param(comptime Id: type) type { + return struct { + id: Id, + takes_value: bool, + names: Names, + + pub fn init(id: Id, takes_value: bool, names: Names) @This() { + // Assert, that if the param have no name, then it has to take + // a value. + debug.assert( + names.long != null or + names.short != null or + takes_value); + + return @This(){ + .id = id, + .takes_value = takes_value, + .names = names, + }; + } + }; +} -- cgit v1.2.3