-module(clip@flag). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/clip/flag.gleam"). -export([to_arg_info/1, help/2, new/1, short/2, run/2]). -export_type([flag/0]). -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 `Flag`s. A `Flag` is a named option with no\n" " associated value, such as `--debug`. A `Flag` produces `True` when present\n" " and `False` when not present.\n" ). -opaque flag() :: {flag, binary(), gleam@option:option(binary()), gleam@option:option(binary())}. -file("src/clip/flag.gleam", 14). ?DOC(false). -spec to_arg_info(flag()) -> clip@arg_info:arg_info(). to_arg_info(Flag) -> _record = clip@arg_info:empty(), {arg_info, erlang:element(2, _record), erlang:element(3, _record), [{flag_info, erlang:element(2, Flag), erlang:element(4, Flag), erlang:element(3, Flag)}], erlang:element(5, _record)}. -file("src/clip/flag.gleam", 21). ?DOC(" Add help text to a `Flag`.\n"). -spec help(flag(), binary()) -> flag(). help(Flag, Help) -> {flag, erlang:element(2, Flag), {some, Help}, erlang:element(4, Flag)}. -file("src/clip/flag.gleam", 27). ?DOC( " Create a new `Flag` with the provided name. `Flag`s always produce a `Bool`\n" " -- `True` if present and `False` if not present.\n" ). -spec new(binary()) -> flag(). new(Name) -> {flag, Name, none, none}. -file("src/clip/flag.gleam", 41). ?DOC( " Add a short name for the given `Flag`. 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.flag(flag.new(\"debug\") |> flag.short(\"d\"))\n" " |> clip.run([\"-d\"])\n" "\n" " // Ok(True)\n" " ```\n" ). -spec short(flag(), binary()) -> flag(). short(Flag, Short) -> {flag, erlang:element(2, Flag), erlang:element(3, Flag), {some, Short}}. -file("src/clip/flag.gleam", 46). ?DOC(false). -spec run(flag(), list(binary())) -> {ok, {boolean(), list(binary())}} | {error, binary()}. run(Flag, Args) -> Long_name = <<"--"/utf8, (erlang:element(2, Flag))/binary>>, Short_name = gleam@option:map( erlang:element(4, Flag), fun(S) -> <<"-"/utf8, S/binary>> end ), case Args of [] -> {ok, {false, []}}; [Head | Rest] when (Long_name =:= Head) orelse (Short_name =:= {some, Head}) -> {ok, {true, Rest}}; [Head@1 | Rest@1] -> _pipe = run(Flag, Rest@1), gleam@result:map( _pipe, fun(V) -> {erlang:element(1, V), [Head@1 | erlang:element(2, V)]} end ) end.