Skip to contentSkip to conversation

qemu-arg: general purpose argument parser

RFC PATCH In review Leandro Dorileo 6 participants 15 messages Mar 8, 2014 – Mar 16, 2014 Kbuild

lore
Share & tools
lore
#0 lore raw

cover qemu-arg: general purpose argument parser

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

Leandro Dorileo (2):
qemu-arg: introduce a general purpose argument parser
qemu-img: migrate to use qemu-arg

 .gitignore              |    1 +
 Makefile                |   12 +-
 include/qemu/qemu-arg.h |  287 ++++++++++++
 qemu-img-cmds.hx        |   77 ---
 qemu-img-descs.h        |  128 +++++
 qemu-img.c              | 1184 ++++++++++++++++-------------------------------
 util/Makefile.objs      |    1 +
 util/qemu-arg.c         |  887 +++++++++++++++++++++++++++++++++++
 8 files changed, 1706 insertions(+), 871 deletions(-)
 create mode 100644 include/qemu/qemu-arg.h
 delete mode 100644 qemu-img-cmds.hx
 create mode 100644 qemu-img-descs.h
 create mode 100644 util/qemu-arg.c
Signature · 1 line
1.9.0
#1 lore raw

RFC PATCH 1/2 qemu-arg: introduce a general purpose argument parser

qemu-arg defines a standardized API for argument parsing, help displaying and texi generation/sync.

The implementation supports command + arguments form (i.e qemu-img requirements) and a more general simple arguments parsing. So we can parse:

$ prog <command> --arg1 --arg2
$ prog --arg1 --arg2

We support the following:
+ basic arguments validation (i.e required arguments and required values);
+ basic arguments transformations (integer, bool values)
+ repeated/"cumullated" arguments (i.e -o opt1=val -o opt2=val2 will result the

     string "opt1=val,opt2=val2")

+ positional arguments;
+ identified positional for fixed/defined numbers of expected positional args;
+ listed positional for N expected positional args;
+ help messages generation;
+ texi generation;
+ default value setting;
+ mutually exclusive arguments;
+ display and parsing decorated arguments ("--argument value" and "--argument=value" are valid)

---
 include/qemu/qemu-arg.h | 287 ++++++++++++++++
 util/Makefile.objs      |   1 +
 util/qemu-arg.c         | 887 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1175 insertions(+)
 create mode 100644 include/qemu/qemu-arg.h
 create mode 100644 util/qemu-arg.c
