File 6137-stdlib-Fix-argparse-format_help-for-sub-commands.patch of Package erlang
From 1aa91e7359656129629c5b1c05c56aba1d36abfa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Janusz=20Wo=C5=BAniak?= <jasewo@gmail.com>
Date: Tue, 3 Sep 2024 22:06:12 +0200
Subject: [PATCH] stdlib: Fix argparse:format_help for sub-commands
Use sub-command's 'help' for formatting instead of command's one when
possible.
---
lib/stdlib/src/argparse.erl | 10 +++++++++-
lib/stdlib/test/argparse_SUITE.erl | 4 ++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/lib/stdlib/src/argparse.erl b/lib/stdlib/src/argparse.erl
index c546c30f16..3536bec696 100644
--- a/lib/stdlib/src/argparse.erl
+++ b/lib/stdlib/src/argparse.erl
@@ -1628,7 +1628,7 @@ format_help({ProgName, Root}, Format) ->
%% usage line has hardcoded format for now
Usage = [ProgName, ShortCmd, FlagsForm, Opts, Args],
%% format usage according to help template
- Template0 = maps:get(help, Root, ""),
+ Template0 = get_help(Root, Nested),
%% when there is no help defined for the command, or help is a string,
%% use the default format (original argparse behaviour)
Template =
@@ -1659,6 +1659,14 @@ collect_options(CmdName, Command, [Cmd|Tail], Args) ->
SubCmd = maps:get(Cmd, Sub),
collect_options(CmdName ++ " " ++ Cmd, SubCmd, Tail, Args ++ maps:get(arguments, Command, [])).
+%% gets help for sub-command
+get_help(Command, []) ->
+ maps:get(help, Command, "");
+get_help(Command, [Cmd|Tail]) ->
+ Sub = maps:get(commands, Command),
+ SubCmd = maps:get(Cmd, Sub),
+ get_help(SubCmd, Tail).
+
%% conditionally adds text and empty lines
maybe_add(_ToAdd, [], _Element, Template) ->
Template;
diff --git a/lib/stdlib/test/argparse_SUITE.erl b/lib/stdlib/test/argparse_SUITE.erl
index a4a6b21a5e..f0cd7c39f7 100644
--- a/lib/stdlib/test/argparse_SUITE.erl
+++ b/lib/stdlib/test/argparse_SUITE.erl
@@ -751,6 +751,8 @@ usage(Config) when is_list(Config) ->
" [-t <t>] ---maybe-req -y <y> --yyy <y> [-u <u>] [-c <choice>] [-q <fc>]\n"
" [-w <ac>] [--unsafe <au>] [--safe <as>] [-foobar <long>] <server> [<optpos>]\n"
"\n"
+ "verifies configuration and starts server\n"
+ "\n"
"Subcommands:\n"
" crawler controls crawler behaviour\n"
" doze dozes a bit\n\n"
@@ -799,6 +801,8 @@ usage(Config) when is_list(Config) ->
#{progname => erl}))),
CrawlerStatus = "Usage:\n erl status crawler [-rfv] [--force] [-i <interval>] [--req <weird>]\n"
" [--float <float>] [---extra <extra>]\n\n"
+ "crawler status\n"
+ "\n"
"Optional arguments:\n"
" -r recursive\n"
" -f, --force force\n"
--
2.43.0