Skip to content

Commit

Permalink
Merge branch 'maint'
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Dec 13, 2024
2 parents b2313fc + 248db36 commit cd27d5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 15 additions & 1 deletion lib/stdlib/src/argparse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,21 @@ is_valid_command_help(_) ->

format_help({ProgName, Root}, Format) ->
Prefix = hd(maps:get(prefixes, Format, [$-])),
Nested = maps:get(command, Format, []),
Nested0 = maps:get(command, Format, [ProgName]),
%% The command path should always start with the progname, that's why it is
%% dropped here to keep the command and sub-commands only.
%%
%% However, earlier versions of this function did not drop that progname.
%% The function thus used to crash with a badkey excception if the caller
%% passed the `CmdPath' returned by `parse/2' to this function's `command'.
%% Therefore, to keep backward compatibility, if the command path does not
%% start with the progname, it uses the entire list untouched.
Nested = case Nested0 of
[ProgName | Tail] ->
Tail;
_ ->
Nested0
end,
%% descent into commands collecting all options on the way
{_CmdName, Cmd, AllArgs} = collect_options(ProgName, Root, Nested, []),
%% split arguments into Flags, Options, Positional, and create help lines
Expand Down
9 changes: 6 additions & 3 deletions lib/stdlib/test/argparse_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,9 @@ usage(Config) when is_list(Config) ->
" --unsafe unsafe atom (atom)\n"
" --safe safe atom (existing atom)\n"
" -foobar foobaring option\n",
?assertEqual(Usage, unicode:characters_to_list(argparse:help(Cmd,
#{progname => "erl", command => ["erl", "start"]}))),
%% Same assertion for the backward-compatible way of calling `argparse:help/2'.
?assertEqual(Usage, unicode:characters_to_list(argparse:help(Cmd,
#{progname => "erl", command => ["start"]}))),
FullCmd = "Usage:\n erl"
Expand Down Expand Up @@ -824,7 +827,7 @@ usage(Config) when is_list(Config) ->
" --float floating-point long form argument (float), default: 3.14\n"
" ---extra extra option very deep\n",
?assertEqual(CrawlerStatus, unicode:characters_to_list(argparse:help(Cmd,
#{progname => "erl", command => ["status", "crawler"]}))),
#{progname => "erl", command => ["erl", "status", "crawler"]}))),
ok.

usage_help_binary() ->
Expand Down Expand Up @@ -860,7 +863,7 @@ usage_required_args() ->
usage_required_args(Config) when is_list(Config) ->
Cmd = #{commands => #{"test" => #{arguments => [#{name => required, required => true, long => "-req"}]}}},
Expected = "Usage:\n " ++ prog() ++ " test --req <required>\n\nOptional arguments:\n --req required\n",
?assertEqual(Expected, unicode:characters_to_list(argparse:help(Cmd, #{command => ["test"]}))).
?assertEqual(Expected, unicode:characters_to_list(argparse:help(Cmd, #{command => ["erl", "test"]}))).

usage_template() ->
[{doc, "Tests templates in help/usage"}].
Expand Down Expand Up @@ -927,7 +930,7 @@ usage_args_ordering(Config) when is_list(Config) ->
" second second\n"
" third third\n"
" fourth fourth\n",
unicode:characters_to_list(argparse:help(Cmd, #{command => ["cmd"]}))),
unicode:characters_to_list(argparse:help(Cmd, #{command => ["erl", "cmd"]}))),
ok.

parser_error_usage() ->
Expand Down

0 comments on commit cd27d5d

Please sign in to comment.