-module(clip@arg). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([to_arg_info/1, to_arg_info_many/1, to_arg_info_many1/1, try_map/2, map/2, default/2, optional/1, help/2, int/1, float/1, new/1, run/2, run_many/2, run_many1/2]). -export_type([arg/1]). -opaque arg(IDR) :: {arg, binary(), gleam@option:option(IDR), gleam@option:option(binary()), fun((binary()) -> {ok, IDR} | {error, binary()})}. -spec pos_info(arg(any())) -> clip@internal@arg_info:positional_info(). pos_info(Arg) -> case Arg of {arg, Name, Default, Help, _} -> {positional_info, Name, begin _pipe = Default, gleam@option:map(_pipe, fun gleam@string:inspect/1) end, Help, no_repeat} end. -spec to_arg_info(arg(any())) -> clip@internal@arg_info:arg_info(). to_arg_info(Arg) -> erlang:setelement(3, clip@internal@arg_info:empty(), [pos_info(Arg)]). -spec to_arg_info_many(arg(any())) -> clip@internal@arg_info:arg_info(). to_arg_info_many(Arg) -> erlang:setelement( 3, clip@internal@arg_info:empty(), [erlang:setelement(5, pos_info(Arg), many_repeat)] ). -spec to_arg_info_many1(arg(any())) -> clip@internal@arg_info:arg_info(). to_arg_info_many1(Arg) -> erlang:setelement( 3, clip@internal@arg_info:empty(), [erlang:setelement(5, pos_info(Arg), many1_repeat)] ). -spec try_map(arg(IEA), fun((IEA) -> {ok, IEC} | {error, binary()})) -> arg(IEC). try_map(Arg, F) -> case Arg of {arg, Name, _, Help, Try_map} -> {arg, Name, none, Help, fun(Arg@1) -> gleam@result:'try'(Try_map(Arg@1), fun(A) -> F(A) end) end} end. -spec map(arg(IEG), fun((IEG) -> IEI)) -> arg(IEI). map(Arg, F) -> try_map(Arg, fun(A) -> {ok, F(A)} end). -spec default(arg(IEP), IEP) -> arg(IEP). default(Arg, Default) -> case Arg of {arg, Name, _, Help, Try_map} -> {arg, Name, {some, Default}, Help, Try_map} end. -spec optional(arg(IEK)) -> arg({ok, IEK} | {error, nil}). optional(Arg) -> _pipe = Arg, _pipe@1 = map(_pipe, fun(Field@0) -> {ok, Field@0} end), default(_pipe@1, {error, nil}). -spec help(arg(IES), binary()) -> arg(IES). help(Arg, Help) -> case Arg of {arg, Name, Default, _, Try_map} -> {arg, Name, Default, {some, Help}, Try_map} end. -spec int(arg(binary())) -> arg(integer()). int(Arg) -> _pipe = Arg, try_map(_pipe, fun(Val) -> _pipe@1 = gleam@int:parse(Val), gleam@result:map_error( _pipe@1, fun(_) -> <<"Non-integer value provided for "/utf8, (erlang:element(2, Arg))/binary>> end ) end). -spec float(arg(binary())) -> arg(float()). float(Arg) -> _pipe = Arg, try_map(_pipe, fun(Val) -> _pipe@1 = gleam@float:parse(Val), gleam@result:map_error( _pipe@1, fun(_) -> <<"Non-float value provided for "/utf8, (erlang:element(2, Arg))/binary>> end ) end). -spec new(binary()) -> arg(binary()). new(Name) -> {arg, Name, none, none, fun(Field@0) -> {ok, Field@0} end}. -spec not_num(binary()) -> boolean(). not_num(Str) -> Result = begin _pipe = gleam@int:parse(Str), gleam@result:is_ok(_pipe) end orelse begin _pipe@1 = gleam@float:parse(Str), gleam@result:is_ok(_pipe@1) end, not Result. -spec run_aux(boolean(), arg(IFA), list(binary())) -> {ok, {IFA, list(binary())}} | {error, binary()}. run_aux(Strict, Arg, Args) -> case {Args, erlang:element(3, Arg)} of {[<<"--"/utf8>> | Rest], _} -> _pipe = run_aux(false, Arg, Rest), gleam@result:map( _pipe, fun(V) -> {erlang:element(1, V), [<<"--"/utf8>> | erlang:element(2, V)]} end ); {[Head | Rest@1], _} -> case (Strict andalso gleam@string:starts_with(Head, <<"-"/utf8>>)) andalso not_num(Head) of true -> _pipe@1 = run_aux(Strict, Arg, Rest@1), gleam@result:map( _pipe@1, fun(V@1) -> {erlang:element(1, V@1), [Head | erlang:element(2, V@1)]} end ); false -> gleam@result:'try'( (erlang:element(5, Arg))(Head), fun(A) -> {ok, {A, Rest@1}} end ) end; {[], {some, V@2}} -> {ok, {V@2, []}}; {[], none} -> {error, <<"missing required arg: "/utf8, (erlang:element(2, Arg))/binary>>} end. -spec run(arg(IFD), list(binary())) -> {ok, {IFD, list(binary())}} | {error, binary()}. run(Arg, Args) -> run_aux(true, Arg, Args). -spec run_many_aux(list(IFG), arg(IFG), list(binary())) -> {ok, {list(IFG), list(binary())}} | {error, binary()}. run_many_aux(Acc, Arg, Args) -> case Args of [] -> {ok, {lists:reverse(Acc), []}}; _ -> case run(Arg, Args) of {ok, {A, Rest}} -> run_many_aux([A | Acc], Arg, Rest); {error, _} -> {ok, {lists:reverse(Acc), Args}} end end. -spec run_many(arg(IFL), list(binary())) -> {ok, {list(IFL), list(binary())}} | {error, binary()}. run_many(Arg, Args) -> run_many_aux([], Arg, Args). -spec run_many1(arg(IFP), list(binary())) -> {ok, {list(IFP), list(binary())}} | {error, binary()}. run_many1(Arg, Args) -> gleam@result:'try'( run_many_aux([], Arg, Args), fun(_use0) -> {Vs, Rest} = _use0, case Vs of [] -> {error, <<"must provide at least one valid value for: "/utf8, (erlang:element(2, Arg))/binary>>}; _ -> {ok, {Vs, Rest}} end end ).