Skip to content

Commit 18709aa

Browse files
authored
Merge pull request #224 from roc-lang/weaver-updates
weaver updates
2 parents 04438a7 + 746874c commit 18709aa

File tree

2 files changed

+102
-11
lines changed

2 files changed

+102
-11
lines changed

examples/args.roc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ app [main] {
33
}
44

55
import pf.Stdout
6+
import pf.Stderr
67
import pf.Task exposing [Task]
78
import pf.Arg.Cli as Cli
89
import pf.Arg.Subcommand as Subcommand
@@ -11,19 +12,26 @@ import pf.Arg.Param as Param
1112
import pf.Arg
1213

1314
main =
14-
{ command: subcommand } = Arg.parse! cli
15+
args = Arg.list!
1516

16-
result =
17-
when subcommand is
18-
Max { first, rest } ->
19-
rest
20-
|> List.walk first \max, n ->
21-
Num.max max n
17+
when Cli.parseOrDisplayMessage cli args is
18+
Ok { command: subcommand } ->
19+
mathOutcome =
20+
when subcommand is
21+
Max { first, rest } ->
22+
rest
23+
|> List.walk first \max, n ->
24+
Num.max max n
2225

23-
Div { dividend, divisor } ->
24-
dividend / divisor
26+
Div { dividend, divisor } ->
27+
dividend / divisor
2528

26-
Stdout.line (Num.toStr result)
29+
Stdout.line (Num.toStr mathOutcome)
30+
31+
Err message ->
32+
Stderr.line! message
33+
34+
Task.err (Exit 1 "")
2735

2836
cli =
2937
Cli.build {

platform/Arg/Cli.roc

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ module [
102102
finish,
103103
finishWithoutValidating,
104104
assertValid,
105+
parseOrDisplayMessage,
105106
]
106107

107108
import Arg.Opt
@@ -116,7 +117,8 @@ import Arg.Base exposing [
116117
import Arg.Parser exposing [Arg, parseArgs]
117118
import Arg.Builder exposing [CliBuilder, GetOptionsAction]
118119
import Arg.Validate exposing [validateCli, CliValidationErr]
119-
import Arg.ErrorFormatter exposing [formatCliValidationErr]
120+
import Arg.ErrorFormatter exposing [formatArgExtractErr, formatCliValidationErr]
121+
import Arg.Help exposing [helpText, usageHelp]
120122

121123
## A parser that interprets command line arguments and returns well-formed data.
122124
CliParser state : {
@@ -281,6 +283,87 @@ assertValid = \result ->
281283
Ok cli -> cli
282284
Err err -> crash (formatCliValidationErr err)
283285

286+
## Parse arguments using a CLI parser or show a useful message on failure.
287+
##
288+
## We have the following priorities in returning messages to the user:
289+
## 1) If the `-h/--help` flag is passed, the help page for the command/subcommand
290+
## called will be displayed no matter if your arguments were correctly parsed.
291+
## 2) If the `-V/--version` flag is passed, the version for the app will
292+
## be displayed no matter if your arguments were correctly parsed.
293+
## 3) If the provided arguments were parsed and neither of the above two
294+
## built-in flags were passed, we return to you your data.
295+
## 4) If the provided arguments were not correct, we return a short message
296+
## with which argument was not provided correctly, followed by the
297+
## usage section of the relevant command/subcommand's help text.
298+
##
299+
## ```roc
300+
## exampleCli =
301+
## Cli.build {
302+
## verbosity: <- Opt.count { short: "v", help: "How verbose our logs should be." },
303+
## }
304+
## |> Cli.finish {
305+
## name: "example",
306+
## version: "v0.1.0",
307+
## description: "An example CLI.",
308+
## }
309+
## |> Cli.assertValid
310+
##
311+
## expect
312+
## exampleCli
313+
## |> Cli.parseOrDisplayMessage ["example", "-h"]
314+
## == Err
315+
## """
316+
## example v0.1.0
317+
##
318+
## An example CLI.
319+
##
320+
## Usage:
321+
## example [OPTIONS]
322+
##
323+
## Options:
324+
## -v How verbose our logs should be.
325+
## -h, --help Show this help page.
326+
## -V, --version Show the version.
327+
## """
328+
##
329+
## expect
330+
## exampleCli
331+
## |> Cli.parseOrDisplayMessage ["example", "-V"]
332+
## == Err "v0.1.0"
333+
##
334+
## expect
335+
## exampleCli
336+
## |> Cli.parseOrDisplayMessage ["example", "-v"]
337+
## == Ok { verbosity: 1 }
338+
##
339+
## expect
340+
## exampleCli
341+
## |> Cli.parseOrDisplayMessage ["example", "-x"]
342+
## == Err
343+
## """
344+
## Error: The argument -x was not recognized.
345+
##
346+
## Usage:
347+
## example [OPTIONS]
348+
## """
349+
## ```
350+
parseOrDisplayMessage : CliParser state, List Str -> Result state Str
351+
parseOrDisplayMessage = \parser, args ->
352+
when parser.parser args is
353+
SuccessfullyParsed data -> Ok data
354+
ShowHelp { subcommandPath } -> Err (helpText parser.config subcommandPath parser.textStyle)
355+
ShowVersion -> Err parser.config.version
356+
IncorrectUsage err { subcommandPath } ->
357+
usageStr = usageHelp parser.config subcommandPath parser.textStyle
358+
incorrectUsageStr =
359+
"""
360+
Error: $(formatArgExtractErr err)
361+
362+
$(usageStr)
363+
"""
364+
365+
Err incorrectUsageStr
366+
284367
expect
285368
build {
286369
verbosity: <- Arg.Opt.count { short: "v" },

0 commit comments

Comments
 (0)