git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Stephen Boyd <bebarino@gmail.com>
Date
Jul 10, 2009, 08:02 UTC
Message-ID
<4A56F58E.1080907@gmail.com>
In-Reply-To
<1247167228-2466-1-git-send-email-michal.kiedrowicz@gmail.com>
Michał Kiedrowicz wrote:
Show 11 quoted lines
> @@ -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.
Show 14 quoted lines
> +
> +	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.

Show 10 quoted lines
> @@ -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.

Previous: Michał Kiedrowicz
Message 6 of 6 in “grep: Add --directories option.”
  1. grep: Add --directories option.Michał Kiedrowicz, Jul 9, 2009
  2. René ScharfeJul 10, 2009
  3. Junio C HamanoJul 10, 2009
  4. Michał KiedrowiczJul 10, 2009
  5. Michał KiedrowiczJul 10, 2009
  6. Stephen BoydJul 10, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.