include/qemu/qemu-arg.h new file +287 -0
new file mode 100644
index 0000000..c8d8fb4
@@ -0,0 +1,287 @@
+/*
+ * QEMU argument helper
+ *
+ * Copyright (c) 2014 Leandro Dorileo <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef _QEMU_ARG_H_
+#define _QEMU_ARG_H_
+
+#include <libintl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct _QemuArgContext QemuArgContext;
+typedef struct _QemuArgCommand QemuArgCommand;
+
+typedef enum _QemuArgOptType {
+    QEMU_ARG_OPT_TYPE_INT,
+    QEMU_ARG_OPT_TYPE_BOOL,
+    QEMU_ARG_OPT_TYPE_STR,
+    QEMU_ARG_OPT_TYPE_POSITIONAL,
+    QEMU_ARG_OPT_TYPE_POSITIONAL_LIST,
+    QEMU_ARG_OPT_TYPE_DEPRECATED,
+    QEMU_ARG_OPT_TYPE_GROUP,
+    QEMU_ARG_OPT_TYPE_SENTINEL,
+} QemuArgOptType;
+
+typedef struct _QemuArgIntValue {
+    /** default value */
+    int def_val;
+
+    /** user value pointer */
+    int *value;
+} QemuArgIntValue;
+
+typedef struct _QemuArgBoolValue {
+    /** default value */
+    bool def_val;
+
+    /** user value pointer */
+    bool *value;
+} QemuArgBoolValue;
+
+typedef struct _QemuArgStrValue {
+    /** default value */
+    char *def_val;
+
+    /** user value pointer */
+    char **value;
+} QemuArgStrValue;
+
+typedef struct _QemuArgStrListValue {
+    /** default value */
+    char **def_val;
+
+    /** user value pointer */
+    char ***value;
+
+    /** list elements counter */
+    int list_cnt;
+} QemuArgStrListValue;
+
+typedef enum _QemuArgOptFlag {
+    ARG_FLAG_NONE       = 0,
+
+    /** provide many arguments instances, their value are concatenated in a
+        comman separated string */
+    ARG_FLAG_CUMULATE   = 1 << 0,
+
+    /** the argument is required */
+    ARG_FLAG_REQUIRED   = 1 << 1,
+
+    /** the argument requires a value like --foo bar where --foo requires bar */
+    ARG_FLAG_REQ_VALUE  = 1 << 2,
+} QemuArgOptFlag;
+
+typedef struct _QemuArgOpt {
+    /** argument type, bool, int, str, etc @see QemuArgOptType */
+    QemuArgOptType type;
+
+    /** the argument's short name i.e -c */
+    const char short_name;
+
+    /** argument's long name i.e --cache */
+    const char *long_name;
+
+    /** argument's description, used to display a hint about the argument's
+        value i.e -f fmt where fmt is the arg's desc */
+    const char *desc;
+
+    /** help message, describes the argument */
+    const char *help;
+
+    /** some behavior flags @see QemuArgOptFlag for possible modifiers */
+    int flags;
+
+    /** indicates the argument was set, for bool values it tells we got the
+        argument and the others we got the argument's value */
+    bool value_set;
+
+    /** value pointer, @see QemuArgIntValue @see QemuArgBoolValue
+        @see QemuArgStrValue @see QemuArgStrListValue */
+    const void *value;
+} QemuArgOpt;
+
+struct _QemuArgCommand {
+    /** the command itself i.e commit, create */
+    const char *cmd;
+
+    /** help msg displayed on main help msg - command listing msg */
+    const char *help;
+
+    /** the command's arguments */
+    QemuArgOpt *args;
+
+    /** number of arguments */
+    int args_cnt;
+
+    /** mutual exclusive groups - it's an array of QemuArgOpt arrays, each array
+        composes arguments mutually exclusive */
+    QemuArgOpt **mutual_groups;
+
+    int (* cb)(const QemuArgContext *ctx, const QemuArgCommand *cmd);
+};
+
+struct _QemuArgContext {
+    /** text displayed on top of help msg */
+    const char *prologue;
+
+    /** text displayed on bottom of help msg */
+    const char *epilogue;
+
+    /** the program name, usage on "usage" msg */
+    const char *prog_name;
+
+    /** command list */
+    const QemuArgCommand *commands;
+
+    /** non command usage or common arguments */
+    QemuArgOpt *args;
+
+    /** number of arguments (non command usage) */
+    int args_cnt;
+
+    /** non command usage */
+    QemuArgOpt **mutual_groups;
+
+    /** support decorated mode, like --arg=value. actually we'll always parse
+     this form, this flag will indicate we must show the long options on help
+    messages @see OPT_DECORATE_LONG and @see OPT_DECORATE_SHORT */
+    int decorate;
+};
+
+/** decoration modes, OPT_DECORATE_LONG | OPT_DECORATE_SHORT will decorate both
+    short and long arguments */
+#define OPT_DECORATE_LONG  (1 << 0)
+#define OPT_DECORATE_SHORT (1 << 1)
+
+/** type wrappers macros */
+#define INT_VALUE(_val, _default)                               \
+    &(QemuArgIntValue) {.value = _val, .def_val = _default}     \
+
+#define BOOL_VALUE(_val, _default)                                      \
+    &(QemuArgBoolValue) {.value = _val, .def_val = _default}            \
+
+#define STR_VALUE(_val, _default)                                       \
+    &(QemuArgStrValue) {.value = _val, .def_val = (char *)_default}     \
+
+#define STR_LIST_VALUE(_val, _default)                                  \
+    &(QemuArgStrListValue) {.value = _val, .def_val = _default}         \
+
+/** QemuArgOpt constructors */
+#define OPT_BOOL(s, l, h, p, df)                                        \
+    {QEMU_ARG_OPT_TYPE_BOOL, s, l, NULL, h, ARG_FLAG_NONE, false,       \
+            BOOL_VALUE(p, df)}                                          \
+
+#define OPT_STR(s, l, d, h, p, df)                                      \
+    {QEMU_ARG_OPT_TYPE_STR, s, l, d, h, ARG_FLAG_REQ_VALUE, false,      \
+            STR_VALUE(p, df)}                                           \
+
+#define OPT_INT(s, l, d, h, p, df)                                      \
+    {QEMU_ARG_OPT_TYPE_INT, s, l, d, h, ARG_FLAG_REQ_VALUE, false,      \
+            INT_VALUE(p, df)}                                           \
+
+#define OPT_CUMUL_STR(s, l, d, h, p, df)                                \
+    {QEMU_ARG_OPT_TYPE_STR, s, l, d, h,                                 \
+            ARG_FLAG_CUMULATE | ARG_FLAG_REQ_VALUE, false,              \
+            STR_VALUE(p, df)}                                           \
+
+#define OPT_CUMUL_STR_REQ(s, l, d, h, p, df)                            \
+    {QEMU_ARG_OPT_TYPE_STR, s, l, d, h,                                 \
+            ARG_FLAG_CUMULATE | ARG_FLAG_REQ_VALUE | ARG_FLAG_REQUIRED, \
+            false, STR_VALUE(p, df)}                                     \
+
+#define OPT_POS(d, h, p, df)                                            \
+    {QEMU_ARG_OPT_TYPE_POSITIONAL, 0, NULL, d, h, ARG_FLAG_NONE,        \
+            false, STR_VALUE(p, df)}                                    \
+
+#define OPT_POS_REQ(d, h, p, df)                                        \
+    {QEMU_ARG_OPT_TYPE_POSITIONAL, 0, NULL, d, h, ARG_FLAG_REQUIRED,    \
+            false, STR_VALUE(p, df)}                                    \
+
+#define OPT_POS_LIST(d, h, p, df)                                       \
+    {QEMU_ARG_OPT_TYPE_POSITIONAL_LIST, 0, NULL, d, h, ARG_FLAG_NONE,   \
+            false, STR_LIST_VALUE(p, df)}                               \
+
+#define OPT_POS_LIST_REQ(d, h, p, df)                                   \
+    {QEMU_ARG_OPT_TYPE_POSITIONAL_LIST, 0, NULL, d, h,                  \
+            ARG_FLAG_REQUIRED, false, STR_LIST_VALUE(p, df)}            \
+
+#define OPT_DEP(s, l, h)                                                \
+    {QEMU_ARG_OPT_TYPE_DEPRECATED, s, l, NULL, h, ARG_FLAG_NONE}        \
+
+#define OPT_GROUP(d)                                                    \
+    {QEMU_ARG_OPT_TYPE_GROUP, 0, NULL, d, NULL, ARG_FLAG_NONE}          \
+
+/** command  and context macro constructor */
+#define OPT_CMD(c, h, a, m, cb)                          \
+    {c, h, a, sizeof(a) / sizeof(QemuArgOpt), m, cb}     \
+
+#define OPT_CTX(n, p, e, pn, c, a, m, d)                                \
+    const QemuArgContext n = {p, e, pn, c, a,                           \
+                              sizeof(a) / sizeof(QemuArgOpt), m, d}     \
+
+#define OPT_MUTUAL_GROUP_SENTINEL NULL
+#define OPT_SENTINEL { QEMU_ARG_OPT_TYPE_SENTINEL }
+#define CMD_SENTINEL { NULL }
+
+/**
+ *
+ * @param argc The program arguments counter
+ * @param argv The program arguments vector
+ * @param ctx The qemu arg context of interest
+ *
+ * Parse commands.
+ *
+ * @return negative number on failure, 0 otherwise.
+ */
+int qemu_arg_parse(int argc, char *argv[], const QemuArgContext *ctx);
+
+/**
+ * @param ctx the context to be cleaned up
+ *
+ * Clean up the argument context.
+ */
+void qemu_arg_context_cleanup(const QemuArgContext *ctx);
+
+/**
+ * @param cmd The command of interest
+ *
+ * Query a command about its parsed positional arguments list.
+ *
+ * @return How many items a positional list has. @see OPT_POS_LIST and
+ * @see OPT_POS_LIST_REQ
+ */
+int qemu_arg_get_command_positional_list_cnt(const QemuArgCommand *cmd);
+
+/**
+ * @param ctx The qemu arg context of interest
+ * @param cmd The qemu command of interest
+ *
+ * Print a command help message.
+ */
+void qemu_arg_print_command_help(const QemuArgContext *ctx,
+                                 const QemuArgCommand *cmd);
+
+#endif /* _QEMU_ARG_H_ */
util/Makefile.objs +1 -0
index 937376b..9d05ec3 100644
@@ -14,3 +14,4 @@ util-obj-y += crc32c.o
 util-obj-y += throttle.o
 util-obj-y += getauxval.o
 util-obj-y += readline.o
