Skip to content

Commit a77b2b6

Browse files
committed
Add --help and --verbose flags, improve error messaging
1 parent 5740dec commit a77b2b6

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
```text
2-
Usage: findup FILE
2+
findup 1.0
33
4-
Finds a directory containing FILE. Starts at the current working directory, and
5-
searches "up" through each parent directory.
4+
USAGE:
5+
findup FILE
66
7-
When a directory is found, it is printed to stdout.
7+
FLAGS:
8+
-h, --help Prints help information
9+
-V, --version Prints version information
810
9-
If no directory containing FILE is found, nothing is printed, and the
10-
program exits with an exit code of 1.
11+
Finds a directory containing FILE. Tested by filename with exact string equality. Starts searching at the current working directory and recurses "up" through parent directories.
1112
12-
By: J.R. Hill <hiljusti@pm.me>
13+
The first directory containing FILE will be printed. If no directory contains FILE, nothing is printed and the program exits with an exit code of 1.⏎
1314
```
1415

16+
MIT License.
17+
By J.R. Hill (hiljusti (at) so.dang.cool)

src/main.zig

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,43 @@ const Dir = std.fs.Dir;
66
const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES;
77
const AccessError = std.os.AccessError;
88

9-
// TODO: Add --help and --verbose flags
10-
11-
const Findup = struct { program: []u8, target: ?[]u8, cwd: Dir };
9+
const Findup = struct { program: []u8, target: ?[]u8, cwd: Dir, printHelp: bool, printVersion: bool };
1210
const FindupError = error{NoFileSpecified};
1311

12+
const VERSION = "findup 1.0\n";
13+
14+
const USAGE =
15+
\\USAGE:
16+
\\ findup FILE
17+
\\
18+
\\FLAGS:
19+
\\ -h, --help Prints help information
20+
\\ -V, --version Prints version information
21+
\\
22+
\\Finds a directory containing FILE. Tested by filename with exact string equality. Starts searching at the current working directory and recurses "up" through parent directories.
23+
\\
24+
\\The first directory containing FILE will be printed. If no directory contains FILE, nothing is printed and the program exits with an exit code of 1.
25+
\\
26+
;
27+
1428
pub fn main() anyerror!void {
1529
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1630
defer arena.deinit();
1731
var buf: [MAX_PATH_BYTES]u8 = undefined;
1832

1933
const findup = initFindup(&arena.allocator) catch |err| {
20-
try stderr.print("ERROR: {e}\n", .{err});
21-
try stderr.print("Usage: findup FILE\n", .{});
34+
try stderr.print("ERROR: {e}\n{s}", .{ err, USAGE });
2235
std.os.exit(1);
2336
};
2437

38+
if (findup.printHelp) {
39+
try stdout.print("{s}\n{s}", .{ VERSION, USAGE });
40+
std.os.exit(0);
41+
} else if (findup.printVersion) {
42+
try stdout.print(VERSION, .{});
43+
std.os.exit(0);
44+
}
45+
2546
const target = findup.target.?;
2647
var cwd = findup.cwd;
2748

@@ -46,11 +67,10 @@ fn initFindup(allocator: *std.mem.Allocator) anyerror!Findup {
4667
const target = if (maybeTarget == null) return FindupError.NoFileSpecified else try maybeTarget.?;
4768
const cwd = std.fs.cwd();
4869

49-
return Findup{
50-
.program = program,
51-
.target = target,
52-
.cwd = cwd,
53-
};
70+
var printHelp = std.mem.eql(u8, "-h", target) or std.mem.eql(u8, "--help", target);
71+
var printVersion = std.mem.eql(u8, "-V", target) or std.mem.eql(u8, "--version", target);
72+
73+
return Findup{ .program = program, .target = target, .cwd = cwd, .printHelp = printHelp, .printVersion = printVersion };
5474
}
5575

5676
fn dirStr(dir: Dir, buf: []u8) anyerror![]u8 {

0 commit comments

Comments
 (0)