From: Ian Campbell <ian.campbell@citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Campbell <ian.campbell@citrix.com>
Subject: [PATCH 2 of 2] xl: treat sub-command main function like a regular C main() function
Date: Mon, 23 Aug 2010 15:50:14 +0100 [thread overview]
Message-ID: <c336fbc9175a6b5fec8a.1282575014@localhost.localdomain> (raw)
In-Reply-To: <patchbomb.1282575012@localhost.localdomain>
# HG changeset patch
# User Ian Campbell <ian.campbell@citrix.com>
# Date 1282574865 -3600
# Node ID c336fbc9175a6b5fec8a5cdf67bbe8354aacd5d0
# Parent 2210bb76868ff58c1c97738f43c52b3e893dd178
xl: treat sub-command main function like a regular C main() function
Currently xl passes the entire argc+argv to each subcommand and relies
on the preservation of the global optind variable to ensure that the
subcommand correctly handles argument parsing (e.g. accounting for "xl
[command]" vs "xl -v [command]").
This requirement for individual sub-commands to parse arguments
relative to optind is subtle and prone to being forgotten (or simply
not expected). Several sub-commands have recently been broken in this
way (now fixed).
Therefore arrange that the argv+argc passed to the sub-commands looks
like you would expect for a regular C main function and includes
argv[0] equal to the command name with command specific arguments in
argv[1] onwards.
Since all sub-commands (currently) correctly obey the optind it is
sufficient to reset it to 1 (as described in getopt(3)) in order to
not break the sub-commands own argument parsing.
Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
diff -r 2210bb76868f -r c336fbc9175a tools/libxl/xl.c
--- a/tools/libxl/xl.c Mon Aug 23 15:34:14 2010 +0100
+++ b/tools/libxl/xl.c Mon Aug 23 15:47:45 2010 +0100
@@ -53,7 +53,7 @@ int main(int argc, char **argv)
}
}
- cmd = argv[optind++];
+ cmd = argv[optind];
if (!cmd) {
help(NULL);
@@ -69,13 +69,18 @@ int main(int argc, char **argv)
exit(1);
}
+ /* Reset options for per-command use of getopt. */
+ argv += optind;
+ argc -= optind;
+ optind = 1;
+
srand(time(0));
cspec = cmdtable_lookup(cmd);
if (cspec)
ret = cspec->cmd_impl(argc, argv);
else if (!strcmp(cmd, "help")) {
- help(argv[optind]);
+ help(argv[1]);
ret = 0;
} else {
fprintf(stderr, "command not implemented\n");
prev parent reply other threads:[~2010-08-23 14:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-23 14:50 [PATCH 0 of 2] xl: fixes and improvements to xl command line parsing Ian Campbell
2010-08-23 14:50 ` [PATCH 1 of 2] xl: correct argument parsing for some sub-commands Ian Campbell
2010-08-23 14:50 ` Ian Campbell [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c336fbc9175a6b5fec8a.1282575014@localhost.localdomain \
--to=ian.campbell@citrix.com \
--cc=xen-devel@lists.xensource.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).