-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const std = @import("std"); | ||
|
||
const ParseResult = struct{ | ||
filename: []const u8, | ||
color: ?bool, | ||
uppercase: ?bool, | ||
size: ?bool, | ||
}; | ||
|
||
const Flag = union(enum){ | ||
help, | ||
toggle, | ||
} | ||
|
||
fn printError(comptime fmt: []const u8, args: anytype) noreturn { | ||
std.debug.print("Error: " ++ fmt ++ "\nTip: use `--help` for help\n", args); | ||
std.process.exit(1); | ||
} | ||
|
||
fn printHelp() noreturn { | ||
std.debug.print( | ||
\\hevi - hex viewer | ||
\\ | ||
\\Usage: | ||
\\ hevi <file> [flags] | ||
\\ | ||
\\Flags: | ||
\\ -h, --help Print this help message | ||
\\ --color, --no-color Enable or disable output coloring | ||
\\ --lowercase, --uppercase Switch between lowercase and uppercase hex | ||
\\ --size, --no-size Enable or disable showing the size at the end | ||
\\ | ||
\\Made by Arnau478 | ||
); | ||
std.process.exit(0); | ||
} | ||
|
||
pub fn parse(args) ParseResult { | ||
var filename: ?[]const u8 = null; | ||
|
||
for(args) |arg| { | ||
if(arg[0] == '-') { | ||
if(arg.len <= 1) { | ||
printError("expected flag", .{}); | ||
} | ||
switch(arg[1]) { | ||
'h' => { | ||
printHelp(); | ||
}, | ||
'-' => { | ||
const name = arg[2..]; | ||
if(name.len == 0) printError("expected flag name", .{}); | ||
|
||
const flags: []Flag = &.{ | ||
|
||
}; | ||
}, | ||
else => |f| printError("invalid flag `{s}`", .{arg}), | ||
} | ||
} | ||
else { | ||
if(filename) { | ||
printError("multiple files specified", .{}); | ||
} | ||
else filename = arg; | ||
} | ||
} | ||
|
||
return .{ | ||
.filename = filename orelse printError("no file specified", .{}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters