-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(IEA) :: {arg, binary(), gleam@option:option(IEA), gleam@option:option(binary()), fun((binary()) -> {ok, IEA} | {error, binary()})}. -file("/Users/drew/code/clip/src/clip/arg.gleam", 24). -spec pos_info(arg(any())) -> clip@arg_info:positional_info(). pos_info(Arg) -> {positional_info, erlang:element(2, Arg), begin _pipe = erlang:element(3, Arg), gleam@option:map(_pipe, fun gleam@string:inspect/1) end, erlang:element(4, Arg), no_repeat}. -file("/Users/drew/code/clip/src/clip/arg.gleam", 34). -spec to_arg_info(arg(any())) -> clip@arg_info:arg_info(). to_arg_info(Arg) -> erlang:setelement(3, clip@arg_info:empty(), [pos_info(Arg)]). -file("/Users/drew/code/clip/src/clip/arg.gleam", 39). -spec to_arg_info_many(arg(any())) -> clip@arg_info:arg_info(). to_arg_info_many(Arg) -> erlang:setelement( 3, clip@arg_info:empty(), [erlang:setelement(5, pos_info(Arg), many_repeat)] ). -file("/Users/drew/code/clip/src/clip/arg.gleam", 47). -spec to_arg_info_many1(arg(any())) -> clip@arg_info:arg_info(). to_arg_info_many1(Arg) -> erlang:setelement( 3, clip@arg_info:empty(), [erlang:setelement(5, pos_info(Arg), many1_repeat)] ). -file("/Users/drew/code/clip/src/clip/arg.gleam", 68). -spec try_map(arg(IEJ), fun((IEJ) -> {ok, IEL} | {error, binary()})) -> arg(IEL). try_map(Arg, F) -> {arg, erlang:element(2, Arg), none, erlang:element(4, Arg), fun(V) -> gleam@result:'try'((erlang:element(5, Arg))(V), fun(A) -> F(A) end) end}. -file("/Users/drew/code/clip/src/clip/arg.gleam", 84). -spec map(arg(IEP), fun((IEP) -> IER)) -> arg(IER). map(Arg, F) -> try_map(Arg, fun(A) -> {ok, F(A)} end). -file("/Users/drew/code/clip/src/clip/arg.gleam", 94). -spec default(arg(IEY), IEY) -> arg(IEY). default(Arg, Default) -> erlang:setelement(3, Arg, {some, Default}). -file("/Users/drew/code/clip/src/clip/arg.gleam", 89). -spec optional(arg(IET)) -> arg({ok, IET} | {error, nil}). optional(Arg) -> _pipe = Arg, _pipe@1 = map(_pipe, fun(Field@0) -> {ok, Field@0} end), default(_pipe@1, {error, nil}). -file("/Users/drew/code/clip/src/clip/arg.gleam", 99). -spec help(arg(IFB), binary()) -> arg(IFB). help(Arg, Help) -> erlang:setelement(4, Arg, {some, Help}). -file("/Users/drew/code/clip/src/clip/arg.gleam", 112). -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). -file("/Users/drew/code/clip/src/clip/arg.gleam", 129). -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). -file("/Users/drew/code/clip/src/clip/arg.gleam", 140). -spec new(binary()) -> arg(binary()). new(Name) -> {arg, Name, none, none, fun(Field@0) -> {ok, Field@0} end}. -file("/Users/drew/code/clip/src/clip/arg.gleam", 144). -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. -file("/Users/drew/code/clip/src/clip/arg.gleam", 150). -spec run_aux(boolean(), arg(IFJ), list(binary())) -> {ok, {IFJ, 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. -file("/Users/drew/code/clip/src/clip/arg.gleam", 173). -spec run(arg(IFM), list(binary())) -> {ok, {IFM, list(binary())}} | {error, binary()}. run(Arg, Args) -> run_aux(true, Arg, Args). -file("/Users/drew/code/clip/src/clip/arg.gleam", 177). -spec run_many_aux(list(IFP), arg(IFP), list(binary())) -> {ok, {list(IFP), 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. -file("/Users/drew/code/clip/src/clip/arg.gleam", 190). -spec run_many(arg(IFU), list(binary())) -> {ok, {list(IFU), list(binary())}} | {error, binary()}. run_many(Arg, Args) -> run_many_aux([], Arg, Args). -file("/Users/drew/code/clip/src/clip/arg.gleam", 196). -spec run_many1(arg(IFY), list(binary())) -> {ok, {list(IFY), 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 ).