mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 07/10] commands: help: ignore options after first regular argument
Date: Wed, 14 Jun 2023 15:54:49 +0200	[thread overview]
Message-ID: <20230614135452.1884124-8-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230614135452.1884124-1-a.fatoum@pengutronix.de>

If some-command -n foo bar -v fails, it would be nice to be able to
just stick help in front of the command to get help text. This doesn't
work currently, because getopt called for help will complain about not
knowing some-command's options. Fix this by only parsing help options up
to the first non-option argument (i.e. one that doesn't start with `-').

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/help.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/commands/help.c b/commands/help.c
index ba8542b90f01..87c1368d746d 100644
--- a/commands/help.c
+++ b/commands/help.c
@@ -3,7 +3,6 @@
 
 #include <common.h>
 #include <command.h>
-#include <getopt.h>
 #include <complete.h>
 
 
@@ -72,10 +71,16 @@ static void list_commands(int verbose)
 static int do_help(int argc, char *argv[])
 {
 	struct command *cmdtp;
-	int opt, verbose = 0, all = 0;
+	int verbose = 0, all = 0;
+	int argi;
 
-	while ((opt = getopt(argc, argv, "va")) > 0) {
-		switch (opt) {
+	/* We can't use getopt() here because we want to stop at the first
+	 * non-option to support, so we can just prefix help in front
+	 * of a command with options.
+	 */
+	argi = 1;
+	while (argi < argc && *argv[argi] == '-') {
+		switch (argv[argi++][1]) {
 		case 'v':
 			verbose = 1;
 			break;
@@ -93,20 +98,20 @@ static int do_help(int argc, char *argv[])
 		return 0;
 	}
 
-	if (optind == argc) {	/* show list of commands */
+	if (argi == argc) {	/* show list of commands */
 		list_commands(verbose);
 		return 0;
 	}
 
 
 	/* command help (long version) */
-	if ((cmdtp = find_cmd(argv[optind])) != NULL) {
+	if ((cmdtp = find_cmd(argv[argi])) != NULL) {
 		barebox_cmd_usage(cmdtp);
 		return 0;
 	} else {
 		printf ("Unknown command '%s' - try 'help'"
 			" without arguments for list of all"
-			" known commands\n\n", argv[optind]
+			" known commands\n\n", argv[argi]
 				);
 		return 1;
 	}
-- 
2.39.2




  parent reply	other threads:[~2023-06-14 13:57 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-14 13:54 [PATCH 00/10] gpio: add proper gpiod API and gpio-mux support Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 01/10] driver: include dev_print and family from <driver.h> Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 02/10] include: linux/printk: define new dev_errp_probe Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 03/10] gpio: have gpiod_ functions return and accept pointers Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 04/10] gpio: gpiolib: rename struct gpio_info to gpio_desc Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 05/10] gpiolib: export proper gpio descriptor API Ahmad Fatoum
2023-06-20  5:20   ` Marco Felsch
2023-06-20  5:55     ` Ahmad Fatoum
2023-06-20  6:13       ` Marco Felsch
2023-06-20  6:18         ` Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 06/10] bitmap: implement bitmap_{to,from}_arr{32,64} Ahmad Fatoum
2023-06-14 13:54 ` Ahmad Fatoum [this message]
2023-06-14 13:54 ` [PATCH 08/10] gpiolib: factor out finding gpio property Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 09/10] gpiolib: add support for requesting and setting gpiod arrays Ahmad Fatoum
2023-06-14 13:54 ` [PATCH 10/10] drivers: port Linux mux framework and gpio-mux driver Ahmad Fatoum
2023-06-14 15:21   ` Ahmad Fatoum
2023-06-20  5:51 ` [PATCH 00/10] gpio: add proper gpiod API and gpio-mux support Marco Felsch
2023-06-21  9:27 ` Sascha Hauer

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=20230614135452.1884124-8-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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