Last active
July 26, 2023 08:05
-
-
Save shalithasuranga/74890d46f52fa9e7cadccefa61266a1d to your computer and use it in GitHub Desktop.
A simple CLI written in Zig using only std
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
const std = @import("std"); | |
const stdout = std.io.getStdOut().writer(); | |
fn print_help() !void { | |
try stdout.print("{s}\n" , .{"-" ** 25}); | |
try stdout.print("0: Exit\n", .{}); | |
try stdout.print("1: Show help\n", .{}); | |
try stdout.print("2: Print Node.js version\n", .{}); | |
try stdout.print("{s}\n" , .{"-" ** 25}); | |
} | |
fn print_node_version() !void { | |
const cmd_res = try std.ChildProcess.exec(.{ | |
.allocator = std.heap.page_allocator, | |
.argv = &[_][]const u8{ | |
"node", | |
"--version", | |
}, | |
}); | |
try stdout.print("{s}\n", .{cmd_res.stdout}); | |
} | |
fn ask_action() !i64 { | |
const stdin = std.io.getStdIn().reader(); | |
var buf: [10]u8 = undefined; | |
try stdout.print("Enter action: ", .{}); | |
if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| { | |
return std.fmt.parseInt(i64, user_input, 10); | |
} | |
else { | |
return @as(i64, 0); | |
} | |
} | |
pub fn main() !void { | |
try print_help(); | |
while(true) { | |
const action = ask_action() catch -1; | |
switch(action) { | |
0 => { | |
std.debug.print("Goodbye!\n", .{}); | |
break; | |
}, | |
1 => { | |
try print_help(); | |
}, | |
2 => { | |
try print_node_version(); | |
}, | |
else => { | |
std.debug.print("Invalid action: {d}\n", .{action}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment