-module(clad). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([flag/0, opt/4, list/1, positional_arguments/1, decode/2]). -export_type([state/0]). -type state() :: {state, gleam@dict:dict(binary(), gleam@dynamic:dynamic_()), gleam@dict:dict(binary(), list(gleam@dynamic:dynamic_())), list(binary())}. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 242). -spec flag() -> decode@zero:decoder(boolean()). flag() -> _pipe = {decoder, fun decode@zero:decode_bool/1}, _pipe@1 = decode@zero:optional(_pipe), decode@zero:map( _pipe@1, fun(_capture) -> gleam@option:unwrap(_capture, false) end ). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 248). -spec optional_field( any(), decode@zero:decoder(GWH), fun((gleam@option:option(GWH)) -> decode@zero:decoder(GWK)) ) -> decode@zero:decoder(GWK). optional_field(Field_name, Field_decoder, Next) -> Decoding_function = fun(Data) -> gleam@bool:guard( gleam@dynamic:classify(Data) =:= <<"Nil"/utf8>>, {ok, none}, fun() -> case decode@zero:run(Data, decode@zero:optional(Field_decoder)) of {ok, none} -> case decode@zero:run(Data, Field_decoder) of {ok, V} -> {ok, {some, V}}; {error, _} -> {ok, none} end; Other -> Other end end ) end, Decoder = decode@zero:new_primitive_decoder(Decoding_function, none), decode@zero:field(Field_name, Decoder, Next). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 285). -spec opt( binary(), binary(), decode@zero:decoder(GWN), fun((GWN) -> decode@zero:decoder(GWP)) ) -> decode@zero:decoder(GWP). opt(Long_name, Short_name, Field_decoder, Next) -> optional_field(Long_name, Field_decoder, fun(Value) -> case Value of {some, V} -> Next(V); none -> decode@zero:field(Short_name, Field_decoder, Next) end end). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 309). -spec list(decode@zero:decoder(GWS)) -> decode@zero:decoder(list(GWS)). list(Inner) -> Single = begin _pipe = Inner, decode@zero:map(_pipe, fun gleam@list:wrap/1) end, decode@zero:one_of(decode@zero:list(Inner), [Single]). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 333). -spec is_number(binary()) -> boolean(). is_number(Str) -> case gleam@regex:from_string(<<"^[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)$"/utf8>>) of {ok, Re} -> gleam@regex:check(Re, Str); {error, _} -> false end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 340). -spec is_alpha(binary()) -> boolean(). is_alpha(Str) -> case gleam@regex:from_string(<<"^[a-zA-Z]+$"/utf8>>) of {ok, Re} -> gleam@regex:check(Re, Str); {error, _} -> false end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 503). -spec append_positional(state(), binary()) -> state(). append_positional(State, Value) -> Positional = [Value | erlang:element(4, State)], erlang:setelement(4, State, Positional). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 515). -spec try_parse_float(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}. try_parse_float(Input) -> _pipe = gleam@float:parse(Input), gleam@result:map(_pipe, fun gleam_stdlib:identity/1). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 520). -spec try_parse_int(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}. try_parse_int(Input) -> _pipe = gleam@int:parse(Input), gleam@result:map(_pipe, fun gleam_stdlib:identity/1). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 525). -spec try_parse_bool(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}. try_parse_bool(Input) -> case Input of <<"true"/utf8>> -> {ok, gleam_stdlib:identity(true)}; <<"True"/utf8>> -> {ok, gleam_stdlib:identity(true)}; <<"false"/utf8>> -> {ok, gleam_stdlib:identity(false)}; <<"False"/utf8>> -> {ok, gleam_stdlib:identity(false)}; _ -> {error, nil} end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 508). -spec parse_value(binary()) -> gleam@dynamic:dynamic_(). parse_value(Input) -> _pipe = try_parse_float(Input), _pipe@1 = gleam@result:'or'(_pipe, try_parse_int(Input)), _pipe@2 = gleam@result:'or'(_pipe@1, try_parse_bool(Input)), gleam@result:unwrap(_pipe@2, gleam_stdlib:identity(Input)). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 427). -spec set_arg(state(), binary(), binary()) -> state(). set_arg(State, Key, Value) -> In_opt = gleam@dict:get(erlang:element(2, State), Key), In_list = gleam@dict:get(erlang:element(3, State), Key), case {In_opt, In_list} of {{error, _}, {error, _}} -> Opts = gleam@dict:insert( erlang:element(2, State), Key, parse_value(Value) ), erlang:setelement(2, State, Opts); {{ok, V}, _} -> Opts@1 = gleam@dict:delete(erlang:element(2, State), Key), List_opts = gleam@dict:insert( erlang:element(3, State), Key, [parse_value(Value), V] ), erlang:setelement(3, erlang:setelement(2, State, Opts@1), List_opts); {_, {ok, Values}} -> List_opts@1 = gleam@dict:insert( erlang:element(3, State), Key, [parse_value(Value) | Values] ), erlang:setelement(3, State, List_opts@1) end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 448). -spec parse_short(binary(), state()) -> {state(), gleam@option:option(binary())}. parse_short(Arg, State) -> case gleam@string:pop_grapheme(Arg) of {ok, {H, <<""/utf8>>}} -> {State, {some, H}}; {ok, {H@1, Rest}} -> case {is_alpha(H@1), is_number(Rest)} of {true, true} -> {set_arg(State, H@1, Rest), none}; {_, _} -> New_state = set_arg(State, H@1, <<"true"/utf8>>), parse_short(Rest, New_state) end; _ -> {State, none} end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 357). -spec parse_arg(binary(), list(binary()), state()) -> {state(), list(binary())}. parse_arg(Arg, Rest, State) -> case Arg of <<"--"/utf8>> -> Positional = begin _pipe = [lists:reverse(Rest), erlang:element(4, State)], gleam@list:flatten(_pipe) end, New_state = erlang:setelement(4, State, Positional), {New_state, []}; <<"--"/utf8, Key/binary>> -> case gleam@string:split(Key, <<"="/utf8>>) of [Key@1, Value] -> New_state@1 = set_arg(State, Key@1, Value), {New_state@1, Rest}; _ -> case Rest of [] -> New_state@2 = set_arg(State, Key, <<"true"/utf8>>), {New_state@2, Rest}; [<<"-"/utf8, _/binary>> | _] -> New_state@2 = set_arg(State, Key, <<"true"/utf8>>), {New_state@2, Rest}; [Next | Rest@1] -> New_state@3 = set_arg(State, Key, Next), {New_state@3, Rest@1} end end; <<"-"/utf8, Key@2/binary>> -> case gleam@string:split(Key@2, <<"="/utf8>>) of [Key@3, Value@1] -> case gleam@string:pop_grapheme(Key@3) of {ok, {Key@4, _}} -> New_state@4 = set_arg(State, Key@4, Value@1), {New_state@4, Rest}; _ -> {State, Rest} end; _ -> case Rest of [] -> case parse_short(Key@2, State) of {New_state@5, {some, K}} -> {set_arg(New_state@5, K, <<"true"/utf8>>), Rest}; {New_state@6, none} -> {New_state@6, Rest} end; [<<"-"/utf8, _/binary>> | _] -> case parse_short(Key@2, State) of {New_state@5, {some, K}} -> {set_arg(New_state@5, K, <<"true"/utf8>>), Rest}; {New_state@6, none} -> {New_state@6, Rest} end; [Next@1 | New_rest] -> case parse_short(Key@2, State) of {New_state@7, {some, K@1}} -> {set_arg(New_state@7, K@1, Next@1), New_rest}; {New_state@8, none} -> {New_state@8, Rest} end end end; _ -> New_state@9 = append_positional(State, Arg), {New_state@9, Rest} end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 347). -spec parse_args(list(binary()), state()) -> state(). parse_args(Args, State) -> case Args of [] -> State; [Arg | Rest] -> {New_state, Rest@1} = parse_arg(Arg, Rest, State), parse_args(Rest@1, New_state) end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 314). -spec parse(list(binary())) -> state(). parse(Args) -> State = {state, gleam@dict:new(), gleam@dict:new(), gleam@list:new()}, State@1 = parse_args(Args, State), erlang:setelement(4, State@1, lists:reverse(erlang:element(4, State@1))). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 220). -spec positional_arguments(fun((list(binary())) -> decode@zero:decoder(GWC))) -> decode@zero:decoder(GWC). positional_arguments(Next) -> decode@zero:field( <<"_"/utf8>>, decode@zero:list({decoder, fun decode@zero:decode_string/1}), fun(Args) -> Next(Args) end ). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 321). -spec to_dynamic(state()) -> gleam@dynamic:dynamic_(). to_dynamic(State) -> List_opts = gleam@dict:map_values( erlang:element(3, State), fun(_, Values) -> _pipe = lists:reverse(Values), gleam_stdlib:identity(_pipe) end ), _pipe@1 = erlang:element(2, State), _pipe@2 = gleam@dict:merge(_pipe@1, List_opts), _pipe@3 = gleam@dict:insert( _pipe@2, <<"_"/utf8>>, gleam_stdlib:identity(erlang:element(4, State)) ), gleam_stdlib:identity(_pipe@3). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 194). -spec decode(list(binary()), decode@zero:decoder(GVW)) -> {ok, GVW} | {error, list(gleam@dynamic:decode_error())}. decode(Args, Decoder) -> _pipe = parse(Args), _pipe@1 = to_dynamic(_pipe), decode@zero:run(_pipe@1, Decoder). -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 481). -spec parse_cluster(binary(), state(), gleam@option:option(binary())) -> {state(), list(binary())}. parse_cluster(Cluster, State, Next) -> case gleam@string:pop_grapheme(Cluster) of {ok, {H, Rest}} -> case {is_alpha(H), is_number(Rest)} of {true, true} -> {set_arg(State, H, Rest), begin _pipe = gleam@option:map( Next, fun gleam@list:wrap/1 ), gleam@option:unwrap(_pipe, []) end}; {_, _} -> New_state = set_arg(State, H, <<"true"/utf8>>), parse_short_arg(Rest, New_state, Next) end; _ -> {State, begin _pipe@1 = gleam@option:map(Next, fun gleam@list:wrap/1), gleam@option:unwrap(_pipe@1, []) end} end. -file("/Users/ryan.miville/dev/clad/src/clad.gleam", 464). -spec parse_short_arg(binary(), state(), gleam@option:option(binary())) -> {state(), list(binary())}. parse_short_arg(Arg, State, Next) -> case gleam@string:length(Arg) of 1 -> Value = gleam@option:unwrap(Next, <<"true"/utf8>>), New_state = set_arg(State, Arg, Value), {New_state, []}; _ -> parse_cluster(Arg, State, Next) end.