Skip to content

Commit 701e900

Browse files
authored
docs: print usage details to standard output if a help flag is used in command (#7)
1 parent cadef44 commit 701e900

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning][semver].
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Print usage details to standard output if a help flag is used in command.
13+
1014
### Maintenance
1115

1216
- Confirm code formatting matches the standard expectations about language.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ The following command uses `git` details:
1010
$ git coverage # https://app.codecov.io/gh/zimeg/git-coverage
1111
```
1212

13+
### Flags
14+
15+
Options are available for customization:
16+
17+
- `--help`: boolean. Print these usage details. Default: `false`
18+
1319
## Installation
1420

1521
Add a build to `$PATH` for automatic detection.

src/main.zig

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
const std = @import("std");
22

3+
const Flags = struct {
4+
help: bool,
5+
6+
pub fn init(allocator: std.mem.Allocator) !Flags {
7+
var flags = Flags{
8+
.help = false,
9+
};
10+
var args = try std.process.argsWithAllocator(allocator);
11+
_ = args.next();
12+
while (true) {
13+
const opt = args.next();
14+
if (opt) |arg| {
15+
if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
16+
flags.help = true;
17+
break;
18+
} else {
19+
return error.FlagOptionMissing;
20+
}
21+
} else {
22+
break;
23+
}
24+
}
25+
return flags;
26+
}
27+
};
28+
329
pub fn main() !void {
430
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
531
var aa = std.heap.ArenaAllocator.init(gpa.allocator());
632
defer aa.deinit();
733
const allocator = aa.allocator();
34+
const writer = std.io.getStdOut().writer();
35+
const flags = try Flags.init(allocator);
36+
if (flags.help) {
37+
try writer.print(
38+
\\usage: git coverage [options]
39+
\\
40+
\\Open test coverage uploaded to codecov in a web browser.
41+
\\
42+
\\ --help boolean print these usage details (default: false)
43+
\\
44+
, .{});
45+
return;
46+
}
847
const proc = try std.process.Child.run(.{
948
.allocator = allocator,
1049
.argv = &.{ "git", "remote", "-v" },
@@ -19,7 +58,7 @@ pub fn main() !void {
1958
.allocator = allocator,
2059
.argv = &[_][]const u8{ "open", url },
2160
}) catch {
22-
try std.io.getStdOut().writer().print("{s}\n", .{url});
61+
try writer.print("{s}\n", .{url});
2362
};
2463
}
2564

0 commit comments

Comments
 (0)