Thread (4 messages) flat view 4 messages, 3 authors, 2016-06-15

Re: [PATCH/RFC] grep: Add --directories option.

From: Stephen Boyd <hidden>
Date: 2016-06-15 22:47:02

Michał Kiedrowicz wrote:
quoted hunk ↗ jump to hunk
@@ -648,10 +658,32 @@ static int help_callback(const struct option *opt, const char *arg, int unset)
 	return -1;
 }
 
+static int directories_callback(const struct option *opt,
+				const char *arg, int unset)
+{
+	int *recurse = opt->value;
+
+	if (!arg)
+		return error("switch `d' requires a value");
This isn't needed because OPT_CALLBACK requires an argument always be given.
+
+	if (!strcmp(arg, "recurse")) {
+		*recurse = 1;
+		return 0;
+	} else if (!strcmp(arg, "skip")) {
+		*recurse = 0;
+		return 0;
+	}
+
+	fprintf(stderr, "Invalid action `%s'.\n", arg);
+	fprintf(stderr, "Available actions are: recurse skip.\n");
+	return -1;
+}
+
I think I would drop the two fprintf's here and just return an error()
saying invalid action. This will in turn cause the usage message to show
up, where you can show the two possible actions.
quoted hunk ↗ jump to hunk
@@ -674,6 +706,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_SET_INT('I', NULL, &opt.binary,
 			"don't match patterns in binary files",
 			GREP_BINARY_NOMATCH),
+		OPT_CALLBACK('d', "directories", &recurse, "action",
+			"action to perform when input file is a directory",
+			directories_callback),
 		OPT_GROUP(""),
 		OPT_BIT('E', "extended-regexp", &opt.regflags,
 			"use extended POSIX regular expressions", REG_EXTENDED),
Do you want to allow "--no-directories", I would suggest setting the
PARSE_OPT_NONEG flag to avoid this. Maybe you want to replace "action"
with "recurse|skip" too.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help