-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:return()}), outil:command()) -> EUK) ) -> EUK. 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:return()}), outil:command()) -> EUN) ) -> EUN. 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:return()}), outil:command()) -> EUQ) ) -> EUQ. 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:return()}), outil:command()) -> EUT) ) -> EUT. 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, EUW} | {error, nil}), fun((fun((outil:command()) -> {ok, EUW} | {error, outil:return()}), outil:command()) -> EUZ) ) -> EUZ. 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) -> _pipe = Arg_parser(erlang:element(6, Run_cmd)), gleam@result:map_error( _pipe, fun(Reason) -> outil@help:handle_error(Reason, Run_cmd) end ) 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, EVC} | {error, nil}) ) -> fun((list(binary())) -> {ok, EVC} | {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.