[libvirt] [PATCH] cmdNetworkUpdate: Prefer VSH_EXCLUSIVE_OPTIONS over if-else tree

We have macros that check and reject mutually exclusive parameters to our commands. Use those instead of if-else tree. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-network.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tools/virsh-network.c b/tools/virsh-network.c index 1ae206a..d0bd7a3 100644 --- a/tools/virsh-network.c +++ b/tools/virsh-network.c @@ -907,9 +907,12 @@ cmdNetworkUpdate(vshControl *ctl, const vshCmd *cmd) bool current = vshCommandOptBool(cmd, "current"); bool config = vshCommandOptBool(cmd, "config"); bool live = vshCommandOptBool(cmd, "live"); - unsigned int flags = 0; + unsigned int flags = VIR_NETWORK_UPDATE_AFFECT_CURRENT; const char *affected; + VSH_EXCLUSIVE_OPTIONS_VAR(current, live); + VSH_EXCLUSIVE_OPTIONS_VAR(current, config); + if (!(network = virshCommandOptNetwork(ctl, cmd, NULL))) return false; @@ -962,18 +965,10 @@ cmdNetworkUpdate(vshControl *ctl, const vshCmd *cmd) xml = xmlFromFile; } - if (current) { - if (live || config) { - vshError(ctl, "%s", _("--current must be specified exclusively")); - goto cleanup; - } - flags |= VIR_NETWORK_UPDATE_AFFECT_CURRENT; - } else { - if (config) - flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG; - if (live) - flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE; - } + if (config) + flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG; + if (live) + flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE; if (virNetworkUpdate(network, command, section, parentIndex, xml, flags) < 0) { -- 2.4.10

Since we have the macro there's no need for us to unwind it by hand and check for mutually exclusive flags ourselves. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-domain.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 0be9280..bfcc0b3 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4542,10 +4542,7 @@ cmdSaveImageEdit(vshControl *ctl, const vshCmd *cmd) * However, in the edit cycle, we let the user retry if the define * step fails, but the define step will always fail on invalid * flags, so we reject it up front to avoid looping. */ - if (define_flags == (VIR_DOMAIN_SAVE_RUNNING | VIR_DOMAIN_SAVE_PAUSED)) { - vshError(ctl, "%s", _("--running and --paused are mutually exclusive")); - return false; - } + VSH_EXCLUSIVE_OPTIONS("running", "paused"); if (vshCommandOptStringReq(ctl, cmd, "file", &file) < 0) return false; -- 2.4.10

On Mon, Feb 22, 2016 at 01:48:15PM +0100, Michal Privoznik wrote:
Since we have the macro there's no need for us to unwind it by hand and check for mutually exclusive flags ourselves.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-domain.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
ACK Jan

On Mon, Feb 22, 2016 at 01:48:14PM +0100, Michal Privoznik wrote:
We have macros that check and reject mutually exclusive parameters to our commands. Use those instead of if-else tree.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/virsh-network.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
ACK
diff --git a/tools/virsh-network.c b/tools/virsh-network.c index 1ae206a..d0bd7a3 100644 --- a/tools/virsh-network.c +++ b/tools/virsh-network.c @@ -907,9 +907,12 @@ cmdNetworkUpdate(vshControl *ctl, const vshCmd *cmd) bool current = vshCommandOptBool(cmd, "current"); bool config = vshCommandOptBool(cmd, "config"); bool live = vshCommandOptBool(cmd, "live"); - unsigned int flags = 0; + unsigned int flags = VIR_NETWORK_UPDATE_AFFECT_CURRENT; const char *affected;
+ VSH_EXCLUSIVE_OPTIONS_VAR(current, live); + VSH_EXCLUSIVE_OPTIONS_VAR(current, config); +
I would rather use VSH_EXCLUSIVE_OPTIONS to get rid of the 'current' variable. Jan
participants (2)
-
Ján Tomko
-
Michal Privoznik