From nobody Sun Feb 8 18:30:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1569604679; cv=none; d=zoho.com; s=zohoarc; b=BK7BzPa43ANbFbp0KUkF9JGkb3Nt11Uc66EZ6sI5Eoe3OYmPxtVM+6x6P0kjPgUnZdkZ6/HihCpOhM0CjV3tBoJsmMn1rQWJvHXrO5yLZjUEvoHdk+RdUlEHz1fejALwV60JOcnhAuzCrD+cMcKQZ89ZcjMGBnCRca/zIxN42pU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569604679; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=te/lPN0AGpGH963DZKdBS/NnQKSbI/VLIUUmr0ot1Zk=; b=fxWGUFJz6ocn9HJURoq4/RBA4NsJXE9H8cmVYXI0tEGFrxl67yq68cTlOustOninotDLA4TBHFg/PZvY3pYXTC1vrrqJIjVWK4eeY28ssAEc8JORM+1Zm4KkCNiPG2dhAFqoEh8r+kn5c+bgv5CAnkXs9UKEkMIY04KRBWfoFTU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1569604679770128.8565800622531; Fri, 27 Sep 2019 10:17:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F1E7E191864E; Fri, 27 Sep 2019 17:17:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CCE1D5D6B0; Fri, 27 Sep 2019 17:17:57 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 8CBD44EE96; Fri, 27 Sep 2019 17:17:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x8RHHrMK016614 for ; Fri, 27 Sep 2019 13:17:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3FF756012E; Fri, 27 Sep 2019 17:17:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6976C600D1; Fri, 27 Sep 2019 17:17:52 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Fri, 27 Sep 2019 18:17:32 +0100 Message-Id: <20190927171733.10842-11-berrange@redhat.com> In-Reply-To: <20190927171733.10842-1-berrange@redhat.com> References: <20190927171733.10842-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 10/11] virt-admin: convert command line parsing to use GOptionContext X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.70]); Fri, 27 Sep 2019 17:17:58 +0000 (UTC) The GOptionContext API has the benefit over getopt_long that it will automatically handle --help output formatting. Signed-off-by: Daniel P. Berrang=C3=A9 --- tools/virt-admin.c | 207 +++++++++++++++++++++------------------------ 1 file changed, 98 insertions(+), 109 deletions(-) diff --git a/tools/virt-admin.c b/tools/virt-admin.c index e549ec1f83..3b48dd549d 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -21,8 +21,6 @@ #include #include "virt-admin.h" =20 -#include - #if WITH_READLINE # include # include @@ -1212,44 +1210,34 @@ vshAdmDeinit(vshControl *ctl) /* * Print usage */ -static void -vshAdmUsage(void) +static char * +vshAdmBuildDescription(void) { const vshCmdGrp *grp; const vshCmdDef *cmd; - - fprintf(stdout, _("\n%s [options]... []" - "\n%s [options]... [args...]\n\n" - " options:\n" - " -c | --connect=3DURI daemon admin connecti= on URI\n" - " -d | --debug=3DNUM debug level [0-4]\n" - " -h | --help this help\n" - " -l | --log=3DFILE output logging to fil= e\n" - " -q | --quiet quiet mode\n" - " -v short version\n" - " -V long version\n" - " --version[=3DTYPE] version, TYPE is shor= t or long (default short)\n" - " commands (non interactive mode):\n\n"), progname, - progname); + GString *str =3D g_string_new("Commands (non interactive mode):\n\n"); =20 for (grp =3D cmdGroups; grp->name; grp++) { - fprintf(stdout, _(" %s (help keyword '%s')\n"), - grp->name, grp->keyword); + g_string_append_printf(str, + _(" %s (help keyword '%s')\n"), + grp->name, grp->keyword); for (cmd =3D grp->commands; cmd->name; cmd++) { if (cmd->flags & VSH_CMD_FLAG_ALIAS) continue; - fprintf(stdout, - " %-30s %s\n", cmd->name, - _(vshCmddefGetInfo(cmd, "help"))); + g_string_append_printf(str, + " %-30s %s\n", + cmd->name, + _(vshCmddefGetInfo(cmd, "help"))); } - fprintf(stdout, "\n"); + g_string_append_printf(str, "\n"); } =20 - fprintf(stdout, "%s", - _("\n (specify help for details about the commands in= the group)\n")); - fprintf(stdout, "%s", - _("\n (specify help for details about the command)\= n\n")); - return; + g_string_append_printf(str, + _("Specify help for details about the c= ommands in the group)\n")); + g_string_append_printf(str, + _("Specify help for details about the= command)\n")); + + return g_string_free(str, FALSE); } =20 /* @@ -1275,103 +1263,104 @@ vshAdmShowVersion(vshControl *ctl ATTRIBUTE_UNUSE= D) vshPrint(ctl, "\n"); } =20 +static gboolean +vshAdmVersion(const gchar *option_name G_GNUC_UNUSED, + const gchar *value, + gpointer data, + GError **error G_GNUC_UNUSED) +{ + vshControl *ctl =3D data; + + if (value =3D=3D NULL || STRNEQ(value, "long")) + puts(VERSION); + else + vshAdmShowVersion(ctl); + + exit(EXIT_SUCCESS); +} + +/* + * argv[]: virsh [options] [command] + * + */ static bool vshAdmParseArgv(vshControl *ctl, int argc, char **argv) { - int arg, debug; - size_t i; - int longindex =3D -1; - struct option opt[] =3D { - {"connect", required_argument, NULL, 'c'}, - {"debug", required_argument, NULL, 'd'}, - {"help", no_argument, NULL, 'h'}, - {"log", required_argument, NULL, 'l'}, - {"quiet", no_argument, NULL, 'q'}, - {"version", optional_argument, NULL, 'v'}, - {NULL, 0, NULL, 0} + int debug =3D 0; + bool version =3D false; + char *logfile =3D NULL; + GOptionEntry opt[] =3D { + { "connect", 'c', 0, + G_OPTION_ARG_STRING, &ctl->connname, + _("hypervisor connection URI"), "URI" }, + { "debug", 'd', 0, + G_OPTION_ARG_INT, &debug, + _("debug level [0-4]\n"), "LEVEL" }, + { "log", 'l', 0, + G_OPTION_ARG_STRING, &logfile, + _("output logging to file"), "FILENAME" }, + { "quiet", 'q', 0, + G_OPTION_ARG_NONE, &ctl->quiet, + _("quite mode"), NULL }, + { "version", 'v', G_OPTION_FLAG_OPTIONAL_ARG, + G_OPTION_ARG_CALLBACK, vshAdmVersion, + _("print short version"), "[short]" }, + { "version", 'V', 0, + G_OPTION_ARG_NONE, &version, + _("print long version"), "long" }, + { NULL, 0, 0, 0, NULL, NULL, NULL }, }; + g_autoptr(GOptionContext) optctx =3D NULL; + GOptionGroup *optgrp; + g_autoptr(GError) error =3D NULL; + + optctx =3D g_option_context_new(_("[COMMAND [OPTION=E2=80=A6] - libvir= t admin shell")); + optgrp =3D g_option_group_new(NULL, NULL, NULL, ctl, NULL); + g_option_group_set_translation_domain(optgrp, PACKAGE); + g_option_group_add_entries(optgrp, opt); + g_option_context_set_main_group(optctx, optgrp); + g_option_context_set_strict_posix(optctx, true); + g_option_context_set_description(optctx, + vshAdmBuildDescription()); + + if (!g_option_context_parse(optctx, &argc, &argv, &error)) { + vshError(ctl, _("option parsing failed: %s\n"), error->message); + exit(EXIT_FAILURE); + } =20 - /* Standard (non-command) options. The leading + ensures that no - * argument reordering takes place, so that command options are - * not confused with top-level virt-admin options. */ - while ((arg =3D getopt_long(argc, argv, "+:c:d:hl:qvV", opt, &longinde= x)) !=3D -1) { - switch (arg) { - case 'c': - VIR_FREE(ctl->connname); - ctl->connname =3D vshStrdup(ctl, optarg); - break; - case 'd': - if (virStrToLong_i(optarg, NULL, 10, &debug) < 0) { - vshError(ctl, _("option %s takes a numeric argument"), - longindex =3D=3D -1 ? "-d" : "--debug"); - exit(EXIT_FAILURE); - } - if (debug < VSH_ERR_DEBUG || debug > VSH_ERR_ERROR) - vshError(ctl, _("ignoring debug level %d out of range [%d-= %d]"), - debug, VSH_ERR_DEBUG, VSH_ERR_ERROR); - else - ctl->debug =3D debug; - break; - case 'h': - vshAdmUsage(); - exit(EXIT_SUCCESS); - break; - case 'l': - vshCloseLogFile(ctl); - ctl->logfile =3D vshStrdup(ctl, optarg); - vshOpenLogFile(ctl); - break; - case 'q': - ctl->quiet =3D true; - break; - case 'v': - if (STRNEQ_NULLABLE(optarg, "long")) { - puts(VERSION); - exit(EXIT_SUCCESS); - } - ATTRIBUTE_FALLTHROUGH; - case 'V': - vshAdmShowVersion(ctl); - exit(EXIT_SUCCESS); - case ':': - for (i =3D 0; opt[i].name !=3D NULL; i++) { - if (opt[i].val =3D=3D optopt) - break; - } - if (opt[i].name) - vshError(ctl, _("option '-%c'/'--%s' requires an argument"= ), - optopt, opt[i].name); - else - vshError(ctl, _("option '-%c' requires an argument"), opto= pt); - exit(EXIT_FAILURE); - case '?': - if (optopt) - vshError(ctl, _("unsupported option '-%c'. See --help."), = optopt); - else - vshError(ctl, _("unsupported option '%s'. See --help."), a= rgv[optind - 1]); - exit(EXIT_FAILURE); - default: - vshError(ctl, _("unknown option")); - exit(EXIT_FAILURE); - } - longindex =3D -1; + if (debug < VSH_ERR_DEBUG || debug > VSH_ERR_ERROR) { + vshError(ctl, _("debug level %d out of range [%d-%d]"), + debug, VSH_ERR_DEBUG, VSH_ERR_ERROR); + exit(EXIT_FAILURE); + } + + if (version) { + vshAdmShowVersion(ctl); + exit(EXIT_SUCCESS); } =20 - if (argc =3D=3D optind) { + if (logfile) { + vshCloseLogFile(ctl); + ctl->logfile =3D logfile; + vshOpenLogFile(ctl); + } + + if (argc =3D=3D 1) { ctl->imode =3D true; } else { /* parse command */ ctl->imode =3D false; - if (argc - optind =3D=3D 1) { - vshDebug(ctl, VSH_ERR_INFO, "commands: \"%s\"\n", argv[optind]= ); - return vshCommandStringParse(ctl, argv[optind], NULL); + if (argc =3D=3D 2) { + vshDebug(ctl, VSH_ERR_INFO, "commands: \"%s\"\n", argv[1]); + return vshCommandStringParse(ctl, argv[1], NULL); } else { - return vshCommandArgvParse(ctl, argc - optind, argv + optind); + return vshCommandArgvParse(ctl, argc - 1, argv + 1); } } return true; } =20 + static const vshCmdDef vshAdmCmds[] =3D { VSH_CMD_CD, VSH_CMD_ECHO, --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list