-module(clip@opt). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([to_arg_info/1, try_map/2, map/2, default/2, optional/1, help/2, new/1, short/2, int/1, float/1, run/2]). -export_type([opt/1]). -opaque opt(ILZ) :: {opt, binary(), gleam@option:option(ILZ), gleam@option:option(binary()), fun((binary()) -> {ok, ILZ} | {error, binary()}), gleam@option:option(binary())}. -spec to_arg_info(opt(any())) -> clip@internal@arg_info:arg_info(). to_arg_info(Opt) -> case Opt of {opt, Name, Default, Help, _, Short} -> erlang:setelement( 2, clip@internal@arg_info:empty(), [{named_info, Name, Short, begin _pipe = Default, gleam@option:map(_pipe, fun gleam@string:inspect/1) end, Help}] ) end. -spec try_map(opt(IMC), fun((IMC) -> {ok, IME} | {error, binary()})) -> opt(IME). try_map(Opt, F) -> case Opt of {opt, Name, _, Help, Try_map, Short} -> {opt, Name, none, Help, fun(Arg) -> gleam@result:'try'(Try_map(Arg), fun(A) -> F(A) end) end, Short} end. -spec map(opt(IMI), fun((IMI) -> IMK)) -> opt(IMK). map(Opt, F) -> try_map(Opt, fun(A) -> {ok, F(A)} end). -spec default(opt(IMM), IMM) -> opt(IMM). default(Opt, Default) -> case Opt of {opt, Name, _, Help, Try_map, Short} -> {opt, Name, {some, Default}, Help, Try_map, Short} end. -spec optional(opt(IMP)) -> opt({ok, IMP} | {error, nil}). optional(Opt) -> _pipe = Opt, _pipe@1 = map(_pipe, fun(Field@0) -> {ok, Field@0} end), default(_pipe@1, {error, nil}). -spec help(opt(IMU), binary()) -> opt(IMU). help(Opt, Help) -> case Opt of {opt, Name, Default, _, Try_map, Short} -> {opt, Name, Default, {some, Help}, Try_map, Short} end. -spec new(binary()) -> opt(binary()). new(Name) -> {opt, Name, none, none, fun(Field@0) -> {ok, Field@0} end, none}. -spec short(opt(binary()), binary()) -> opt(binary()). short(Opt, Short_name) -> case Opt of {opt, Name, Default, Help, Try_map, _} -> {opt, Name, Default, Help, Try_map, {some, Short_name}} end. -spec int(opt(binary())) -> opt(integer()). int(Opt) -> _pipe = Opt, 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, Opt))/binary>> end ) end). -spec float(opt(binary())) -> opt(float()). float(Opt) -> _pipe = Opt, 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, Opt))/binary>> end ) end). -spec run(opt(INE), list(binary())) -> {ok, {INE, list(binary())}} | {error, binary()}. run(Opt, Args) -> Long_name = <<"--"/utf8, (erlang:element(2, Opt))/binary>>, Short_name = gleam@option:map( erlang:element(6, Opt), fun(S) -> <<"-"/utf8, S/binary>> end ), Names = begin _pipe = Short_name, _pipe@1 = gleam@option:map(_pipe, fun(S@1) -> [S@1] end), gleam@option:unwrap(_pipe@1, []) end, Names@1 = begin _pipe@2 = [Long_name | Names], gleam@string:join(_pipe@2, <<", "/utf8>>) end, case {Args, erlang:element(3, Opt)} of {[Key, Val | Rest], _} when (Key =:= Long_name) orelse ({some, Key} =:= Short_name) -> gleam@result:'try'( (erlang:element(5, Opt))(Val), fun(A) -> {ok, {A, Rest}} end ); {[Head | Rest@1], _} -> _pipe@3 = run(Opt, Rest@1), gleam@result:map( _pipe@3, fun(V) -> {erlang:element(1, V), [Head | erlang:element(2, V)]} end ); {[], {some, V@1}} -> {ok, {V@1, []}}; {[], none} -> {error, <<"missing required arg: "/utf8, Names@1/binary>>} end.