refactor argument fetching for load cmd

This commit is contained in:
Luna 2020-06-02 16:59:53 -03:00
parent dc98c7a22f
commit c78ca9dd5b

View file

@ -49,20 +49,22 @@ pub const Runner = struct {
// parse the index from 1 to end // parse the index from 1 to end
var index = try std.fmt.parseInt(usize, load_path[1..], 10); var index = try std.fmt.parseInt(usize, load_path[1..], 10);
// don't care about the 'repl' being prepended when we're in repl // if it isn't in the repl, args look like this:
if (self.repl) index += 1; // 'scritcher ./script ./image'
// if it is, it looks like this
// 'scritcher repl ./script ./image'
var args_it = std.process.args(); // ':0' should ALWAYS point to the image.
_ = args_it.skip(); if (self.repl) index += 3 else index += 2;
var i: usize = 0; var args = try std.process.argsAlloc(self.allocator);
while (i <= index) : (i += 1) { defer std.process.argsFree(self.allocator, args);
_ = args_it.skip(); std.debug.warn("ARGS!! {} \n", .{args.len});
for (args) |arg, idx| {
std.debug.warn("arg{} = {}\n", .{ idx, arg });
} }
std.debug.warn("fetch arg idx={}, val={}\n", .{ index, args[index] });
const arg = try (args_it.next(self.allocator) orelse @panic("expected argument")); return args[index];
return arg;
} else { } else {
return load_path; return load_path;
} }
@ -90,7 +92,7 @@ pub const Runner = struct {
// krita/gimp and make it export a bmp and while in the program you can // krita/gimp and make it export a bmp and while in the program you can
// apply filters, etc. // apply filters, etc.
if (!std.mem.endsWith(u8, load_path, ".bmp") and !std.mem.endsWith(u8, load_path, ".ppm")) { if (!std.mem.endsWith(u8, load_path, ".bmp") and !std.mem.endsWith(u8, load_path, ".ppm")) {
std.debug.warn("Only BMP files are allowed to be loaded.\n", .{}); std.debug.warn("Only BMP files are allowed to be loaded. Got path '{}'\n", .{load_path});
return RunError.NoBMP; return RunError.NoBMP;
} }