fetch_ml/cli/src/utils/flags.zig
Jeremie Fraeys c85575048f
refactor(cli): consolidate shared types and reduce code duplication
Extract common UserContext and authentication logic from cancel.zig and
status.zig into new utils/auth.zig module. Add CommonFlags struct to
utils/flags.zig for shared CLI flags. Add getWebSocketUrl() helper to
Config to eliminate URL construction duplication.

Changes:
- Create cli/src/utils/auth.zig with UserContext and authenticateUser()
- Create cli/src/utils/flags.zig with CommonFlags struct
- Update cancel.zig and status.zig to use shared modules
- Add getWebSocketUrl() helper to config.zig
- Export new modules from utils.zig

Reduces code duplication and improves separation of concerns in the
Zig CLI codebase.
2026-02-18 13:00:48 -05:00

35 lines
1.2 KiB
Zig

/// Common command-line flags shared across commands
pub const CommonFlags = struct {
json: bool = false,
dry_run: bool = false,
verbose: bool = false,
force: bool = false,
validate: bool = false,
};
/// Parse common flags from arguments
pub fn parseCommonFlags(args: []const []const u8, flags: *CommonFlags) struct { consumed: usize, had_help: bool } {
var i: usize = 0;
var had_help = false;
while (i < args.len) : (i += 1) {
const arg = args[i];
if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
had_help = true;
} else if (std.mem.eql(u8, arg, "--json")) {
flags.json = true;
} else if (std.mem.eql(u8, arg, "--dry-run")) {
flags.dry_run = true;
} else if (std.mem.eql(u8, arg, "--verbose") or std.mem.eql(u8, arg, "-v")) {
flags.verbose = true;
} else if (std.mem.eql(u8, arg, "--force") or std.mem.eql(u8, arg, "-f")) {
flags.force = true;
} else if (std.mem.eql(u8, arg, "--validate")) {
flags.validate = true;
} else {
break; // Stop at first non-flag argument
}
}
return .{ .consumed = i, .had_help = had_help };
}
const std = @import("std");