-module(outil@arg). -compile(no_auto_import). -export([bool/3, float/3, int/3, string/3]). -spec bool( outil@core:command(EZO), binary(), fun((fun((list(binary())) -> {ok, boolean()} | {error, outil@core:error()}), outil@core:command(EZO)) -> outil@core:command(EZO)) ) -> outil@core:command(EZO). bool(Cmd, Name, Continue) -> with_positional_argument( Cmd, {bool_argument, Name}, fun outil@core:parse_bool/1, Continue ). -spec float( outil@core:command(EZT), binary(), fun((fun((list(binary())) -> {ok, float()} | {error, outil@core:error()}), outil@core:command(EZT)) -> outil@core:command(EZT)) ) -> outil@core:command(EZT). float(Cmd, Name, Continue) -> with_positional_argument( Cmd, {float_argument, Name}, fun gleam@float:parse/1, Continue ). -spec int( outil@core:command(EZY), binary(), fun((fun((list(binary())) -> {ok, integer()} | {error, outil@core:error()}), outil@core:command(EZY)) -> outil@core:command(EZY)) ) -> outil@core:command(EZY). int(Cmd, Name, Continue) -> with_positional_argument( Cmd, {int_argument, Name}, fun gleam@int:parse/1, Continue ). -spec string( outil@core:command(FAD), binary(), fun((fun((list(binary())) -> {ok, binary()} | {error, outil@core:error()}), outil@core:command(FAD)) -> outil@core:command(FAD)) ) -> outil@core:command(FAD). string(Cmd, Name, Continue) -> with_positional_argument( Cmd, {string_argument, Name}, fun(Field@0) -> {ok, Field@0} end, Continue ). -spec with_positional_argument( outil@core:command(FAI), outil@core:argument(), fun((binary()) -> {ok, FAK} | {error, nil}), fun((fun((list(binary())) -> {ok, FAK} | {error, outil@core:error()}), outil@core:command(FAI)) -> outil@core:command(FAI)) ) -> outil@core:command(FAI). 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 ), Continue(Arg_parser, append_argument(Cmd, Argument)). -spec append_argument(outil@core:command(FAQ), outil@core:argument()) -> outil@core:command(FAQ). 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, FAT} | {error, nil}) ) -> fun((list(binary())) -> {ok, FAT} | {error, outil@core:error()}). 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>> -> {error, {out_of_place_option, Arg}}; _ -> _pipe = Parser(Arg), gleam@result:map_error( _pipe, fun(_) -> {malformed_argument, Name, Arg} end ) end end end.