Subject: Re: [PATCH] grep -A/-B/-Cinfinity to get full context

From: Ramkumar Ramachandra <[email-masked]>
Date: Fri, 20 Aug 2010 19:02:09 +0530

Hi Jonathan,

Jonathan Nieder writes:
> Just a proof of concept.
> 
> Cc: René Scharfe <[email-masked]>
> Signed-off-by: Jonathan Nieder <[email-masked]>

Elegant. OPT_INTEGER for integers without the ability to represent
infinity, and OPT_INT_INF for integers with definite representation
for infinity.

> diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt
> index 312e3b2..f152a3f 100644
> --- a/Documentation/technical/api-parse-options.txt
> +++ b/Documentation/technical/api-parse-options.txt
> @@ -160,6 +160,12 @@ There are some macros to easily define options:
>  	Introduce an option with integer argument.
>  	The integer is put into `int_var`.
>  
> +`OPT_INT_INF(short, long, &int_var, inf_val, description)`::
> +	Introduce an option with integer argument.
> +	The integer is put into `int_var`.  If the special value
> +	"inf" (or "infinity") is used as an argument, then `inf_val`
> +	is put into `int_var`.

Nice. You're making the caller pass the `inf_val` argument - this way,
each caller can figure out some integer that's outside its sane range
and use that to represent infinity.

> -		OPT_INTEGER('B', NULL, &opt.pre_context,
> +		OPT_INT_INF('B', NULL, &opt.pre_context, -1,
>  			"show <n> context lines before matches"),
> -		OPT_INTEGER('A', NULL, &opt.post_context,
> +		OPT_INT_INF('A', NULL, &opt.post_context, -1,
>  			"show <n> context lines after matches"),
>  		OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM",
>  			context_callback),

For many cases, -1 is probably a good value to choose.

> diff --git a/parse-options.h b/parse-options.h
> index 7435cdb..6ae041a 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -126,6 +126,9 @@ struct option {
>  #define OPT_STRING(s, l, v, a, h)   { OPTION_STRING,  (s), (l), (v), (a), (h) }
>  #define OPT_UYN(s, l, v, h)         { OPTION_CALLBACK, (s), (l), (v), NULL, \
>  				      (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
> +#define OPT_INT_INF(s, l, v, i, h) \
> +	{ OPTION_CALLBACK, (s), (l), (v), "n", (h), 0, \
> +	  parse_opt_infinity_cb, (intptr_t)(i) }

Okay, you've used the same character "n" for OPT_INTEGER and OPT_INT_INF.

Thanks for the pleasant reading.

-- Ram