Packages
A Gleam library for writing command line tools.
Current section
Files
Jump to
Current section
Files
src/outil@arg.erl
-module(outil@arg).
-compile(no_auto_import).
-export([bool/3, float/3, int/3, string/3]).
-spec bool(
outil:command(),
binary(),
fun((fun((outil:command()) -> {ok, boolean()} |
{error, outil:command_return(any())}), outil:command()) -> FBY)
) -> FBY.
bool(Cmd, Name, Continue) ->
with_positional_argument(
Cmd,
{bool_argument, Name},
fun outil:parse_bool/1,
Continue
).
-spec float(
outil:command(),
binary(),
fun((fun((outil:command()) -> {ok, float()} |
{error, outil:command_return(any())}), outil:command()) -> FCD)
) -> FCD.
float(Cmd, Name, Continue) ->
with_positional_argument(
Cmd,
{float_argument, Name},
fun gleam@float:parse/1,
Continue
).
-spec int(
outil:command(),
binary(),
fun((fun((outil:command()) -> {ok, integer()} |
{error, outil:command_return(any())}), outil:command()) -> FCI)
) -> FCI.
int(Cmd, Name, Continue) ->
with_positional_argument(
Cmd,
{int_argument, Name},
fun gleam@int:parse/1,
Continue
).
-spec string(
outil:command(),
binary(),
fun((fun((outil:command()) -> {ok, binary()} |
{error, outil:command_return(any())}), outil:command()) -> FCN)
) -> FCN.
string(Cmd, Name, Cont) ->
with_positional_argument(
Cmd,
{string_argument, Name},
fun(Field@0) -> {ok, Field@0} end,
Cont
).
-spec with_positional_argument(
outil:command(),
outil:argument(),
fun((binary()) -> {ok, FCS} | {error, nil}),
fun((fun((outil:command()) -> {ok, FCS} |
{error, outil:command_return(any())}), outil:command()) -> FCV)
) -> FCV.
with_positional_argument(Cmd, Argument, Parse, Continue) ->
Arg_pos = gleam@list:length(erlang:element(4, Cmd)),
Arg_parser = positional_arg_parser(
Arg_pos,
erlang:element(2, Argument),
Parse
),
Arg_parser@1 = fun(Run_cmd) ->
outil@help:wrap_usage(Run_cmd, Arg_parser)
end,
Continue(Arg_parser@1, append_argument(Cmd, Argument)).
-spec append_argument(outil:command(), outil:argument()) -> outil:command().
append_argument(Cmd, Arg) ->
{command,
erlang:element(2, Cmd),
erlang:element(3, Cmd),
gleam@list:append(erlang:element(4, Cmd), [Arg]),
erlang:element(5, Cmd),
erlang:element(6, Cmd)}.
-spec positional_arg_parser(
integer(),
binary(),
fun((binary()) -> {ok, FDA} | {error, nil})
) -> fun((list(binary())) -> {ok, FDA} | {error, outil@error:reason()}).
positional_arg_parser(At, Name, Parser) ->
fun(Args) -> case gleam@list:at(Args, At) of
{error, _} ->
{error, {missing_argument, Name}};
{ok, Arg} ->
case Arg of
<<"-"/utf8, _/binary>> ->
{error, {out_of_place_option, Arg}};
_ ->
_pipe = Parser(Arg),
gleam@result:map_error(
_pipe,
fun(_) -> {malformed_argument, Name, Arg} end
)
end
end end.