diff mbox series

[v2,1/2] parse-options.h: add parse_opt_expiry_date helper

Message ID bcd74559c2474451687c81e97834b13a859d2191.1641223223.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series reflog.c: switch to use parse-options API | expand

Commit Message

John Cai Jan. 3, 2022, 3:20 p.m. UTC
From: John Cai <johncai86@gmail.com>

Extract the logic in parse_opt_expiry_date_cb into a helper
parse_opt_expiry_date. This is to prepare for the following commit where
we need to parse an expiry date that gets passed into a callback
function in opt->value as part of a struct.

The next commit will utilize this helper in a callback function that
aims to wrap the functionality in parse_opt_expiry_date_cb.

Signed-off-by: "John Cai" <johncai86@gmail.com>
---
 parse-options-cb.c | 7 ++++++-
 parse-options.h    | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

Comments

Junio C Hamano Jan. 4, 2022, 2:26 a.m. UTC | #1
"John Cai via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: John Cai <johncai86@gmail.com>
>
> Extract the logic in parse_opt_expiry_date_cb into a helper
> parse_opt_expiry_date. This is to prepare for the following commit where
> we need to parse an expiry date that gets passed into a callback
> function in opt->value as part of a struct.
>
> The next commit will utilize this helper in a callback function that
> aims to wrap the functionality in parse_opt_expiry_date_cb.
>
> Signed-off-by: "John Cai" <johncai86@gmail.com>
> ---
>  parse-options-cb.c | 7 ++++++-
>  parse-options.h    | 1 +
>  2 files changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/parse-options-cb.c b/parse-options-cb.c
> index 3c811e1e4a7..3edb88a54d8 100644
> --- a/parse-options-cb.c
> +++ b/parse-options-cb.c
> @@ -34,10 +34,15 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
>  
>  int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
>  			     int unset)
> +{
> +	return parse_opt_expiry_date((timestamp_t *)opt->value, arg, unset);
> +}
> +
> +int parse_opt_expiry_date(timestamp_t *t, const char *arg, int unset)
>  {
>  	if (unset)
>  		arg = "never";
> -	if (parse_expiry_date(arg, (timestamp_t *)opt->value))
> +	if (parse_expiry_date(arg, t))
>  		die(_("malformed expiration date '%s'"), arg);
>  	return 0;
>  }

Does this even belong to parse-options-cb.c file, though?  It's
interface tells us that it is not limited to the parse-options
infrastructure (i.e. it does not even take "struct option" at all).

I am not sure how having this new function will be helpful to begin
with.  We'll see why it helps by looking at the next step, I
presume, but I wonder if the new caller of this function should just
call parse_expiry_date() itself (with "unset means 'never'").

> diff --git a/parse-options.h b/parse-options.h
> index 275fb440818..0a15bac8619 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -301,6 +301,7 @@ enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
>  					   const char *, int);
>  int parse_opt_passthru(const struct option *, const char *, int);
>  int parse_opt_passthru_argv(const struct option *, const char *, int);
> +int parse_opt_expiry_date(timestamp_t *, const char *, int);
>  
>  #define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
>  #define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))
diff mbox series

Patch

diff --git a/parse-options-cb.c b/parse-options-cb.c
index 3c811e1e4a7..3edb88a54d8 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -34,10 +34,15 @@  int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
 
 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
 			     int unset)
+{
+	return parse_opt_expiry_date((timestamp_t *)opt->value, arg, unset);
+}
+
+int parse_opt_expiry_date(timestamp_t *t, const char *arg, int unset)
 {
 	if (unset)
 		arg = "never";
-	if (parse_expiry_date(arg, (timestamp_t *)opt->value))
+	if (parse_expiry_date(arg, t))
 		die(_("malformed expiration date '%s'"), arg);
 	return 0;
 }
diff --git a/parse-options.h b/parse-options.h
index 275fb440818..0a15bac8619 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -301,6 +301,7 @@  enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
 					   const char *, int);
 int parse_opt_passthru(const struct option *, const char *, int);
 int parse_opt_passthru_argv(const struct option *, const char *, int);
+int parse_opt_expiry_date(timestamp_t *, const char *, int);
 
 #define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
 #define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))