siga

Sistema Interativo de GerĂȘncia de Atividades.
Log | Files | Refs | README | LICENSE

options.zig (3898B)


      1 //! Command line options, in the same style as the Go auth service:
      2 //! `-address <host>` and `-port <port>`. `--flag` and `-flag=value` also work.
      3 
      4 const std = @import("std");
      5 
      6 pub const default_address = "localhost";
      7 pub const default_port: u16 = 16000;
      8 
      9 pub const usage =
     10     \\usage: registration [-address <ip>] [-port <port>]
     11     \\
     12     \\  -address  address to listen to (default "localhost")
     13     \\  -port     port to listen to (default 16000)
     14     \\
     15 ;
     16 
     17 pub const Options = struct {
     18     address: []const u8 = default_address,
     19     port: u16 = default_port,
     20 };
     21 
     22 pub const ParseError = error{ HelpRequested, UnknownFlag, MissingValue, InvalidPort };
     23 
     24 /// On error, `diag` holds the offending argument.
     25 pub const Diagnostic = struct {
     26     arg: []const u8 = "",
     27 };
     28 
     29 /// Parses `args`, without the program name.
     30 pub fn parse(args: []const [:0]const u8, diag: *Diagnostic) ParseError!Options {
     31     var opts: Options = .{};
     32     var i: usize = 0;
     33     while (i < args.len) : (i += 1) {
     34         const arg: []const u8 = args[i];
     35         diag.arg = arg;
     36 
     37         if (arg.len < 2 or arg[0] != '-') return error.UnknownFlag;
     38         const flag = if (std.mem.startsWith(u8, arg, "--")) arg[2..] else arg[1..];
     39 
     40         var name = flag;
     41         var inline_value: ?[]const u8 = null;
     42         if (std.mem.indexOfScalar(u8, flag, '=')) |eq| {
     43             name = flag[0..eq];
     44             inline_value = flag[eq + 1 ..];
     45         }
     46 
     47         if (std.mem.eql(u8, name, "h") or std.mem.eql(u8, name, "help")) {
     48             return error.HelpRequested;
     49         }
     50 
     51         const is_address = std.mem.eql(u8, name, "address");
     52         const is_port = std.mem.eql(u8, name, "port");
     53         if (!is_address and !is_port) return error.UnknownFlag;
     54 
     55         const value = inline_value orelse blk: {
     56             i += 1;
     57             if (i >= args.len) return error.MissingValue;
     58             break :blk args[i];
     59         };
     60 
     61         if (is_address) {
     62             if (value.len == 0) return error.MissingValue;
     63             opts.address = value;
     64         } else {
     65             diag.arg = value;
     66             opts.port = std.fmt.parseInt(u16, value, 10) catch return error.InvalidPort;
     67         }
     68     }
     69     return opts;
     70 }
     71 
     72 /// Turns the `-address` value into an IP address. Accepts `localhost` and
     73 /// IPv4/IPv6 literals; other host names are not resolved.
     74 pub fn resolveAddress(io: std.Io, address: []const u8, port: u16) !std.Io.net.IpAddress {
     75     if (std.ascii.eqlIgnoreCase(address, "localhost")) {
     76         return .{ .ip4 = .{ .bytes = .{ 127, 0, 0, 1 }, .port = port } };
     77     }
     78     return std.Io.net.IpAddress.resolve(io, address, port);
     79 }
     80 
     81 test "defaults" {
     82     var diag: Diagnostic = .{};
     83     const opts = try parse(&.{}, &diag);
     84     try std.testing.expectEqualStrings("localhost", opts.address);
     85     try std.testing.expectEqual(16000, opts.port);
     86 }
     87 
     88 test "flag forms" {
     89     var diag: Diagnostic = .{};
     90     const a = try parse(&.{ "-address", "0.0.0.0", "-port", "8000" }, &diag);
     91     try std.testing.expectEqualStrings("0.0.0.0", a.address);
     92     try std.testing.expectEqual(8000, a.port);
     93 
     94     const b = try parse(&.{ "--address=::1", "--port=9" }, &diag);
     95     try std.testing.expectEqualStrings("::1", b.address);
     96     try std.testing.expectEqual(9, b.port);
     97 }
     98 
     99 test "errors" {
    100     var diag: Diagnostic = .{};
    101     try std.testing.expectError(error.MissingValue, parse(&.{"-port"}, &diag));
    102     try std.testing.expectError(error.InvalidPort, parse(&.{ "-port", "70000" }, &diag));
    103     try std.testing.expectEqualStrings("70000", diag.arg);
    104     try std.testing.expectError(error.InvalidPort, parse(&.{"-port=abc"}, &diag));
    105     try std.testing.expectError(error.UnknownFlag, parse(&.{"-verbose"}, &diag));
    106     try std.testing.expectEqualStrings("-verbose", diag.arg);
    107     try std.testing.expectError(error.UnknownFlag, parse(&.{"solto"}, &diag));
    108     try std.testing.expectError(error.HelpRequested, parse(&.{"-h"}, &diag));
    109 }