Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to use purity inference #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ from another CLI parsing library that you think would fit well in Weaver's nest.
## Example

```roc
app [main] {
app [main!] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
weaver: "https://github.com/smores56/weaver/releases/download/0.3.1/CZWzZ3WIfkG5_rxdcwPQ0PqgrlZQFwKQUi2zyMYddXc.tar.br",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import pf.Stdout
import weaver.Opt
import weaver.Cli
import weaver.Param

main =
args = Arg.list!
main! = \{} ->
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand All @@ -44,7 +43,7 @@ main =
Err message ->
Stdout.line! message

Task.err (Exit 1 "")
Err (Exit 1 "")

cliParser =
{ Cli.weave <-
Expand Down
13 changes: 6 additions & 7 deletions examples/basic.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
app [main!] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import pf.Stdout
import weaver.Opt
import weaver.Cli
import weaver.Param

main =
args = Arg.list!
main! = \{} ->
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand All @@ -22,7 +21,7 @@ main =
Err message ->
Stdout.line! message

Task.err (Exit 1 "")
Err (Exit 1 "")

cliParser =
{ Cli.weave <-
Expand Down
13 changes: 6 additions & 7 deletions examples/default-values.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
app [main!] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import pf.Stdout
import weaver.Opt
import weaver.Cli
import weaver.Param

main =
args = Arg.list!
main! = \{} ->
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand All @@ -22,7 +21,7 @@ main =
Err message ->
Stdout.line! message

Task.err (Exit 1 "")
Err (Exit 1 "")

cliParser =
{ Cli.weave <-
Expand Down
13 changes: 6 additions & 7 deletions examples/single-arg.roc
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
app [main!] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import pf.Stdout
import weaver.Opt
import weaver.Cli

main =
args = Arg.list!
main! = \{} ->
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand All @@ -21,7 +20,7 @@ main =
Err message ->
Stdout.line! message

Task.err (Exit 1 "")
Err (Exit 1 "")

cliParser =
Opt.u64 { short: "a", long: "alpha", help: "Set the alpha level." }
Expand Down
13 changes: 6 additions & 7 deletions examples/subcommands.roc
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
app [main!] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import pf.Stdout
import weaver.Opt
import weaver.Cli
import weaver.Param
import weaver.SubCmd

main =
args = Arg.list!
main! = \{} ->
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand All @@ -23,7 +22,7 @@ main =
Err message ->
Stdout.line! message

Task.err (Exit 1 "")
Err (Exit 1 "")

cliParser =
{ Cli.weave <-
Expand Down
15 changes: 5 additions & 10 deletions package/Opt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ import Parser exposing [ArgValue]
builderWithOptionParser : OptionConfig, (List ArgValue -> Result data ArgExtractErr) -> CliBuilder data fromAction toAction
builderWithOptionParser = \option, valueParser ->
argParser = \args ->
{ values, remainingArgs } <- extractOptionValues { args, option }
|> Result.try
data <- valueParser values
|> Result.try
{ values, remainingArgs } = try extractOptionValues { args, option }
data = try valueParser values

Ok { data, remainingArgs }

Expand Down Expand Up @@ -124,8 +122,7 @@ single = \{ parser, type, short ? "", long ? "", help ? "", default ? NoDefault
NoDefault -> Err (MissingOption option)

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try
value = try getMaybeValue values option

when value is
Err NoValue -> defaultGenerator {}
Expand Down Expand Up @@ -172,8 +169,7 @@ maybe = \{ parser, type, short ? "", long ? "", help ? "" } ->
option = { expectedValue: ExpectsValue type, plurality: Optional, short, long, help }

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try
value = try getMaybeValue values option

when value is
Err NoValue -> Ok (Err NoValue)
Expand Down Expand Up @@ -250,8 +246,7 @@ flag = \{ short ? "", long ? "", help ? "" } ->
option = { expectedValue: NothingExpected, plurality: Optional, short, long, help }

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try
value = try getMaybeValue values option

when value is
Err NoValue -> Ok Bool.false
Expand Down
6 changes: 2 additions & 4 deletions package/Param.roc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ import Extract exposing [extractParamValues]
builderWithParameterParser : ParameterConfig, (List Str -> Result data ArgExtractErr) -> CliBuilder data fromAction toAction
builderWithParameterParser = \param, valueParser ->
argParser = \args ->
{ values, remainingArgs } <- extractParamValues { args, param }
|> Result.try
data <- valueParser values
|> Result.try
{ values, remainingArgs } = try extractParamValues { args, param }
data = try valueParser values

Ok { data, remainingArgs }

Expand Down
93 changes: 41 additions & 52 deletions package/Validate.roc
Original file line number Diff line number Diff line change
Expand Up @@ -70,58 +70,47 @@ validateCommand :
}
-> Result {} CliValidationErr
validateCommand = \{ name, options, parentOptions, parameters, subcommands, subcommandPath } ->
{} <- ensureCommandIsWellNamed { name, subcommandPath }
|> Result.try
_ <- options
|> List.mapTry \option ->
{} <- ensureOptionIsWellNamed { option, subcommandPath }
|> Result.try
{} <- ensureOptionValueTypeIsWellNamed { option, subcommandPath }
|> Result.try

Ok {}
|> Result.try

_ <- parameters
|> List.mapTry \param ->
{} <- ensureParamIsWellNamed { name: param.name, subcommandPath }
|> Result.try
{} <- ensureParamValueTypeIsWellNamed { param, subcommandPath }
|> Result.try

Ok {}
|> Result.try

{} <- checkIfThereAreOverlappingParameters parameters subcommandPath
|> Result.try

when subcommands is
HasSubcommands subcommandConfigs if !(Dict.isEmpty subcommandConfigs) ->
subcommandConfigs
|> Dict.toList
|> List.mapTry \(subcommandName, subcommand) ->
updatedParentOptions =
options
|> List.map \option -> { option, subcommandPath }
|> List.concat parentOptions

validateCommand {
name: subcommandName,
options: subcommand.options,
parentOptions: updatedParentOptions,
parameters: subcommand.parameters,
subcommands: subcommand.subcommands,
subcommandPath: subcommandPath |> List.append subcommandName,
}
|> Result.map \_successes -> {}

_noSubcommands ->
allOptionsToCheck =
options
|> List.map \option -> { option, subcommandPath }
|> List.concat parentOptions

checkIfThereAreOverlappingOptions allOptionsToCheck
ensureCommandIsWellNamed { name, subcommandPath }
|> Result.try \{} ->
List.mapTry options \option ->
ensureOptionIsWellNamed { option, subcommandPath }
|> Result.try \{} ->
ensureOptionValueTypeIsWellNamed { option, subcommandPath }
|> Result.try \_ ->
List.mapTry parameters \param ->
ensureParamIsWellNamed { name: param.name, subcommandPath }
|> Result.try \{} ->
ensureParamValueTypeIsWellNamed { param, subcommandPath }
|> Result.try \_ ->
checkIfThereAreOverlappingParameters parameters subcommandPath
|> Result.try \{} ->
when subcommands is
HasSubcommands subcommandConfigs if !(Dict.isEmpty subcommandConfigs) ->
subcommandConfigs
|> Dict.toList
|> List.mapTry \(subcommandName, subcommand) ->
updatedParentOptions =
options
|> List.map \option -> { option, subcommandPath }
|> List.concat parentOptions

validateCommand {
name: subcommandName,
options: subcommand.options,
parentOptions: updatedParentOptions,
parameters: subcommand.parameters,
subcommands: subcommand.subcommands,
subcommandPath: subcommandPath |> List.append subcommandName,
}
|> Result.map \_successes -> {}

_noSubcommands ->
allOptionsToCheck =
options
|> List.map \option -> { option, subcommandPath }
|> List.concat parentOptions

checkIfThereAreOverlappingOptions allOptionsToCheck

ensureCommandIsWellNamed : { name : Str, subcommandPath : List Str } -> Result {} CliValidationErr
ensureCommandIsWellNamed = \{ name, subcommandPath } ->
Expand Down
Loading