+util-obj-y += qemu-arg.o
util/qemu-arg.c new file +887 -0

887 lines not on this page · raw message

Signature · 1 line
1.9.0
#2 lore raw

RFC PATCH 2/2 qemu-img: migrate to use qemu-arg

In reply to Leandro Dorileo

Remove the arg parsing implementations using getopt and use qemu-arg. Also remove the qemu-img-cmds.hx since it's now generated on building time, adapted the build system to generate the .hx file using the qemu-img itself using the qemu-arg internal command generate-hx.

---
 .gitignore       |    1 +
 Makefile         |   12 +-
 qemu-img-cmds.hx |   77 ----
 qemu-img-descs.h |  128 ++++++
 qemu-img.c       | 1184 ++++++++++++++++++------------------------------------
 5 files changed, 531 insertions(+), 871 deletions(-)
 delete mode 100644 qemu-img-cmds.hx
 create mode 100644 qemu-img-descs.h
.gitignore +1 -0
index ef7019f..700e92d 100644
@@ -116,3 +116,4 @@ cscope.*
 tags
 TAGS
 *~
+qemu-img-cmds.hx
Makefile +5 -7
index bd9cd4f..0f2f102 100644
@@ -214,8 +214,6 @@ util/module.o-cflags = -D'CONFIG_BLOCK_MODULES=$(block-modules)'
 
 ######################################################################
 
-qemu-img.o: qemu-img-cmds.h
-
 qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
 qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
 qemu-io$(EXESUF): qemu-io.o $(block-obj-y) libqemuutil.a libqemustub.a
@@ -225,9 +223,6 @@ qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
 fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o libqemuutil.a libqemustub.a
 fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
 
-qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
-	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
-
 qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
 qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
 
@@ -272,7 +267,7 @@ clean:
 	rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 	rm -f fsdev/*.pod
 	rm -rf .libs */.libs
-	rm -f qemu-img-cmds.h
+	rm -f qemu-img-cmds.hx
 	@# May not be present in GENERATED_HEADERS
 	rm -f trace/generated-tracers-dtrace.dtrace*
 	rm -f trace/generated-tracers-dtrace.h*
@@ -442,7 +437,10 @@ qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
 qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@,"  GEN   $@")
 
-qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
+qemu-img-cmds.hx: qemu-img$(EXESUF)
+	$(call quiet-command,$(SRC_PATH)/qemu-img generate-hx -h > $(SRC_PATH)/$@,"  GEN   $@")
+
+qemu-img-cmds.texi: qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 
 qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi
qemu-img-cmds.hx deleted +0 -77

77 lines not on this page · raw message

qemu-img-descs.h new file +128 -0

128 lines not on this page · raw message

qemu-img.c +397 -787

1,768 lines not on this page · raw message

Signature · 1 line
1.9.0
#3 lore raw

Re: [Qemu-devel] [PATCH RFC 2/2] qemu-img: migrate to use qemu-arg

Il 08/03/2014 19:47, Leandro Dorileo ha scritto:

Quoted 6 lines

Remove the arg parsing implementations using getopt and use qemu-arg. Also remove the qemu-img-cmds.hx since it's now generated on building time, adapted the build system to generate the .hx file using the qemu-img itself using the qemu-arg internal command generate-hx.

This makes it much harder to cross-compile QEMU. Also, I wonder how hard it would be to apply the same approach to the main QEMU binary which already uses QemuOpts for its more complex arguments; for sure you risk that accumulating multiple layers of abstractions makes the code even harder to read than it is now.

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH RFC 2/2] qemu-img: migrate to use qemu-arg

Hi Paolo,

Quoted 9 lines · Paolo Bonzini

Il 08/03/2014 19:47, Leandro Dorileo ha scritto:

Quoted 6 lines

