aboutsummaryrefslogtreecommitdiff
path: root/src/argument.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/argument.zig')
-rw-r--r--src/argument.zig21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/argument.zig b/src/argument.zig
index 37d9045..b553352 100644
--- a/src/argument.zig
+++ b/src/argument.zig
@@ -22,6 +22,8 @@ fn help(err: ?anyerror) noreturn {
\\
\\ -n, --non-empty Only output texts which are non-empty.
\\
+ \\ -m, --min-length n Minimum length of sentential string.
+ \\
\\ recognize [grammar] [options]
\\ Options:
\\ -i, --input entry Specify input source, if the path
@@ -74,12 +76,13 @@ pub const Entry = struct {
name: []const u8,
file: std.fs.File,
- pub fn open(path: []const u8, flags: std.fs.File.OpenFlags) Self {
+ pub fn open(path: []const u8, writer: bool) Self {
var cwd = std.fs.cwd();
return Self {
.name = path,
- .file = cwd.openFile(path, flags) catch |e| help(e)
+ .file = (if (writer) cwd.createFile(path, .{})
+ else cwd.openFile(path, .{})) catch |e| help(e)
};
}
@@ -115,7 +118,7 @@ pub const RecognizeArgs = struct {
pub const GenerateArgs = struct {
count: usize,
- empty: bool,
+ min_length: usize,
output: Entry,
grammar: Grammar,
};
@@ -145,7 +148,7 @@ pub const Args = union(Mode) {
while (args.next()) |arg| {
if (check_flags(arg, &[_][]const u8 { "-i", "--input" })) {
- input = Entry.open(check(args.next()), .{});
+ input = Entry.open(check(args.next()), false);
} else help(error.InvalidArgument);
}
@@ -160,22 +163,24 @@ pub const Args = union(Mode) {
.generate => {
var count: usize = 1;
var output: ?Entry = null;
- var empty: bool = true;
+ var min_length: usize = 0;
while (args.next()) |arg| {
if (check_flags(arg, &[_][]const u8 { "-o", "--output" })) {
- output = Entry.open(check(args.next()), .{ .mode = .write_only });
+ output = Entry.open(check(args.next()), true);
} else if (check_flags(arg, &[_][]const u8 { "-c", "--count" })) {
count = parse_int(check(args.next()));
} else if (check_flags(arg, &[_][]const u8 { "-n", "--non-empty" })) {
- empty = false;
+ min_length = 1;
+ } else if (check_flags(arg, &[_][]const u8 { "-m", "--min-length" })) {
+ min_length = parse_int(check(args.next()));
} else help(error.InvalidArgument);
}
return Self {
.generate = .{
.count = count,
- .empty = empty,
+ .min_length = min_length,
.output = output orelse Entry.stdout(),
.grammar = grammar,
},