diff options
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/main.zig b/src/main.zig index 7425c6d..b47f48d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,12 +11,34 @@ const trackproto = @import("tracker_protocol.zig"); const peer_id = [20]u8{ 0x30, 0x30, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33, 0x34, 0x34, 0x35, 0x35, 0x36, 0x36, 0x37, 0x37, 0x38, 0x38, 0x39, 0x39 }; // To make this a practical torrent app... // Add some UI? or keep it a console app? I might keep it as a console app for now -pub fn main() !void { +const Args = struct { + torrent_file: []const u8, +}; + +fn read_args() !Args { + var args = std.process.args(); + if (!args.skip()) return error.NotEnoughArgs; + const nxt = args.next() orelse return error.NotEnoughArgs; + if (std.mem.eql(u8, nxt, "-h") or std.mem.eql(u8, nxt, "--help")) { + try printUsage(false); + return error.Help; + } + return .{ + .torrent_file = nxt, + }; +} + +fn do_main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const a = gpa.allocator(); - const f = try std.fs.cwd().openFile("src/sample.torrent", .{}); + const args = read_args() catch |e| switch (e) { + error.Help => return, + else => |err| return err, + }; + + const f = try std.fs.cwd().openFile(args.torrent_file, .{}); defer f.close(); var fr = f.reader(); var mib = try bencode.bdecode(a, fr); @@ -120,6 +142,22 @@ pub fn main() !void { std.log.info("fin", .{}); } +pub fn main() !void { + do_main() catch |e| { + try printUsage(true); + return e; + }; +} + +fn printUsage(err: bool) !void { + var f = if (err) std.io.getStdErr() else std.io.getStdOut(); + try f.writeAll( + \\ Usage: zbt TORRENTFILE + \\ Download the data specfied by TORRENTFILE to the current directory + \\ + ); +} + test { _ = bencode; _ = MetaInfo; |