Remove the arg parsing implementations using getopt and use qemu-arg. Also remove the qemu-img-cmds.hx since it's now generated on building time, adapted the build system to generate the .hx file using the qemu-img itself using the qemu-arg internal command generate-hx.

This makes it much harder to cross-compile QEMU.

What's non-portable in this case? what would limit the QEMU cross-compile?

Quoted 3 lines

Also, I wonder how hard it
would be to apply the same approach to the main QEMU binary which already uses QemuOpts for its more complex arguments;

Yeah, you're right, QEMU binary is much more complex, In that case I think we should put QemuOpts and QemuArg together or so, I still need to better understand the current vl.c + QemuOpt source code to come up with a good solution.

Quoted 3 lines

for sure you risk that
accumulating multiple layers of abstractions makes the code even harder to read than it is now.

The idea is to keep things simple not the other way round. I think it's possible to accommodate both cases without imposing more complexity.

Regards...

Signature · 1 line
Leandro Dorileo
#5 lore raw

Re: [Qemu-devel] [PATCH RFC 2/2] qemu-img: migrate to use qemu-arg

Quoted 9 lines · Leandro Dorileo

Hi Paolo,

Quoted 6 lines · Paolo Bonzini

Il 08/03/2014 19:47, Leandro Dorileo ha scritto:

Quoted 5 lines

Remove the arg parsing implementations using getopt and use qemu-arg. Also remove the qemu-img-cmds.hx since it's now generated on building time, adapted the build system to generate the .hx file using the qemu-img itself using the qemu-arg internal command generate-hx.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This makes it much harder to cross-compile QEMU.

What's non-portable in this case? what would limit the QEMU cross-compile?

qemu-img is a binary for the target system; it can't run at all on the host system, so you can't run it during compilation. You need to keep "things we do during build" completely separate from the runtime binaries.

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH RFC 2/2] qemu-img: migrate to use qemu-arg

Quoted 12 lines · Peter Maydell
Quoted 11 lines · Leandro Dorileo

Hi Paolo,

Quoted 8 lines · Paolo Bonzini

Il 08/03/2014 19:47, Leandro Dorileo ha scritto:

Quoted 6 lines

Remove the arg parsing implementations using getopt and use qemu-arg. Also remove the qemu-img-cmds.hx since it's now generated on building time, adapted the build system to generate the .hx file using the qemu-img itself using the qemu-arg internal command generate-hx.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

This makes it much harder to cross-compile QEMU.

What's non-portable in this case? what would limit the QEMU cross-compile?

qemu-img is a binary for the target system; it can't run at all on the host system, so you can't run it during compilation. You need to keep "things we do during build" completely separate from the runtime binaries.

Yep, true.

Signature · 1 line
Leandro Dorileo
#7 lore raw
In reply to Leandro Dorileo
Quoted 3 lines · Leandro Dorileo

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

Can you describe what the QEMU-specific features are that mean we must roll our own argument-parsing infrastructure rather than using (say) the glib option parsing routines?

thanks
-- PMM

#8 lore raw

Hi Peter,

Quoted 8 lines · Peter Maydell
Quoted 3 lines · Leandro Dorileo

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

Can you describe what the QEMU-specific features are that mean we must roll our own argument-parsing infrastructure rather than using (say) the glib option parsing routines?

I don't think GOption will do the output I want and think to be the ideal for qemu or specially to qemu-img.

GOption knows nothing about command, since qemu-img was my first target I wanted something to handle its command schema. GOption will not show the users the list of available commands neither show commands specific options, I wanted the user to run qemu-img create -h and show the create command arguments (similar to git output). GOption will not do that by default, of course we can wrap that and have something similar.

GOption will not be able to list commands and their arguments so we can generate the .hx file (see patch 02 in my series) or maybe the texi output and keep the sync between the implemented commands, arguments and the textinfo stuffs. GOption also doesn't know about the "cumulative" stuff - well I agree this last one is not somethig to justify qemu-arg per se.

The command callbacks flow is also something GOption will not give us for free, we would still need to know ourself about the available commands and their callees.

As I said, my first target was qemu-img, but I ended up writing something more generic to be used elsewhere.

Of course I could wrap GOption or getopt and handle all the corner cases but parsing the arguments myself gave me more flexibility.

Regards...

Signature · 1 line
Leandro Dorileo
#9 lore raw
In reply to Leandro Dorileo

