mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] commands: help: ignore options after first regular argument
@ 2023-06-14  9:02 Ahmad Fatoum
  2023-06-14 14:21 ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Ahmad Fatoum @ 2023-06-14  9:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-14 14:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-14  9:02 [PATCH] commands: help: ignore options after first regular argument Ahmad Fatoum
2023-06-14 14:21 ` Sascha Hauer
2023-06-14 14:27   ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox