Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse: Make formatting of argument descriptions less ambiguous #7490

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/stdlib/src/argparse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1252,17 +1252,21 @@ format_description(#{help := {_Short, Desc}} = Opt) ->
String
end, Desc
);
%% default format: "desc", "desc (type)", "desc (default)", "desc (type, default)"
%% default format:
%% "desc"
%% "desc (type)"
%% "desc, default: abc"
%% "desc (type), default: abc"
format_description(#{name := Name} = Opt) ->
NameStr = maps:get(help, Opt, io_lib:format("~ts", [Name])),
case {NameStr, format_type(Opt), format_default(Opt)} of
{"", "", Type} -> Type;
{"", Default, ""} -> Default;
{Desc, "", ""} -> Desc;
{Desc, "", Default} -> [Desc, " (", Default, ")"];
{Desc, "", Default} -> [Desc, " , default: ", Default];
{Desc, Type, ""} -> [Desc, " (", Type, ")"];
{"", Type, Default} -> [Type, ", ", Default];
{Desc, Type, Default} -> [Desc, " (", Type, ", ", Default, ")"]
{"", Type, Default} -> [Type, ", default: ", Default];
{Desc, Type, Default} -> [Desc, " (", Type, "), default: ", Default]
end.

%% option formatting helpers
Expand Down
8 changes: 4 additions & 4 deletions lib/stdlib/test/argparse_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ unicode(Config) when is_list(Config) ->
Prog = [prog()],
?assertEqual({ok, Expected, Prog, Cmd}, argparse:parse([], Cmd)), %% default
?assertEqual({ok, Expected, Prog, Cmd}, argparse:parse(["★"], Cmd)), %% specified in the command line
?assertEqual("Usage:\n " ++ prog() ++ " <text>\n\nArguments:\n text åäö (binary, ★)\n",
?assertEqual("Usage:\n " ++ prog() ++ " <text>\n\nArguments:\n text åäö (binary), default: ★\n",
unicode:characters_to_list(argparse:help(Cmd))),
%% test command name and argument name in unicode
Uni = #{commands => #{"åäö" => #{help => "öФ"}}, handler => optional,
Expand Down Expand Up @@ -775,7 +775,7 @@ usage(Config) when is_list(Config) ->
" -v verbosity level\n"
" -i interval set (int >= 1)\n"
" --req required optional, right?\n"
" --float floating-point long form argument (float, 3.14)\n",
" --float floating-point long form argument (float), default: 3.14\n",
?assertEqual(Usage, unicode:characters_to_list(argparse:help(Cmd,
#{progname => "erl", command => ["start"]}))),
FullCmd = "Usage:\n erl"
Expand All @@ -790,7 +790,7 @@ usage(Config) when is_list(Config) ->
" -v verbosity level\n"
" -i interval set (int >= 1)\n"
" --req required optional, right?\n"
" --float floating-point long form argument (float, 3.14)\n",
" --float floating-point long form argument (float), default: 3.14\n",
?assertEqual(FullCmd, unicode:characters_to_list(argparse:help(Cmd,
#{progname => erl}))),
CrawlerStatus = "Usage:\n erl status crawler [-rfv] [---extra <extra>] [--force] [-i <interval>]\n"
Expand All @@ -799,7 +799,7 @@ usage(Config) when is_list(Config) ->
" -f, --force force\n -v verbosity level\n"
" -i interval set (int >= 1)\n"
" --req required optional, right?\n"
" --float floating-point long form argument (float, 3.14)\n",
" --float floating-point long form argument (float), default: 3.14\n",
?assertEqual(CrawlerStatus, unicode:characters_to_list(argparse:help(Cmd,
#{progname => "erl", command => ["status", "crawler"]}))),
ok.
Expand Down
Loading