Am 08.03.2014 19:47, schrieb Leandro Dorileo:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

Why? :) You forgot to describe what's wrong with the current infrastructure, how your approach is different and what the benefit is.

Regards,
Andreas

Quoted 18 lines

Leandro Dorileo (2):
qemu-arg: introduce a general purpose argument parser
qemu-img: migrate to use qemu-arg

 .gitignore              |    1 +
 Makefile                |   12 +-
 include/qemu/qemu-arg.h |  287 ++++++++++++
 qemu-img-cmds.hx        |   77 ---
 qemu-img-descs.h        |  128 +++++
 qemu-img.c              | 1184 ++++++++++++++++-------------------------------
 util/Makefile.objs      |    1 +
 util/qemu-arg.c         |  887 +++++++++++++++++++++++++++++++++++
 8 files changed, 1706 insertions(+), 871 deletions(-)
 create mode 100644 include/qemu/qemu-arg.h
 delete mode 100644 qemu-img-cmds.hx
 create mode 100644 qemu-img-descs.h
 create mode 100644 util/qemu-arg.c
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#10 lore raw

Hi Andreas,

Quoted 8 lines · Andreas Färber

Am 08.03.2014 19:47, schrieb Leandro Dorileo:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

Why? :) You forgot to describe what's wrong with the current infrastructure, how your approach is different and what the benefit is.

Indead, I failed miserably on that (describing the problem intended to be solved). The general sense is to unify the argument parsing strategies with some benefits like help generation. If you see, every qemu binary has its own strategy - where some (most?) use getopt and the QEMU main binary uses QemuOpt, they lack a runtine help message generation, the sync between the help message and expected/implemented arguments are done manually.

But I'm not sure if we really want/need it, that's the why I sent an early RFC. For me it was just few weekend hours putting all together. :)

I see a good benefit on that - having a simple, clean, consistent and common API throughout the source code. But again, do we want it? is it worth it?

Regards...

Signature · 1 line
Leandro Dorileo
#11 lore raw
In reply to Leandro Dorileo

Am 08.03.2014 um 19:47 hat Leandro Dorileo geschrieben:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

I was planning to reply to this in more detail, but it doesn't look like I can find the time to do so, so let me just summarise my thoughts briefly.

I do like the idea of simplifying qemu-img's argument parsing, but we shouldn't make the mistake of introducing another option parsing infrastructure and end up with three different coexisting models. If we were to introduce a new framework, we must make sure that all code is converted to it and QemuOpts can be dropped.

Now replacing QemuOpts and all of its users is not something that seems to be very productive - it works quite well in those places. So I think we should rather extend QemuOpts to work for qemu-img as well.

We would probably need to add a new parser to QemuOpts that parses command line options into a QemuOpts, and extend the definition of them with a couple of new features that are required there (sub-QemuOpts for -o, perhaps enumerations for things like --output=human/json, positional parameters).

I see that you also fill the values in (global) variables. The existing code that converts QemuOpts into C variables are the QAPI visitors. So I could imagine that we define all qemu-img options in a JSON schema like qapi-schema.json and generate the struct definitions from it. We would then end up with something like this, where the code to create a QemuImgCreateOptions struct from the QemuOpts would be QAPI generated:

void img_create(QemuImgCreateOptions *options, Error **errp);

Not sure if we really want to go that far, but perhaps it's some food for thought.

Kevin

#12 lore raw

Hi Kevin,

Quoted 8 lines · Kevin Wolf

Am 08.03.2014 um 19:47 hat Leandro Dorileo geschrieben:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

I was planning to reply to this in more detail, but it doesn't look like I can find the time to do so, so let me just summarise my thoughts briefly.

Ok.

Quoted 6 lines

I do like the idea of simplifying qemu-img's argument parsing, but we shouldn't make the mistake of introducing another option parsing infrastructure and end up with three different coexisting models. If we were to introduce a new framework, we must make sure that all code is converted to it and QemuOpts can be dropped.

Agreed.

Quoted 4 lines

Now replacing QemuOpts and all of its users is not something that seems to be very productive - it works quite well in those places. So I think we should rather extend QemuOpts to work for qemu-img as well.

Indeed, I took some time yesterday to take a deeper look at QemuOpt users, I saw that converting it all to an entirely new framework would be a massive task and maybe not worth it. Extending QemuOpts to new needs seems to be a more reasonable thing.

