diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-12-02 00:14:41 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-12-02 00:14:41 +0000 |
commit | 9bd0ffda30c0fa991a89920d01ec2cb12dbbf7bc (patch) | |
tree | 547c4a10cb5fd632b5d7712f8338ccbb7a75a8e2 /src/main.zig | |
parent | 56325badfaa713a920128d27479e9205615957c1 (diff) | |
download | zbt-9bd0ffda30c0fa991a89920d01ec2cb12dbbf7bc.tar.gz zbt-9bd0ffda30c0fa991a89920d01ec2cb12dbbf7bc.tar.bz2 zbt-9bd0ffda30c0fa991a89920d01ec2cb12dbbf7bc.tar.xz zbt-9bd0ffda30c0fa991a89920d01ec2cb12dbbf7bc.zip |
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; |