@@ -102,6 +102,7 @@ module [
102102 finish,
103103 finishWithoutValidating,
104104 assertValid,
105+ parseOrDisplayMessage,
105106]
106107
107108import Arg . Opt
@@ -116,7 +117,8 @@ import Arg.Base exposing [
116117import Arg . Parser exposing [Arg , parseArgs]
117118import Arg . Builder exposing [CliBuilder , GetOptionsAction ]
118119import 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.
122124CliParser 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+
284367expect
285368 build {
286369 verbosity: <- Arg . Opt . count { short: " v" },
0 commit comments