Something went wrong. Try again.
various tools I have been using throughout the years
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143const std = @import("std");
pub const OptionsTag = enum { settings, status, help };
pub const Options = union(OptionsTag) { settings: Settings, status: StatusSettings, help: void,};
pub const Settings = struct { task_length: u32 = 26, task_break: u32 = 4, notifications: bool = true, socket: ?[]const u8 = null,};
pub const StatusSettings = struct { socket: ?[]const u8 = null,};
pub fn parseArgs(args: []const []const u8) !Options { if (args.len > 0 and std.mem.eql(u8, args[0], "status")) { return parseStatusArgs(args[1..]); }
var settings = Settings{};
var i: u32 = 0; while (i < args.len) : (i += 1) { const arg = args[i]; if (i + 1 < args.len and std.mem.eql(u8, arg, "-t")) { i += 1; settings.task_length = try std.fmt.parseInt(u32, args[i], 10); } else if (i + 1 < args.len and std.mem.eql(u8, arg, "-b")) { i += 1; settings.task_break = try std.fmt.parseInt(u32, args[i], 10); } else if (std.mem.eql(u8, arg, "-s")) { settings.notifications = false; } else if (i + 1 < args.len and std.mem.eql(u8, arg, "--socket")) { i += 1; settings.socket = args[i]; } else if (std.mem.eql(u8, arg, "-h")) { return .{ .help = {} }; } else { return error.InvalidArgument; } }
return .{ .settings = settings };}
fn parseStatusArgs(args: []const []const u8) !Options { var status = StatusSettings{};
var i: u32 = 0; while (i < args.len) : (i += 1) { const arg = args[i]; if (i + 1 < args.len and std.mem.eql(u8, arg, "--socket")) { i += 1; status.socket = args[i]; } else if (std.mem.eql(u8, arg, "-h")) { return .{ .help = {} }; } else { return error.InvalidArgument; } }
return .{ .status = status };}
pub fn usage(allocator: std.mem.Allocator, program: []const u8) ![]u8 { const result = try allocator.print( \\Usage: {s} [-t <task_length>] [-b <task_break>] [-s] [--socket <path>] \\ {s} status [--socket <path>] \\ \\[-t <task_length>] Task length in minutes (default: 26 mins) \\[-b <task_break>] Task break length in minutes (default: 4 mins) \\[-s] Disable notifications \\[--socket <path>] Status socket (default: $XDG_RUNTIME_DIR/pracomer.sock) \\[-h] Show this help message \\ , .{ program, program });
return result;}
test "parseArgs options" { const args = [_][]const u8{ "-t", "30", "-b", "20", "-s", };
const options = try parseArgs(&args); try std.testing.expectEqual(options.settings.task_length, 30); try std.testing.expectEqual(options.settings.task_break, 20); try std.testing.expectEqual(options.settings.notifications, false); try std.testing.expectEqual(options.settings.socket, null);}
test "parseArgs socket" { const args = [_][]const u8{ "--socket", "/tmp/pracomer.sock" };
const options = try parseArgs(&args); try std.testing.expectEqualStrings("/tmp/pracomer.sock", options.settings.socket.?);}
test "parseArgs status" { const options = try parseArgs(&[_][]const u8{"status"}); try std.testing.expectEqual(OptionsTag.status, @as(OptionsTag, options)); try std.testing.expectEqual(options.status.socket, null);}
test "parseArgs status socket" { const options = try parseArgs(&[_][]const u8{ "status", "--socket", "/run/pracomer.sock" }); try std.testing.expectEqualStrings("/run/pracomer.sock", options.status.socket.?);}
test "parseArgs status rejects timer flags" { const result = parseArgs(&[_][]const u8{ "status", "-t", "30" }); try std.testing.expectError(error.InvalidArgument, result);}
test "parseArgs rejects file" { const result = parseArgs(&[_][]const u8{ "--file", "/tmp/pracomer.log" }); try std.testing.expectError(error.InvalidArgument, result);}
test "parseArgs help" { const args = [_][]const u8{"-h"};
const options = try parseArgs(&args); try std.testing.expectEqual(options, OptionsTag.help);}
test "parseArgs nok" { const args = [_][]const u8{ "some", "junk", "arguments" };
const result = parseArgs(&args); try std.testing.expectError(error.InvalidArgument, result);}