Skip to content

Commit

Permalink
Merge pull request #37 from max-au/max-au/fix-unsafe-atom
Browse files Browse the repository at this point in the history
[argparse] fix help generation for {atom, unsafe} type
  • Loading branch information
max-au authored Mar 24, 2022
2 parents 69acf3c + e73a532 commit 55caa1d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ To be considered after 1.2.0:

## Changelog

Verson 1.2.1:
Verson 1.2.3:
* implemented global default
* minor bugfixes

Expand Down
2 changes: 1 addition & 1 deletion doc/overview.edoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
** this is the overview.doc file for the application 'argparse' **

@version 1.2.2
@version 1.2.3
@author Maxim Fedorov, <[email protected]>
@title argparse: A simple framework to create complex CLI.

Expand Down
2 changes: 1 addition & 1 deletion src/argparse.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, argparse,
[{description, "argparse: arguments parser, and cli framework"},
{vsn, "1.2.2"},
{vsn, "1.2.3"},
{applications,
[kernel,
stdlib
Expand Down
4 changes: 4 additions & 0 deletions src/argparse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,10 @@ format_type(#{type := {binary, Re, _}}) when is_binary(Re) ->
io_lib:format("binary re: ~ts", [Re]);
format_type(#{type := {StrBin, Choices}}) when StrBin =:= string orelse StrBin =:= binary, is_list(Choices) ->
io_lib:format("choice: ~ts", [lists:join(", ", Choices)]);
format_type(#{type := atom}) ->
"existing atom";
format_type(#{type := {atom, unsafe}}) ->
"atom";
format_type(#{type := {atom, Choices}}) ->
io_lib:format("choice: ~ts", [lists:join(", ", [atom_to_list(C) || C <- Choices])]);
format_type(#{type := boolean}) ->
Expand Down
14 changes: 10 additions & 4 deletions test/argparse_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ ubiq_cmd() ->
#{name => choice, short => $c, type => {int, [1,2,3]}, help => "tough choice"},
#{name => fc, short => $q, type => {float, [2.1,1.2]}, help => "floating choice"},
#{name => ac, short => $w, type => {atom, [one, two]}, help => "atom choice"},
#{name => au, long => "-unsafe", type => {atom, unsafe}, help => "unsafe atom"},
#{name => as, long => "-safe", type => atom, help => "safe atom"},
#{name => name, required => false, nargs => list, help => hidden},
#{name => long, long => "foobar", required => false, help => "foobaring option"}
], commands => #{
Expand Down Expand Up @@ -275,9 +277,12 @@ type_validators(Config) when is_list(Config) ->
parse_opts("5", [#{name => bin, type => binary}])),
?assertEqual(#{str => "011"},
parse_opts("11", [#{name => str, type => {custom, fun(S) -> [$0|S] end}}])),
%% funny non-atom-atom
%% %% funny non-atom-atom: ensure the atom does not exist
?assertException(error, badarg, list_to_existing_atom("$can_never_be")),
ArgMap = parse_opts("$can_never_be", [#{name => atom, type => {atom, unsafe}}]),
?assert(is_map_key(atom, ArgMap)), %% must be successful, but really we can't create an atom in code!
argparse:validate(#{arguments => [#{name => atom, type => {atom, unsafe}}]}),
%% must be successful, but really we can't create an atom in code!
?assertEqual(list_to_existing_atom("$can_never_be"), maps:get(atom, ArgMap)),
%% choices: exceptions
?assertException(error, {argparse, {invalid_argument, Prog, bin, "K"}},
parse_opts("K", [#{name => bin, type => {binary, [<<"M">>, <<"N">>]}}])),
Expand Down Expand Up @@ -729,12 +734,13 @@ usage() ->
usage(Config) when is_list(Config) ->
Cmd = ubiq_cmd(),
Usage = "usage: " ++ prog() ++ " start {crawler|doze} [-lrfv] [-s <shard>...] [-z <z>] [-m <more>] [-b <bin>] [-g <g>] [-t <t>] ---maybe-req -y <y>"
" --yyy <y> [-u <u>] [-c <choice>] [-q <fc>] [-w <ac>] [-foobar <long>] [--force] [-i <interval>] [--req <weird>] [--float <float>] <server> [<optpos>]"
" --yyy <y> [-u <u>] [-c <choice>] [-q <fc>] [-w <ac>] [--unsafe <au>] [--safe <as>] [-foobar <long>] [--force] [-i <interval>] [--req <weird>] [--float <float>] <server> [<optpos>]"
"\n\nSubcommands:\n crawler controls crawler behaviour\n doze dozes a bit\n\nArguments:\n server server to start\n optpos optional positional (int)"
"\n\nOptional arguments:\n -s initial shards (int)\n -z between (1 <= int <= 10)\n -l maybe lower (int <= 10)"
"\n -m less than 10 (int <= 10)\n -b binary with re (binary re: m)\n -g binary with re (binary re: m)\n -t string with re (string re: m)"
"\n ---maybe-req maybe required int (int)\n -y, --yyy string with re (string re: m)\n -u string choices (choice: 1, 2)\n -c tough choice (choice: 1, 2, 3)"
"\n -q floating choice (choice: 2.10000, 1.20000)\n -w atom choice (choice: one, two)\n -foobar foobaring option\n -r recursive\n -f, --force force\n -v verbosity level"
"\n -q floating choice (choice: 2.10000, 1.20000)\n -w atom choice (choice: one, two)\n --unsafe unsafe atom (atom)\n --safe safe atom (existing atom)"
"\n -foobar foobaring option\n -r recursive\n -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",
?assertEqual(Usage, argparse:help(Cmd, #{command => ["start"]})),
FullCmd = "usage: " ++ prog() ++ " <command> [-rfv] [--force] [-i <interval>] [--req <weird>] [--float <float>]\n\nSubcommands:\n start verifies configuration and starts server"
Expand Down

0 comments on commit 55caa1d

Please sign in to comment.