Quoted 6 lines

We would probably need to add a new parser to QemuOpts that parses command line options into a QemuOpts, and extend the definition of them with a couple of new features that are required there (sub-QemuOpts for -o, perhaps enumerations for things like --output=human/json, positional parameters).

Ok.

Quoted 9 lines

I see that you also fill the values in (global) variables. The existing code that converts QemuOpts into C variables are the QAPI visitors. So I could imagine that we define all qemu-img options in a JSON schema like qapi-schema.json and generate the struct definitions from it. We would then end up with something like this, where the code to create a QemuImgCreateOptions struct from the QemuOpts would be QAPI generated:

void img_create(QemuImgCreateOptions *options, Error **errp);

Using json descriptors was something I considered after sending the patches and the feedbacks I already got. That's something that would be useful on cases of generating different outputs from it, like manpages or even fragments of it, it would be more flexible as far as I can see.

But yeah, I need to take a look at qapi and see how things work.

Quoted 3 lines

Not sure if we really want to go that far, but perhaps it's some food for thought.

Yeah, sure.

Regards...

Signature · 1 line
Leandro Dorileo
#13 lore raw
Quoted 15 lines · Leandro Dorileo

Hi Kevin,

Quoted 8 lines · Kevin Wolf

Am 08.03.2014 um 19:47 hat Leandro Dorileo geschrieben:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

I was planning to reply to this in more detail, but it doesn't look like I can find the time to do so, so let me just summarise my thoughts briefly.

Ok.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

I do like the idea of simplifying qemu-img's argument parsing, but we shouldn't make the mistake of introducing another option parsing infrastructure and end up with three different coexisting models. If we were to introduce a new framework, we must make sure that all code is converted to it and QemuOpts can be dropped.

Agreed.

For that matter, if you want to help the current conversion efforts going on, we are trying to get rid of QEMUOptionParameters to _just_ use QemuOpts:
https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg01728.html

Quoted 8 lines
Quoted 6 lines

We would probably need to add a new parser to QemuOpts that parses command line options into a QemuOpts, and extend the definition of them with a couple of new features that are required there (sub-QemuOpts for -o, perhaps enumerations for things like --output=human/json, positional parameters).

Ok.

We also need to think how to expose those improvements via the QMP command query-command-line-arguments; lots of details in this thread:
https://lists.gnu.org/archive/html/qemu-devel/2014-03/threads.html#00934

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#14 lore raw

Hi Erick,

Quoted 22 lines · Eric Blake
Quoted 21 lines · Leandro Dorileo

Hi Kevin,

Quoted 8 lines · Kevin Wolf

Am 08.03.2014 um 19:47 hat Leandro Dorileo geschrieben:

Quoted 3 lines

The following patchset introduces a general purpose argument parser and migrates qemu-img to make use of it. qemu-img is just the first user of it, if we see a good feedback here I move forward and migrate all the other possible users.

I was planning to reply to this in more detail, but it doesn't look like I can find the time to do so, so let me just summarise my thoughts briefly.

Ok.

Quoted 6 lines

I do like the idea of simplifying qemu-img's argument parsing, but we shouldn't make the mistake of introducing another option parsing infrastructure and end up with three different coexisting models. If we were to introduce a new framework, we must make sure that all code is converted to it and QemuOpts can be dropped.

Quoted 6 lines
Quoted 1 line

Agreed.

For that matter, if you want to help the current conversion efforts going on, we are trying to get rid of QEMUOptionParameters to _just_ use QemuOpts:
https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg01728.html

Yep sure, I have followed the current patches and reviews, I have just posted a patch with an initial testsuite for QemuOpt in the sense to help to have it integrated.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

We would probably need to add a new parser to QemuOpts that parses command line options into a QemuOpts, and extend the definition of them with a couple of new features that are required there (sub-QemuOpts for -o, perhaps enumerations for things like --output=human/json, positional parameters).

Quoted 5 lines
Quoted 1 line

Ok.

We also need to think how to expose those improvements via the QMP command query-command-line-arguments; lots of details in this thread:
https://lists.gnu.org/archive/html/qemu-devel/2014-03/threads.html#00934

Will take a look at it.

Thanks...

Signature · 1 line
Leandro Dorileo