Packages
glint
0.1.2
1.3.0
1.2.1
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc5
1.0.0-rc4
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.18.1
0.18.0
0.17.1
0.17.0
0.16.0
0.16.0-rc1
0.15.0
0.15.0-rc2
0.15.0-rc1
0.14.0
0.13.0
0.12.0
0.12.0-rc6
0.12.0-rc5
0.12.0-rc4
0.12.0-rc3
0.12.0-rc2
0.12.0-rc1
0.11.4
0.11.3
0.11.2
0.11.0
0.10.0
0.9.0
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Gleam command-line argument parsing with flags and automated help text generation.
Current section
Files
Jump to
Current section
Files
src/glint@flag.erl
-module(glint@flag).
-compile(no_auto_import).
-export([int/2, string/2, bool/2, build_map/1, update_flags/2]).
-export_type([flag_value/0, flag/0]).
-type flag_value() :: {bool_flag, boolean()} |
{int_flag, integer()} |
{string_flag, binary()}.
-type flag() :: {flag, binary(), flag_value()}.
-spec int(binary(), integer()) -> flag().
int(Name, Value) ->
{flag, Name, {int_flag, Value}}.
-spec string(binary(), binary()) -> flag().
string(Name, Value) ->
{flag, Name, {string_flag, Value}}.
-spec bool(binary(), boolean()) -> flag().
bool(Name, Value) ->
{flag, Name, {bool_flag, Value}}.
-spec build_map(list(flag())) -> gleam@map:map_(binary(), flag_value()).
build_map(Flags) ->
gleam@list:fold(
Flags,
gleam@map:new(),
fun(M, Flag) ->
gleam@map:insert(
M,
erlang:element(2, Flag),
erlang:element(3, Flag)
)
end
).
-spec update_flags(gleam@map:map_(binary(), flag_value()), binary()) -> {ok,
gleam@map:map_(binary(), flag_value())} |
{error, snag:snag()}.
update_flags(Flags, Flag_input) ->
case begin
_pipe = Flag_input,
_pipe@1 = gleam@string:split_once(_pipe, <<"="/utf8>>),
gleam@result:replace_error(_pipe@1, no_value_flag_err(Flag_input))
end of
{error, _try} -> {error, _try};
{ok, {Key, Value}} ->
case begin
_pipe@2 = gleam@map:get(Flags, Key),
gleam@result:replace_error(_pipe@2, undefined_flag_err(Key))
end of
{error, _try@1} -> {error, _try@1};
{ok, Default} ->
case case Default of
{int_flag, _@1} ->
_pipe@3 = Value,
_pipe@4 = gleam@int:parse(_pipe@3),
_pipe@5 = gleam@result:replace_error(
_pipe@4,
int_flag_err(Key, Value)
),
gleam@result:map(
_pipe@5,
fun(A) -> {int_flag, A} end
);
{string_flag, _@2} ->
{ok, {string_flag, Value}};
{bool_flag, _@3} ->
case Value of
<<"true"/utf8>> ->
{ok, {bool_flag, true}};
<<"false"/utf8>> ->
{ok, {bool_flag, false}};
_@4 ->
{error, bool_flag_err(Key, Value)}
end
end of
{error, _try@2} -> {error, _try@2};
{ok, New_value} ->
{ok, gleam@map:insert(Flags, Key, New_value)}
end
end
end.
-spec layer_invalid_flag(snag:snag(), binary()) -> snag:snag().
layer_invalid_flag(Err, Flag) ->
_pipe = [<<"invalid flag '"/utf8>>, Flag, <<"'"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
snag:layer(Err, _pipe@1).
-spec no_value_flag_err(binary()) -> snag:snag().
no_value_flag_err(Flag_input) ->
_pipe = [<<"flag '"/utf8>>, Flag_input, <<"' has no assigned value"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Flag_input).
-spec undefined_flag_err(binary()) -> snag:snag().
undefined_flag_err(Key) ->
_pipe = <<"flag provided but not defined"/utf8>>,
_pipe@1 = snag:new(_pipe),
layer_invalid_flag(_pipe@1, Key).
-spec int_flag_err(binary(), binary()) -> snag:snag().
int_flag_err(Key, Value) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as int"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).
-spec bool_flag_err(binary(), binary()) -> snag:snag().
bool_flag_err(Key, Value) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as boolean"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).