-module(outil@opt). -compile(no_auto_import). -export([bool/4, bool_/5, float/5, float_/6, int/5, int_/6, string/5, string_/6, with_named_option/5, named_opt_parser/4]). -spec bool( outil@core:command(ETR), binary(), binary(), fun((fun((list(binary())) -> {ok, boolean()} | {error, outil@core:error()}), outil@core:command(ETR)) -> outil@core:command(ETR)) ) -> outil@core:command(ETR). bool(Cmd, Long, Description, Continue) -> bool_(Cmd, Long, none, Description, Continue). -spec bool_( outil@core:command(ETW), binary(), gleam@option:option(binary()), binary(), fun((fun((list(binary())) -> {ok, boolean()} | {error, outil@core:error()}), outil@core:command(ETW)) -> outil@core:command(ETW)) ) -> outil@core:command(ETW). bool_(Cmd, Long, Short, Description, Continue) -> Opt = {opt, Long, Short, Description, bool_opt}, Continue(bool_opt_parser(Long, Short), add_option(Cmd, Opt)). -spec float( outil@core:command(EUC), binary(), binary(), float(), fun((fun((list(binary())) -> {ok, float()} | {error, outil@core:error()}), outil@core:command(EUC)) -> outil@core:command(EUC)) ) -> outil@core:command(EUC). float(Cmd, Long, Description, Default, Continue) -> float_(Cmd, Long, none, Description, Default, Continue). -spec float_( outil@core:command(EUH), binary(), gleam@option:option(binary()), binary(), float(), fun((fun((list(binary())) -> {ok, float()} | {error, outil@core:error()}), outil@core:command(EUH)) -> outil@core:command(EUH)) ) -> outil@core:command(EUH). float_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {float_opt, Default}}, with_named_option(Cmd, Opt, Default, fun gleam@float:parse/1, Continue). -spec int( outil@core:command(EUN), binary(), binary(), integer(), fun((fun((list(binary())) -> {ok, integer()} | {error, outil@core:error()}), outil@core:command(EUN)) -> outil@core:command(EUN)) ) -> outil@core:command(EUN). int(Cmd, Long, Description, Default, Continue) -> int_(Cmd, Long, none, Description, Default, Continue). -spec int_( outil@core:command(EUS), binary(), gleam@option:option(binary()), binary(), integer(), fun((fun((list(binary())) -> {ok, integer()} | {error, outil@core:error()}), outil@core:command(EUS)) -> outil@core:command(EUS)) ) -> outil@core:command(EUS). int_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {int_opt, Default}}, with_named_option(Cmd, Opt, Default, fun gleam@int:parse/1, Continue). -spec string( outil@core:command(EUY), binary(), binary(), binary(), fun((fun((list(binary())) -> {ok, binary()} | {error, outil@core:error()}), outil@core:command(EUY)) -> outil@core:command(EUY)) ) -> outil@core:command(EUY). string(Cmd, Long, Description, Default, Continue) -> string_(Cmd, Long, none, Description, Default, Continue). -spec string_( outil@core:command(EVD), binary(), gleam@option:option(binary()), binary(), binary(), fun((fun((list(binary())) -> {ok, binary()} | {error, outil@core:error()}), outil@core:command(EVD)) -> outil@core:command(EVD)) ) -> outil@core:command(EVD). string_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {string_opt, Default}}, with_named_option( Cmd, Opt, Default, fun(Field@0) -> {ok, Field@0} end, Continue ). -spec with_named_option( outil@core:command(EVJ), outil@core:opt(), EVL, fun((binary()) -> {ok, EVL} | {error, nil}), fun((fun((list(binary())) -> {ok, EVL} | {error, outil@core:error()}), outil@core:command(EVJ)) -> outil@core:command(EVJ)) ) -> outil@core:command(EVJ). with_named_option(Cmd, Opt, Default, Parse, Continue) -> Continue( named_opt_parser( erlang:element(2, Opt), erlang:element(3, Opt), Parse, Default ), add_option(Cmd, Opt) ). -spec add_option(outil@core:command(EVR), outil@core:opt()) -> outil@core:command(EVR). add_option(Cmd, Opt) -> {command, erlang:element(2, Cmd), erlang:element(3, Cmd), erlang:element(4, Cmd), gleam@list:append(erlang:element(5, Cmd), [Opt]), erlang:element(6, Cmd)}. -spec named_opt_parser( binary(), gleam@option:option(binary()), fun((binary()) -> {ok, EVV} | {error, nil}), EVV ) -> fun((list(binary())) -> {ok, EVV} | {error, outil@core:error()}). named_opt_parser(Long, Short, Parser, Default) -> fun(Args) -> case find(Long, Short, Args) of {none, _} -> {ok, Default}; {{some, _}, {some, Arg}} -> _pipe = Parser(Arg), gleam@result:map_error( _pipe, fun(_) -> {malformed_argument, Long, Arg} end ); {{some, _}, none} -> {error, {missing_argument, Long}} end end. -spec bool_opt_parser(binary(), gleam@option:option(binary())) -> fun((list(binary())) -> {ok, boolean()} | {error, outil@core:error()}). bool_opt_parser(Long, Short) -> fun(Args) -> {Opt, Arg} = find(Long, Short, Args), case Opt of none -> {ok, false}; {some, _} -> case Arg of none -> {ok, true}; {some, Arg@1} -> _pipe = outil@core:parse_bool(Arg@1), gleam@result:map_error( _pipe, fun(_) -> {malformed_argument, Long, Arg@1} end ) end end end. -spec find(binary(), gleam@option:option(binary()), list(binary())) -> {gleam@option:option(binary()), gleam@option:option(binary())}. find(Long, Short, Args) -> Long_opt = <<"--"/utf8, Long/binary>>, Short_opt = begin _pipe = Short, gleam@option:map(_pipe, fun(S) -> <<"-"/utf8, S/binary>> end) end, Opt = gleam@list:find( Args, fun(Arg) -> Is_long = gleam@string:starts_with(Arg, Long_opt), Is_short = begin _pipe@1 = Short_opt, _pipe@2 = gleam@option:map( _pipe@1, fun(S@1) -> gleam@string:starts_with(Arg, S@1) end ), gleam@option:unwrap(_pipe@2, false) end, Is_long orelse Is_short end ), case Opt of {error, _} -> {none, none}; {ok, Opt@1} -> Arg@1 = begin _pipe@3 = gleam@string:split(Opt@1, <<"="/utf8>>), gleam@list:at(_pipe@3, 1) end, {{some, Opt@1}, gleam@option:from_result(Arg@1)} end.