Current section

51 Versions

Jump to

Compare versions

7 files changed
+349 additions
-350 deletions
  @@ -1,5 +1,5 @@
1 1 name = "glint"
2 - version = "0.11.0"
2 + version = "0.11.2"
3 3
4 4 # Fill out these fields if you intend to generate HTML documentation or publish
5 5 # your project to the Hex package manager.
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"glint">>}.
2 2 {<<"app">>, <<"glint">>}.
3 - {<<"version">>, <<"0.11.0">>}.
3 + {<<"version">>, <<"0.11.2">>}.
4 4 {<<"description">>, <<"Gleam command line argument parsing with basic flag support.">>}.
5 5 {<<"licenses">>, [<<"Apache-2.0">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -15,6 +15,11 @@
15 15 {<<"optional">>, false},
16 16 {<<"requirement">>, <<"~> 0.19">>}
17 17 ]},
18 + {<<"snag">>, [
19 + {<<"app">>, <<"snag">>},
20 + {<<"optional">>, false},
21 + {<<"requirement">>, <<"~> 0.2">>}
22 + ]},
18 23 {<<"gleam_community_ansi">>, [
19 24 {<<"app">>, <<"gleam_community_ansi">>},
20 25 {<<"optional">>, false},
  @@ -24,11 +29,6 @@
24 29 {<<"app">>, <<"gleam_community_colour">>},
25 30 {<<"optional">>, false},
26 31 {<<"requirement">>, <<"~> 1.0">>}
27 - ]},
28 - {<<"snag">>, [
29 - {<<"app">>, <<"snag">>},
30 - {<<"optional">>, false},
31 - {<<"requirement">>, <<"~> 0.2">>}
32 32 ]}
33 33 ]}.
34 34 {<<"files">>, [
  @@ -1,5 +1,5 @@
1 1 {application, glint, [
2 - {vsn, "0.11.0"},
2 + {vsn, "0.11.2"},
3 3 {applications, [gleam_community_ansi,
4 4 gleam_community_colour,
5 5 gleam_stdlib,
  @@ -1,12 +1,12 @@
1 1 -module(glint).
2 - -compile(no_auto_import).
2 + -compile([no_auto_import, nowarn_unused_vars]).
3 3
4 - -export([add_command_from_stub/2, new/0, with_config/2, with_global_flags/2, with_pretty_help/2, without_pretty_help/1, default_pretty_help/0, add_command/5, execute/2, run/2, help_flag/0]).
4 + -export([with_config/2, with_global_flags/2, new/0, with_pretty_help/2, without_pretty_help/1, default_pretty_help/0, help_flag/0, add_command/5, add_command_from_stub/2, execute/2, run/2]).
5 5 -export_type([glint/1, config/0, pretty_help/0, command_input/0, contents/1, command/1, stub/1, out/1]).
6 6
7 - -opaque glint(FKL) :: {glint,
7 + -opaque glint(FMK) :: {glint,
8 8 config(),
9 - command(FKL),
9 + command(FMK),
10 10 gleam@map:map_(binary(), glint@flag:contents())}.
11 11
12 12 -type config() :: {config, gleam@option:option(pretty_help())}.
  @@ -20,42 +20,28 @@
20 20 list(binary()),
21 21 gleam@map:map_(binary(), glint@flag:contents())}.
22 22
23 - -opaque contents(FKN) :: {contents,
24 - fun((command_input()) -> FKN),
23 + -opaque contents(FML) :: {contents,
24 + fun((command_input()) -> FML),
25 25 gleam@map:map_(binary(), glint@flag:contents()),
26 26 binary()}.
27 27
28 - -opaque command(FKO) :: {command,
29 - gleam@option:option(contents(FKO)),
30 - gleam@map:map_(binary(), command(FKO))}.
28 + -opaque command(FMM) :: {command,
29 + gleam@option:option(contents(FMM)),
30 + gleam@map:map_(binary(), command(FMM))}.
31 31
32 - -type stub(FKP) :: {stub,
32 + -type stub(FMN) :: {stub,
33 33 list(binary()),
34 - fun((command_input()) -> FKP),
34 + fun((command_input()) -> FMN),
35 35 list({binary(), glint@flag:contents()}),
36 36 binary()}.
37 37
38 - -type out(FKQ) :: {out, FKQ} | {help, binary()}.
38 + -type out(FMO) :: {out, FMO} | {help, binary()}.
39 39
40 - -spec add_command_from_stub(glint(FLQ), stub(FLQ)) -> glint(FLQ).
41 - add_command_from_stub(Glint, Stub) ->
42 - add_command(
43 - Glint,
44 - erlang:element(2, Stub),
45 - erlang:element(3, Stub),
46 - erlang:element(4, Stub),
47 - erlang:element(5, Stub)
48 - ).
49 -
50 - -spec new() -> glint(any()).
51 - new() ->
52 - {glint, {config, none}, empty_command(), gleam@map:new()}.
53 -
54 - -spec with_config(glint(FLW), config()) -> glint(FLW).
40 + -spec with_config(glint(FMZ), config()) -> glint(FMZ).
55 41 with_config(Glint, Config) ->
56 42 erlang:setelement(2, Glint, Config).
57 43
58 - -spec with_global_flags(glint(FLZ), list({binary(), glint@flag:contents()})) -> glint(FLZ).
44 + -spec with_global_flags(glint(FNC), list({binary(), glint@flag:contents()})) -> glint(FNC).
59 45 with_global_flags(Glint, Flags) ->
60 46 erlang:setelement(
61 47 4,
  @@ -73,11 +59,15 @@ with_global_flags(Glint, Flags) ->
73 59 empty_command() ->
74 60 {command, none, gleam@map:new()}.
75 61
76 - -spec with_pretty_help(glint(FMF), pretty_help()) -> glint(FMF).
62 + -spec new() -> glint(any()).
63 + new() ->
64 + {glint, {config, none}, empty_command(), gleam@map:new()}.
65 +
66 + -spec with_pretty_help(glint(FNI), pretty_help()) -> glint(FNI).
77 67 with_pretty_help(Glint, Pretty) ->
78 68 erlang:setelement(2, Glint, {config, {some, Pretty}}).
79 69
80 - -spec without_pretty_help(glint(FMI)) -> glint(FMI).
70 + -spec without_pretty_help(glint(FNL)) -> glint(FNL).
81 71 without_pretty_help(Glint) ->
82 72 erlang:setelement(2, Glint, {config, none}).
83 73
  @@ -118,67 +108,12 @@ default_pretty_help() ->
118 108 end,
119 109 {pretty_help, Usage_colour, Flags_colour, Subcommands_colour}.
120 110
121 - -spec sanitize_path(list(binary())) -> list(binary()).
122 - sanitize_path(Path) ->
123 - _pipe = Path,
124 - _pipe@1 = gleam@list:map(_pipe, fun gleam@string:trim/1),
125 - gleam@list:filter(_pipe@1, fun is_not_empty/1).
126 -
127 - -spec add_command(
128 - glint(FMN),
129 - list(binary()),
130 - fun((command_input()) -> FMN),
131 - list({binary(), glint@flag:contents()}),
132 - binary()
133 - ) -> glint(FMN).
134 - add_command(Glint, Path, F, Flags, Description) ->
135 - erlang:setelement(
136 - 3,
137 - Glint,
138 - begin
139 - _pipe = Path,
140 - _pipe@1 = sanitize_path(_pipe),
141 - do_add_command(
142 - erlang:element(3, Glint),
143 - _pipe@1,
144 - {contents, F, glint@flag:build_map(Flags), Description}
145 - )
146 - end
147 - ).
148 -
149 - -spec do_add_command(command(FMT), list(binary()), contents(FMT)) -> command(FMT).
150 - do_add_command(Root, Path, Contents) ->
151 - case Path of
152 - [] ->
153 - erlang:setelement(2, Root, {some, Contents});
154 -
155 - [X | Xs] ->
156 - erlang:setelement(
157 - 3,
158 - Root,
159 - begin
160 - gleam@map:update(
161 - erlang:element(3, Root),
162 - X,
163 - fun(Node) ->
164 - _pipe = Node,
165 - _pipe@1 = gleam@option:lazy_unwrap(
166 - _pipe,
167 - fun empty_command/0
168 - ),
169 - do_add_command(_pipe@1, Xs, Contents)
170 - end
171 - )
172 - end
173 - )
174 - end.
175 -
176 111 -spec execute_root(
177 - command(FMY),
112 + command(FOB),
178 113 gleam@map:map_(binary(), glint@flag:contents()),
179 114 list(binary()),
180 115 list(binary())
181 - ) -> {ok, out(FMY)} | {error, snag:snag()}.
116 + ) -> {ok, out(FOB)} | {error, snag:snag()}.
182 117 execute_root(Cmd, Global_flags, Args, Flag_inputs) ->
183 118 _pipe@3 = case erlang:element(2, Cmd) of
184 119 {some, Contents} ->
  @@ -201,94 +136,16 @@ execute_root(Cmd, Global_flags, Args, Flag_inputs) ->
201 136 end,
202 137 snag:context(_pipe@3, <<"failed to run command"/utf8>>).
203 138
204 - -spec execute(glint(FND), list(binary())) -> {ok, out(FND)} |
205 - {error, snag:snag()}.
206 - execute(Glint, Args) ->
207 - Help_flag = help_flag(),
208 - {Help, Args@2} = case gleam@list:pop(Args, fun(S) -> S =:= Help_flag end) of
209 - {ok, {_, Args@1}} ->
210 - {true, Args@1};
211 -
212 - _ ->
213 - {false, Args}
214 - end,
215 - {Flags, Args@3} = gleam@list:partition(
216 - Args@2,
217 - fun(_capture) -> gleam@string:starts_with(_capture, <<"--"/utf8>>) end
218 - ),
219 - do_execute(
220 - erlang:element(3, Glint),
221 - erlang:element(2, erlang:element(2, Glint)),
222 - erlang:element(4, Glint),
223 - Args@3,
224 - Flags,
225 - Help,
226 - []
227 - ).
228 -
229 - -spec do_execute(
230 - command(FNH),
231 - gleam@option:option(pretty_help()),
232 - gleam@map:map_(binary(), glint@flag:contents()),
233 - list(binary()),
234 - list(binary()),
235 - boolean(),
236 - list(binary())
237 - ) -> {ok, out(FNH)} | {error, snag:snag()}.
238 - do_execute(Cmd, Pretty_help, Global_flags, Args, Flags, Help, Command_path) ->
239 - case Args of
240 - [] when Help ->
241 - _pipe = Command_path,
242 - _pipe@1 = cmd_help(_pipe, Cmd, Pretty_help, Global_flags),
243 - _pipe@2 = {help, _pipe@1},
244 - {ok, _pipe@2};
245 -
246 - [] ->
247 - execute_root(Cmd, Global_flags, [], Flags);
248 -
249 - [Arg | Rest] ->
250 - case gleam@map:get(erlang:element(3, Cmd), Arg) of
251 - {ok, Cmd@1} ->
252 - do_execute(
253 - Cmd@1,
254 - Pretty_help,
255 - Global_flags,
256 - Rest,
257 - Flags,
258 - Help,
259 - [Arg | Command_path]
260 - );
261 -
262 - _ when Help ->
263 - _pipe@3 = Command_path,
264 - _pipe@4 = cmd_help(_pipe@3, Cmd, Pretty_help, Global_flags),
265 - _pipe@5 = {help, _pipe@4},
266 - {ok, _pipe@5};
267 -
268 - _ ->
269 - execute_root(Cmd, Global_flags, Args, Flags)
270 - end
271 - end.
272 -
273 - -spec run(glint(any()), list(binary())) -> nil.
274 - run(Glint, Args) ->
275 - case execute(Glint, Args) of
276 - {error, Err} ->
277 - _pipe = Err,
278 - _pipe@1 = snag:pretty_print(_pipe),
279 - gleam@io:println(_pipe@1);
280 -
281 - {ok, {help, Help}} ->
282 - gleam@io:println(Help);
283 -
284 - _ ->
285 - nil
286 - end.
287 -
288 139 -spec is_not_empty(binary()) -> boolean().
289 140 is_not_empty(S) ->
290 141 S /= <<""/utf8>>.
291 142
143 + -spec sanitize_path(list(binary())) -> list(binary()).
144 + sanitize_path(Path) ->
145 + _pipe = Path,
146 + _pipe@1 = gleam@list:map(_pipe, fun gleam@string:trim/1),
147 + gleam@list:filter(_pipe@1, fun is_not_empty/1).
148 +
292 149 -spec help_flag() -> binary().
293 150 help_flag() ->
294 151 <<(<<"--"/utf8>>)/binary, "help"/utf8>>.
  @@ -303,6 +160,37 @@ wrap_with_space(S) ->
303 160 <<<<" "/utf8, S/binary>>/binary, " "/utf8>>
304 161 end.
305 162
163 + -spec subcommand_help(binary(), command(any())) -> binary().
164 + subcommand_help(Name, Cmd) ->
165 + case erlang:element(2, Cmd) of
166 + none ->
167 + Name;
168 +
169 + {some, Contents} ->
170 + <<<<Name/binary, "\t\t"/utf8>>/binary,
171 + (erlang:element(4, Contents))/binary>>
172 + end.
173 +
174 + -spec subcommands_help(gleam@map:map_(binary(), command(any()))) -> binary().
175 + subcommands_help(Cmds) ->
176 + _pipe = Cmds,
177 + _pipe@1 = gleam@map:map_values(_pipe, fun subcommand_help/2),
178 + _pipe@2 = gleam@map:values(_pipe@1),
179 + _pipe@3 = gleam@list:sort(_pipe@2, fun gleam@string:compare/2),
180 + gleam@string:join(_pipe@3, <<"\n\t"/utf8>>).
181 +
182 + -spec heading_style(binary(), gleam_community@colour:colour()) -> binary().
183 + heading_style(Heading, Colour) ->
184 + _pipe = Heading,
185 + _pipe@1 = gleam_community@ansi:bold(_pipe),
186 + _pipe@2 = gleam_community@ansi:underline(_pipe@1),
187 + _pipe@3 = gleam_community@ansi:italic(_pipe@2),
188 + _pipe@4 = gleam_community@ansi:hex(
189 + _pipe@3,
190 + gleam_community@colour:to_rgb_hex(Colour)
191 + ),
192 + gleam_community@ansi:reset(_pipe@4).
193 +
306 194 -spec usage_help(
307 195 binary(),
308 196 gleam@map:map_(binary(), glint@flag:contents()),
  @@ -426,33 +314,145 @@ cmd_help(Path, Cmd, Pretty_help, Global_flags) ->
426 314 _pipe@13 = gleam@list:filter(_pipe@12, fun is_not_empty/1),
427 315 gleam@string:join(_pipe@13, <<"\n\n"/utf8>>).
428 316
429 - -spec subcommands_help(gleam@map:map_(binary(), command(any()))) -> binary().
430 - subcommands_help(Cmds) ->
431 - _pipe = Cmds,
432 - _pipe@1 = gleam@map:map_values(_pipe, fun subcommand_help/2),
433 - _pipe@2 = gleam@map:values(_pipe@1),
434 - _pipe@3 = gleam@list:sort(_pipe@2, fun gleam@string:compare/2),
435 - gleam@string:join(_pipe@3, <<"\n\t"/utf8>>).
317 + -spec do_add_command(command(FNW), list(binary()), contents(FNW)) -> command(FNW).
318 + do_add_command(Root, Path, Contents) ->
319 + case Path of
320 + [] ->
321 + erlang:setelement(2, Root, {some, Contents});
436 322
437 - -spec subcommand_help(binary(), command(any())) -> binary().
438 - subcommand_help(Name, Cmd) ->
439 - case erlang:element(2, Cmd) of
440 - none ->
441 - Name;
442 -
443 - {some, Contents} ->
444 - <<<<Name/binary, "\t\t"/utf8>>/binary,
445 - (erlang:element(4, Contents))/binary>>
323 + [X | Xs] ->
324 + erlang:setelement(
325 + 3,
326 + Root,
327 + begin
328 + gleam@map:update(
329 + erlang:element(3, Root),
330 + X,
331 + fun(Node) ->
332 + _pipe = Node,
333 + _pipe@1 = gleam@option:lazy_unwrap(
334 + _pipe,
335 + fun empty_command/0
336 + ),
337 + do_add_command(_pipe@1, Xs, Contents)
338 + end
339 + )
340 + end
341 + )
446 342 end.
447 343
448 - -spec heading_style(binary(), gleam_community@colour:colour()) -> binary().
449 - heading_style(Heading, Colour) ->
450 - _pipe = Heading,
451 - _pipe@1 = gleam_community@ansi:bold(_pipe),
452 - _pipe@2 = gleam_community@ansi:underline(_pipe@1),
453 - _pipe@3 = gleam_community@ansi:italic(_pipe@2),
454 - _pipe@4 = gleam_community@ansi:hex(
455 - _pipe@3,
456 - gleam_community@colour:to_rgb_hex(Colour)
344 + -spec add_command(
345 + glint(FNQ),
346 + list(binary()),
347 + fun((command_input()) -> FNQ),
348 + list({binary(), glint@flag:contents()}),
349 + binary()
350 + ) -> glint(FNQ).
351 + add_command(Glint, Path, F, Flags, Description) ->
352 + erlang:setelement(
353 + 3,
354 + Glint,
355 + begin
356 + _pipe = Path,
357 + _pipe@1 = sanitize_path(_pipe),
358 + do_add_command(
359 + erlang:element(3, Glint),
360 + _pipe@1,
361 + {contents, F, glint@flag:build_map(Flags), Description}
362 + )
363 + end
364 + ).
365 +
366 + -spec add_command_from_stub(glint(FMT), stub(FMT)) -> glint(FMT).
367 + add_command_from_stub(Glint, Stub) ->
368 + add_command(
369 + Glint,
370 + erlang:element(2, Stub),
371 + erlang:element(3, Stub),
372 + erlang:element(4, Stub),
373 + erlang:element(5, Stub)
374 + ).
375 +
376 + -spec do_execute(
377 + command(FOK),
378 + gleam@option:option(pretty_help()),
379 + gleam@map:map_(binary(), glint@flag:contents()),
380 + list(binary()),
381 + list(binary()),
382 + boolean(),
383 + list(binary())
384 + ) -> {ok, out(FOK)} | {error, snag:snag()}.
385 + do_execute(Cmd, Pretty_help, Global_flags, Args, Flags, Help, Command_path) ->
386 + case Args of
387 + [] when Help ->
388 + _pipe = Command_path,
389 + _pipe@1 = cmd_help(_pipe, Cmd, Pretty_help, Global_flags),
390 + _pipe@2 = {help, _pipe@1},
391 + {ok, _pipe@2};
392 +
393 + [] ->
394 + execute_root(Cmd, Global_flags, [], Flags);
395 +
396 + [Arg | Rest] ->
397 + case gleam@map:get(erlang:element(3, Cmd), Arg) of
398 + {ok, Cmd@1} ->
399 + do_execute(
400 + Cmd@1,
401 + Pretty_help,
402 + Global_flags,
403 + Rest,
404 + Flags,
405 + Help,
406 + [Arg | Command_path]
407 + );
408 +
409 + _ when Help ->
410 + _pipe@3 = Command_path,
411 + _pipe@4 = cmd_help(_pipe@3, Cmd, Pretty_help, Global_flags),
412 + _pipe@5 = {help, _pipe@4},
413 + {ok, _pipe@5};
414 +
415 + _ ->
416 + execute_root(Cmd, Global_flags, Args, Flags)
417 + end
418 + end.
419 +
420 + -spec execute(glint(FOG), list(binary())) -> {ok, out(FOG)} |
421 + {error, snag:snag()}.
422 + execute(Glint, Args) ->
423 + Help_flag = help_flag(),
424 + {Help, Args@2} = case gleam@list:pop(Args, fun(S) -> S =:= Help_flag end) of
425 + {ok, {_, Args@1}} ->
426 + {true, Args@1};
427 +
428 + _ ->
429 + {false, Args}
430 + end,
431 + {Flags, Args@3} = gleam@list:partition(
432 + Args@2,
433 + fun(_capture) -> gleam@string:starts_with(_capture, <<"--"/utf8>>) end
457 434 ),
458 - gleam_community@ansi:reset(_pipe@4).
435 + do_execute(
436 + erlang:element(3, Glint),
437 + erlang:element(2, erlang:element(2, Glint)),
438 + erlang:element(4, Glint),
439 + Args@3,
440 + Flags,
441 + Help,
442 + []
443 + ).
444 +
445 + -spec run(glint(any()), list(binary())) -> nil.
446 + run(Glint, Args) ->
447 + case execute(Glint, Args) of
448 + {error, Err} ->
449 + _pipe = Err,
450 + _pipe@1 = snag:pretty_print(_pipe),
451 + gleam@io:println(_pipe@1);
452 +
453 + {ok, {help, Help}} ->
454 + gleam@io:println(Help);
455 +
456 + _ ->
457 + nil
458 + end.
  @@ -126,9 +126,9 @@ pub fn without_pretty_help(glint: Glint(a)) -> Glint(a) {
126 126 /// buttercup (r: 252, g: 226, b: 174) colour for subcommands
127 127 ///
128 128 pub fn default_pretty_help() -> PrettyHelp {
129 - assert Ok(usage_colour) = colour.from_rgb255(182, 255, 234)
130 - assert Ok(flags_colour) = colour.from_rgb255(255, 175, 243)
131 - assert Ok(subcommands_colour) = colour.from_rgb255(252, 226, 174)
129 + let assert Ok(usage_colour) = colour.from_rgb255(182, 255, 234)
130 + let assert Ok(flags_colour) = colour.from_rgb255(255, 175, 243)
131 + let assert Ok(subcommands_colour) = colour.from_rgb255(252, 226, 174)
132 132
133 133 PrettyHelp(
134 134 usage: usage_colour,
Loading more files…