-module(clip@opt). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/clip/opt.gleam"). -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]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " Functions for building `Opt`s. An `Opt` is a named option with a\n" " value, such as `--name \"Drew\"`\n" ). -opaque opt(QUZ) :: {opt, binary(), gleam@option:option(QUZ), gleam@option:option(binary()), fun((binary()) -> {ok, QUZ} | {error, binary()}), gleam@option:option(binary())}. -file("src/clip/opt.gleam", 22). ?DOC(false). -spec to_arg_info(opt(any())) -> clip@arg_info:arg_info(). to_arg_info(Opt) -> _record = clip@arg_info:empty(), {arg_info, [{named_info, erlang:element(2, Opt), erlang:element(6, Opt), begin _pipe = erlang:element(3, Opt), gleam@option:map(_pipe, fun gleam@string:inspect/1) end, erlang:element(4, Opt)}], erlang:element(3, _record), erlang:element(4, _record), erlang:element(5, _record)}. -file("src/clip/opt.gleam", 47). ?DOC( " Modify the value produced by an `Opt` in a way that may fail.\n" "\n" " ```gleam\n" " opt.new(\"age\")\n" " |> opt.try_map(fn(age_str) {\n" " case int.parse(age_str) {\n" " Ok(age) -> Ok(age)\n" " Error(Nil) -> Error(\"Unable to parse integer\")\n" " }\n" " })\n" " ```\n" "\n" " Note: `try_map` can change the type of an `Opt` and therefore clears any\n" " previously set default value.\n" ). -spec try_map(opt(QVC), fun((QVC) -> {ok, QVE} | {error, binary()})) -> opt(QVE). try_map(Opt, F) -> {opt, erlang:element(2, Opt), none, erlang:element(4, Opt), fun(Arg) -> gleam@result:'try'( (erlang:element(5, Opt))(Arg), fun(A) -> F(A) end ) end, erlang:element(6, Opt)}. -file("src/clip/opt.gleam", 69). ?DOC( " Modify the value produced by an `Opt` in a way that cannot fail.\n" "\n" " ```gleam\n" " opt.new(\"name\")\n" " |> opt.map(fn(name) { string.uppercase(name) })\n" " ```\n" "\n" " Note: `map` can change the type of an `Opt` and therefore clears any\n" " previously set default value.\n" ). -spec map(opt(QVI), fun((QVI) -> QVK)) -> opt(QVK). map(Opt, F) -> try_map(Opt, fun(A) -> {ok, F(A)} end). -file("src/clip/opt.gleam", 74). ?DOC(" Provide a default value for an `Opt` when it is not provided by the user.\n"). -spec default(opt(QVM), QVM) -> opt(QVM). default(Opt, Default) -> {opt, erlang:element(2, Opt), {some, Default}, erlang:element(4, Opt), erlang:element(5, Opt), erlang:element(6, Opt)}. -file("src/clip/opt.gleam", 79). ?DOC(" Transform an `Opt(a)` to an `Opt(Result(a, Nil)`, making it optional.\n"). -spec optional(opt(QVP)) -> opt({ok, QVP} | {error, nil}). optional(Opt) -> _pipe = Opt, _pipe@1 = map(_pipe, fun(Field@0) -> {ok, Field@0} end), default(_pipe@1, {error, nil}). -file("src/clip/opt.gleam", 84). ?DOC(" Add help text to an `Opt`.\n"). -spec help(opt(QVU), binary()) -> opt(QVU). help(Opt, Help) -> {opt, erlang:element(2, Opt), erlang:element(3, Opt), {some, Help}, erlang:element(5, Opt), erlang:element(6, Opt)}. -file("src/clip/opt.gleam", 91). ?DOC( " Create a new `Opt` with the provided name. New `Opt`s always initially\n" " produce a `String`, which is the unmodified value given by the user on the\n" " command line.\n" ). -spec new(binary()) -> opt(binary()). new(Name) -> {opt, Name, none, none, fun(Field@0) -> {ok, Field@0} end, none}. -file("src/clip/opt.gleam", 105). ?DOC( " Add a short name for the given `Opt`. Short names are provided at the\n" " command line with a single `-` as a prefix.\n" "\n" " ```gleam\n" " clip.command(fn(a) { a })\n" " |> clip.opt(opt.new(\"name\") |> opt.short(\"n\"))\n" " |> clip.run([\"-n\", \"Drew\"])\n" "\n" " // Ok(\"Drew\")\n" " ```\n" ). -spec short(opt(binary()), binary()) -> opt(binary()). short(Opt, Short_name) -> {opt, erlang:element(2, Opt), erlang:element(3, Opt), erlang:element(4, Opt), erlang:element(5, Opt), {some, Short_name}}. -file("src/clip/opt.gleam", 118). ?DOC( " Modify an `Opt(String)` to produce an `Int`.\n" "\n" " ```gleam\n" " opt.new(\"age\")\n" " |> opt.int\n" " ```\n" "\n" " Note: `int` changes the type of an `Opt` and therefore clears any\n" " previously set default value.\n" ). -spec int(opt(binary())) -> opt(integer()). int(Opt) -> _pipe = Opt, try_map(_pipe, fun(Val) -> _pipe@1 = gleam_stdlib:parse_int(Val), gleam@result:map_error( _pipe@1, fun(_) -> <<"Non-integer value provided for "/utf8, (erlang:element(2, Opt))/binary>> end ) end). -file("src/clip/opt.gleam", 135). ?DOC( " Modify an `Opt(String)` to produce a `Float`.\n" "\n" " ```gleam\n" " opt.new(\"height\")\n" " |> opt.float\n" " ```\n" "\n" " Note: `float` changes the type of an `Opt` and therefore clears any\n" " previously set default value.\n" ). -spec float(opt(binary())) -> opt(float()). float(Opt) -> _pipe = Opt, try_map(_pipe, fun(Val) -> _pipe@1 = gleam_stdlib:parse_float(Val), gleam@result:map_error( _pipe@1, fun(_) -> <<"Non-float value provided for "/utf8, (erlang:element(2, Opt))/binary>> end ) end). -file("src/clip/opt.gleam", 144). ?DOC(false). -spec run(opt(QWE), list(binary())) -> {ok, {QWE, 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.