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, nowarn_unused_vars, nowarn_unused_function]).
-export([bool/3, float/3, int/3, string/3]).
-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, FKN} | {error, nil})
) -> fun((list(binary())) -> {ok, FKN} | {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.
-spec with_positional_argument(
outil:command(),
outil:argument(),
fun((binary()) -> {ok, FKI} | {error, nil}),
fun((fun((outil:command(), fun((FKI) -> {ok, FMA} |
{error, outil:command_return(FMB)})) -> {ok, FMA} |
{error, outil:command_return(FMB)}), outil:command()) -> FLZ)
) -> FLZ.
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, And_then) ->
_pipe = outil@help:wrap_usage(Run_cmd, Arg_parser),
gleam@result:then(_pipe, And_then)
end,
Continue(Arg_parser@1, append_argument(Cmd, Argument)).
-spec bool(
outil:command(),
binary(),
fun((fun((outil:command(), fun((boolean()) -> {ok, FMD} |
{error, outil:command_return(FME)})) -> {ok, FMD} |
{error, outil:command_return(FME)}), outil:command()) -> FMF)
) -> FMF.
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(), fun((float()) -> {ok, FMH} |
{error, outil:command_return(FMI)})) -> {ok, FMH} |
{error, outil:command_return(FMI)}), outil:command()) -> FMJ)
) -> FMJ.
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(), fun((integer()) -> {ok, FML} |
{error, outil:command_return(FMM)})) -> {ok, FML} |
{error, outil:command_return(FMM)}), outil:command()) -> FMN)
) -> FMN.
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(), fun((binary()) -> {ok, FMP} |
{error, outil:command_return(FMQ)})) -> {ok, FMP} |
{error, outil:command_return(FMQ)}), outil:command()) -> FMR)
) -> FMR.
string(Cmd, Name, Continue) ->
with_positional_argument(
Cmd,
{string_argument, Name},
fun(Field@0) -> {ok, Field@0} end,
Continue
).