Skip to content

Commit 5a3564b

Browse files
committed
Closes #3; Add help info text
1 parent cea4b2e commit 5a3564b

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/HelpMessage.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
3+
const helpMessage =
4+
"Usage:\n" ++
5+
"(1) {s} <host> <port> [runModeOptions]\n" ++
6+
"(2) {s} [infoOptions]\n" ++
7+
"\n" ++
8+
"Arguments:\n" ++
9+
"\t<host>\t\t IPv4 address of the server\n" ++
10+
"\t<port>\t\t Port number of the server\n" ++
11+
"\n" ++
12+
"[runModeOptions]:\n" ++
13+
"\tfor now there are no runModeOptions\n" ++
14+
"\n" ++
15+
"[infoOptions]:\n" ++
16+
"\t-h, --help\tPrint this help and exit\n" ++
17+
"\t-v, --version\tPrint version number and exit\n" ++
18+
"\n";
19+
20+
pub fn printHelpMessage(programName: []const u8) !void {
21+
try std.io.getStdOut().writer().print(helpMessage, .{ programName, programName });
22+
}

src/main.zig

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const net = std.net;
33
const print = std.debug.print;
44

55
const Client = @import("client.zig").Client;
6+
const HelpMessage = @import("HelpMessage.zig");
67

78
const version = "1.0.0";
89

@@ -17,20 +18,40 @@ pub fn main() !void {
1718
const program_name = args.next() orelse "mini-RTS-zig-bot";
1819

1920
const host_value = args.next() orelse {
20-
print("no host name or ip address provided\nusage {s} <host> <port>\n", .{program_name});
21+
try std.io.getStdOut().writer().print("no host provided\n", .{});
22+
try HelpMessage.printHelpMessage(program_name);
2123
return;
2224
};
2325

24-
if (std.mem.eql(u8, host_value, "--version") or std.mem.eql(u8, host_value, "-v")) {
25-
try std.io.getStdOut().writer().print("{s}\n", .{version});
26+
if (std.mem.eql(u8, host_value, "-v") or std.mem.eql(u8, host_value, "--version")) {
27+
if (args.next()) |option| {
28+
try std.io.getStdOut().writer().print("unnecessary additional option: {s}\n", .{option});
29+
try HelpMessage.printHelpMessage(program_name);
30+
} else try std.io.getStdOut().writer().print("{s}\n", .{version});
31+
return;
32+
} else if (std.mem.eql(u8, host_value, "-h") or std.mem.eql(u8, host_value, "--help")) {
33+
if (args.next()) |option| try std.io.getStdOut().writer().print("unnecessary additional option: {s}\n", .{option});
34+
try HelpMessage.printHelpMessage(program_name);
2635
return;
2736
}
2837

2938
const port_value = args.next() orelse {
30-
print("no port provided\nusage {s} <host> <port>\n", .{program_name});
39+
try std.io.getStdOut().writer().print("no port provided\n", .{});
40+
try HelpMessage.printHelpMessage(program_name);
41+
return;
42+
};
43+
44+
if (args.next()) |option| {
45+
try std.io.getStdOut().writer().print("unnecessary additional option: {s}\nrunModeOptions are not supported\n", .{option});
46+
try HelpMessage.printHelpMessage(program_name);
47+
return;
48+
}
49+
50+
const port = std.fmt.parseInt(u16, port_value, 10) catch {
51+
try std.io.getStdOut().writer().print("invalid port number {s}\n", .{port_value});
52+
try HelpMessage.printHelpMessage(program_name);
3153
return;
3254
};
33-
const port = try std.fmt.parseInt(u16, port_value, 10);
3455

3556
var cli = try Client.init(host_value, port, null);
3657
defer cli.deinit();

0 commit comments

Comments
 (0)