Skip to content

Commit

Permalink
Add examples/
Browse files Browse the repository at this point in the history
  • Loading branch information
PrajwalCH committed Aug 28, 2022
1 parent 13960e4 commit a10b630
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
59 changes: 59 additions & 0 deletions examples/git.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const std = @import("std");
const yazap = @import("yazap");

const allocator = std.heap.page_allocator;
const flag = yazap.flag;
const Command = yazap.Command;
const Arg = yazap.Arg;

// git init
// git commit -m "message"
// git pull <remote>
// git push <remote> <branch_name>

pub fn main() anyerror!void {
var git = Command.new(allocator, "mygit");
defer git.deinit();

var cmd_commit = Command.new(allocator, "commit");
try cmd_commit.addArg(flag.argOne("message", 'm'));

var cmd_pull = Command.new(allocator, "pull");
try cmd_pull.takesSingleValue("REMOTE");
cmd_pull.argRequired(true);

var cmd_push = Command.new(allocator, "push");
try cmd_push.takesSingleValue("REMOTE");
try cmd_push.takesSingleValue("BRANCH_NAME");
try cmd_push.argRequired(true);

try git.addSubcommand(Command.new(allocator, "init"));
try git.addSubcommand(cmd_commit);
try git.addSubcommand(cmd_pull);
try git.addSubcommand(cmd_push);

var args = try git.parseProcess();
defer args.deinit();

if (args.isPresent("init")) {
std.debug.print("Initilize empty repo", .{});
return;
}

if (args.subcommandContext("commit")) |commit_args| {
if (commit_args.valueOf("message")) |message| {
std.log.info("Commit message {s}", .{message});
return;
}
}

if (args.subcommandContext("push")) |push_args| {
if (push_args.isPresent("REMOTE") and push_args.isPresent("BRANCH_NAME")) {
const remote = push_args.valueOf("REMOTE").?;
const branch_name = push_args.valueOf("BRANCH_NAME").?;

std.log.info("REMOTE={s}, BRANCH_NAME={s}", .{ remote, branch_name });
return;
}
}
}
40 changes: 40 additions & 0 deletions examples/touch.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const std = @import("std");
const yazap = @import("yazap");

const allocator = std.heap.page_allocator;
const flag = yazap.flag;
const Command = yazap.Command;

pub fn main() anyerror!void {
var touch = Command.new(allocator, "mytouch");
defer touch.deinit();

try touch.takesSingleValue("FILE_NAME");
touch.argRequired(true);

try touch.addArg(flag.boolean("no-create", 'c'));
try touch.addArg(flag.boolean("version", 'v'));
try touch.addArg(flag.boolean("help", 'h'));

var args = try touch.parseProcess();
defer args.deinit();

if (args.isPresent("help")) {
std.debug.print("Show help", .{});
return;
}

if (args.isPresent("version")) {
std.debug.print("v0.1.0", .{});
return;
}

if (args.valueOf("FILE_NAME")) |file_name| {
if (args.isPresent("no-create")) {
std.debug.print("I'am not creating it", .{});
} else {
var file = try std.fs.cwd().createFile(file_name);
defer file.close();
}
}
}

0 comments on commit a10b630

Please sign in to comment.