diff mbox

[1/2] ndctl, util: add OPT_STRING_LIST to parse_option

Message ID 20180417063848.5585-2-qi.fuli@jp.fujitsu.com
State New, archived
Headers show

Commit Message

QI Fuli April 17, 2018, 6:38 a.m. UTC
This patch adds OPT_STRING_LIST to parse_option in order to support
multiple space seperated string objects in one option.

Signed-off-by: QI Fuli <qi.fuli@jp.fujitsu.com>
---
 ccan/list/list.h     |  6 ++++++
 util/parse-options.c | 25 +++++++++++++++++++++++++
 util/parse-options.h |  3 +++
 3 files changed, 34 insertions(+)

Comments

Dan Williams April 17, 2018, 2:56 p.m. UTC | #1
On Mon, Apr 16, 2018 at 11:38 PM, QI Fuli <qi.fuli@jp.fujitsu.com> wrote:
> This patch adds OPT_STRING_LIST to parse_option in order to support
> multiple space seperated string objects in one option.
>
> Signed-off-by: QI Fuli <qi.fuli@jp.fujitsu.com>
> ---
>  ccan/list/list.h     |  6 ++++++
>  util/parse-options.c | 25 +++++++++++++++++++++++++
>  util/parse-options.h |  3 +++
>  3 files changed, 34 insertions(+)
>
> diff --git a/ccan/list/list.h b/ccan/list/list.h
> index 4d1d34e..f6c927f 100644
> --- a/ccan/list/list.h
> +++ b/ccan/list/list.h
> @@ -26,6 +26,12 @@ struct list_node
>         struct list_node *next, *prev;
>  };
>
> +struct string_list_node
> +{
> +       char *str;
> +       struct list_node list;
> +};
> +
>  /**
>   * struct list_head - the head of a doubly-linked list
>   * @h: the list_head (containing next and prev pointers)
> diff --git a/util/parse-options.c b/util/parse-options.c
> index 751c091..cac18f0 100644
> --- a/util/parse-options.c
> +++ b/util/parse-options.c
> @@ -20,6 +20,7 @@
>  #include <util/util.h>
>  #include <util/strbuf.h>
>  #include <util/parse-options.h>
> +#include <ccan/list/list.h>
>
>  #define OPT_SHORT 1
>  #define OPT_UNSET 2
> @@ -695,3 +696,27 @@ int parse_opt_verbosity_cb(const struct option *opt,
>         }
>         return 0;
>  }
> +
> +int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
> +{
> +       if (unset)
> +               return 0;
> +
> +       if (!arg)
> +               return -1;
> +
> +       struct list_head *v = opt->value;
> +       char *temp = strdup(arg);
> +       const char *deli = " ";
> +
> +       temp = strtok(temp, deli);
> +       while (temp != NULL) {
> +               struct string_list_node *sln = malloc(sizeof(*sln));
> +               sln->str = temp;
> +               list_add_tail(v, &sln->list);
> +               temp = strtok(NULL, deli);
> +       }
> +
> +       free(temp);
> +       return 0;
> +}

As far as I can see we do not need to allocate a list or add this new
OPT_STRING_LIST argument type. Just teach the util_<object>_filter()
routines that the 'ident' argument may be a space delimited list.  See
the attached patch:
QI Fuli April 17, 2018, 3:29 p.m. UTC | #2
> -----Original Message-----

> From: Dan Williams [mailto:dan.j.williams@intel.com]

> Sent: Tuesday, April 17, 2018 11:57 PM

> To: Qi, Fuli/斉 福利 <qi.fuli@jp.fujitsu.com>

> Cc: linux-nvdimm <linux-nvdimm@lists.01.org>

> Subject: Re: [PATCH 1/2] ndctl, util: add OPT_STRING_LIST to parse_option

> 

> On Mon, Apr 16, 2018 at 11:38 PM, QI Fuli <qi.fuli@jp.fujitsu.com> wrote:

> > This patch adds OPT_STRING_LIST to parse_option in order to support

> > multiple space seperated string objects in one option.

> >

> > Signed-off-by: QI Fuli <qi.fuli@jp.fujitsu.com>

> > ---

> >  ccan/list/list.h     |  6 ++++++

> >  util/parse-options.c | 25 +++++++++++++++++++++++++

> > util/parse-options.h |  3 +++

> >  3 files changed, 34 insertions(+)

> >

> > diff --git a/ccan/list/list.h b/ccan/list/list.h index

> > 4d1d34e..f6c927f 100644

> > --- a/ccan/list/list.h

> > +++ b/ccan/list/list.h

> > @@ -26,6 +26,12 @@ struct list_node

> >         struct list_node *next, *prev;  };

> >

> > +struct string_list_node

> > +{

> > +       char *str;

> > +       struct list_node list;

> > +};

> > +

> >  /**

> >   * struct list_head - the head of a doubly-linked list

> >   * @h: the list_head (containing next and prev pointers) diff --git

> > a/util/parse-options.c b/util/parse-options.c index 751c091..cac18f0

> > 100644

> > --- a/util/parse-options.c

> > +++ b/util/parse-options.c

> > @@ -20,6 +20,7 @@

> >  #include <util/util.h>

> >  #include <util/strbuf.h>

> >  #include <util/parse-options.h>

> > +#include <ccan/list/list.h>

> >

> >  #define OPT_SHORT 1

> >  #define OPT_UNSET 2

> > @@ -695,3 +696,27 @@ int parse_opt_verbosity_cb(const struct option *opt,

> >         }

> >         return 0;

> >  }

> > +

> > +int parse_opt_string_list(const struct option *opt, const char *arg,

> > +int unset) {

> > +       if (unset)

> > +               return 0;

> > +

> > +       if (!arg)

> > +               return -1;

> > +

> > +       struct list_head *v = opt->value;

> > +       char *temp = strdup(arg);

> > +       const char *deli = " ";

> > +

> > +       temp = strtok(temp, deli);

> > +       while (temp != NULL) {

> > +               struct string_list_node *sln = malloc(sizeof(*sln));

> > +               sln->str = temp;

> > +               list_add_tail(v, &sln->list);

> > +               temp = strtok(NULL, deli);

> > +       }

> > +

> > +       free(temp);

> > +       return 0;

> > +}

> 

> As far as I can see we do not need to allocate a list or add this new

> OPT_STRING_LIST argument type. Just teach the util_<object>_filter() routines

> that the 'ident' argument may be a space delimited list.  See the attached patch:


Thank you for your comment.

The OPT_STRING_LIST is copied from git.
Consider multiple arguments per option should be supported not only in
monitor and list but also in other commands, as Vishal mentioned:
  "ndctl disable-namespace namespace1.0 namespace2.0 ..."

If you think this feature is not needed in other commands, I will delete
OPT_STRING_LIST and make a v2 patch.
diff mbox

Patch

diff --git a/ccan/list/list.h b/ccan/list/list.h
index 4d1d34e..f6c927f 100644
--- a/ccan/list/list.h
+++ b/ccan/list/list.h
@@ -26,6 +26,12 @@  struct list_node
 	struct list_node *next, *prev;
 };
 
+struct string_list_node
+{
+	char *str;
+	struct list_node list;
+};
+
 /**
  * struct list_head - the head of a doubly-linked list
  * @h: the list_head (containing next and prev pointers)
diff --git a/util/parse-options.c b/util/parse-options.c
index 751c091..cac18f0 100644
--- a/util/parse-options.c
+++ b/util/parse-options.c
@@ -20,6 +20,7 @@ 
 #include <util/util.h>
 #include <util/strbuf.h>
 #include <util/parse-options.h>
+#include <ccan/list/list.h>
 
 #define OPT_SHORT 1
 #define OPT_UNSET 2
@@ -695,3 +696,27 @@  int parse_opt_verbosity_cb(const struct option *opt,
 	}
 	return 0;
 }
+
+int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
+{
+	if (unset)
+		return 0;
+
+	if (!arg)
+		return -1;
+
+	struct list_head *v = opt->value;
+	char *temp = strdup(arg);
+	const char *deli = " ";
+
+	temp = strtok(temp, deli);
+	while (temp != NULL) {
+		struct string_list_node *sln = malloc(sizeof(*sln));
+		sln->str = temp;
+		list_add_tail(v, &sln->list);
+		temp = strtok(NULL, deli);
+	}
+
+	free(temp);
+	return 0;
+}
diff --git a/util/parse-options.h b/util/parse-options.h
index 6fd6b24..10b79cb 100644
--- a/util/parse-options.h
+++ b/util/parse-options.h
@@ -135,6 +135,8 @@  struct option {
 #define OPT_LONG(s, l, v, h)        { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
 #define OPT_U64(s, l, v, h)         { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
 #define OPT_STRING(s, l, v, a, h)   { .type = OPTION_STRING,  .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) }
+#define OPT_STRING_LIST(s, l, v, a, h) \
+	{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = check_vtype(v, struct list_head *), (a), .help = (h) , 0, &parse_opt_string_list }
 #define OPT_DATE(s, l, v, h) \
 	{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
 #define OPT_CALLBACK(s, l, v, a, h, f) \
@@ -206,6 +208,7 @@  extern int parse_options_end(struct parse_opt_ctx_t *ctx);
 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
 extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
 extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
+extern int parse_opt_string_list(const struct option *, const char *, int);
 
 #define OPT__VERBOSE(var)  OPT_BOOLEAN('v', "verbose", (var), "be verbose")
 #define OPT__QUIET(var)    OPT_BOOLEAN('q', "quiet",   (var), "be quiet")