Skip to content

Commit

Permalink
Merge branch 'maint'
Browse files Browse the repository at this point in the history
* maint:
  argparse: Make formatting of argument descriptions less ambiguous
  • Loading branch information
jhogberg committed Jul 26, 2023
2 parents e352673 + e4dcfa2 commit 91c8570
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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

0 comments on commit 91c8570

Please sign in to comment.