Update CLI examples and tests to use Program

This commit is contained in:
Richard Feldman 2022-09-19 20:01:59 -04:00
parent 0e472200f0
commit aa6d29fbb1
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
5 changed files with 69 additions and 26 deletions

View file

@ -491,7 +491,7 @@ mod cli_run {
// expected_ending: "[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]\n", // expected_ending: "[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]\n",
// use_valgrind: true, // use_valgrind: true,
// }, // },
args:"interactive" => Example { cli_args:"interactive" => Example {
filename: "args.roc", filename: "args.roc",
executable_filename: "args", executable_filename: "args",
stdin: &[], stdin: &[],
@ -507,7 +507,7 @@ mod cli_run {
expected_ending: "hi there!\nIt is known\n", expected_ending: "hi there!\nIt is known\n",
use_valgrind: true, use_valgrind: true,
}, },
// tea:"tea" => Example { // tui_tea:"tea" => Example {
// filename: "Main.roc", // filename: "Main.roc",
// executable_filename: "tea-example", // executable_filename: "tea-example",
// stdin: &[], // stdin: &[],
@ -515,7 +515,7 @@ mod cli_run {
// expected_ending: "", // expected_ending: "",
// use_valgrind: true, // use_valgrind: true,
// }, // },
cli:"interactive" => Example { cli_form:"interactive" => Example {
filename: "form.roc", filename: "form.roc",
executable_filename: "form", executable_filename: "form",
stdin: &["Giovanni\n", "Giorgio\n"], stdin: &["Giovanni\n", "Giorgio\n"],
@ -967,23 +967,50 @@ mod cli_run {
&[], &[],
indoc!( indoc!(
r#" r#"
UNRECOGNIZED NAME tests/known_bad/TypeError.roc TYPE MISMATCH ...d/../../../../examples/interactive/cli-platform/main.roc
Nothing is named `d` in this scope. Something is off with the type annotation of the main required symbol:
10 _ <- await (line d) 2 requires {} { main : InternalProgram }
^ ^^^^^^^^^^^^^^^
Did you mean one of these? This #UserApp.main value is a:
U8 Task.Task {} * [Write [Stdout]*]* ?
Ok
I8 But the type annotation on main says it should be:
F64
InternalProgram.InternalProgram ?
Tip: Type comparisons between an opaque type are only ever equal if
both types are the same opaque type. Did you mean to create an opaque
type by wrapping it? If I have an opaque type Age := U32 I can create
an instance of this opaque type by doing @Age 23.
TYPE MISMATCH ...d/../../../../examples/interactive/cli-platform/main.roc
This 1st argument to toEffect has an unexpected type:
9 mainForHost = InternalProgram.toEffect main
^^^^
This #UserApp.main value is a:
Task.Task {} * [Write [Stdout]*]* ?
But toEffect needs its 1st argument to be:
InternalProgram.InternalProgram ?
Tip: Type comparisons between an opaque type are only ever equal if
both types are the same opaque type. Did you mean to create an opaque
type by wrapping it? If I have an opaque type Age := U32 I can create
an instance of this opaque type by doing @Age 23.
1 error and 0 warnings found in <ignored for test> ms."# 2 errors and 1 warning found in <ignored for test> ms."#
), ),
); );
} }

View file

@ -1,11 +1,13 @@
app "type-error" app "type-error"
packages { pf: "../../../../examples/interactive/cli-platform/main.roc" } packages { pf: "../../../../examples/interactive/cli-platform/main.roc" }
imports [pf.Stdout.{ line }, pf.Task.{ await }] imports [pf.Stdout.{ line }, pf.Task.{ await }, pf.Program]
provides [main] to pf provides [main] to pf
main = \_args -> main =
_ <- await (line "a") _ <- await (line "a")
_ <- await (line "b") _ <- await (line "b")
_ <- await (line "c") _ <- await (line "c")
_ <- await (line d) _ <- await (line "d")
line "e" line "e"
# Type mismatch because this line is missing:
# |> Program.quick

View file

@ -1,10 +1,10 @@
app "args" app "args"
packages { pf: "cli-platform/main.roc" } packages { pf: "cli-platform/main.roc" }
imports [pf.Stdout, pf.Task, pf.Arg] imports [pf.Stdout, pf.Arg, pf.Program.{ Program }]
provides [main] to pf provides [main] to pf
main : List Str -> Task.Task {} [] [Write [Stdout]] main : Program
main = \args -> main = Program.withArgs \args ->
parser = parser =
divCmd = divCmd =
Arg.succeed (\dividend -> \divisor -> Div (Num.toF64 dividend) (Num.toF64 divisor)) Arg.succeed (\dividend -> \divisor -> Div (Num.toF64 dividend) (Num.toF64 divisor))
@ -49,9 +49,15 @@ main = \args ->
|> Arg.program { name: "args-example", help: "A calculator example of the CLI platform argument parser" } |> Arg.program { name: "args-example", help: "A calculator example of the CLI platform argument parser" }
when Arg.parseFormatted parser args is when Arg.parseFormatted parser args is
Ok cmd -> runCmd cmd |> Num.toStr |> Stdout.line Ok cmd ->
runCmd cmd
|> Num.toStr
|> Stdout.line
|> Program.exit 0
Err helpMenu -> Err helpMenu ->
Stdout.line helpMenu Stdout.line helpMenu
|> Program.exit 1
runCmd = \cmd -> runCmd = \cmd ->
when cmd is when cmd is

View file

@ -1,12 +1,16 @@
app "echo" app "echo"
packages { pf: "cli-platform/main.roc" } packages { pf: "cli-platform/main.roc" }
imports [pf.Stdin, pf.Stdout, pf.Task] imports [pf.Stdin, pf.Stdout, pf.Task.{ Task }, pf.Program.{ Program, ExitCode }]
provides [main] to pf provides [main] to pf
main : List Str -> Task.Task {} [] [Read [Stdin], Write [Stdout]] main : Program
main = \_args -> main = Program.noArgs mainTask
mainTask : List Str -> Task ExitCode [] [Read [Stdin], Write [Stdout]]
mainTask = \_args ->
_ <- Task.await (Stdout.line "🗣 Shout into this cave and hear the echo! 👂👂👂") _ <- Task.await (Stdout.line "🗣 Shout into this cave and hear the echo! 👂👂👂")
Task.loop {} (\_ -> Task.map tick Step) Task.loop {} (\_ -> Task.map tick Step)
|> Program.exit 0
tick : Task.Task {} [] [Read [Stdin]*, Write [Stdout]*]* tick : Task.Task {} [] [Read [Stdin]*, Write [Stdout]*]*
tick = tick =

View file

@ -1,12 +1,16 @@
app "form" app "form"
packages { pf: "cli-platform/main.roc" } packages { pf: "cli-platform/main.roc" }
imports [pf.Stdin, pf.Stdout, pf.Task.{ await, Task }] imports [pf.Stdin, pf.Stdout, pf.Task.{ await, Task }, pf.Program.{ Program, ExitCode }]
provides [main] to pf provides [main] to pf
main : List Str -> Task {} * [Read [Stdin], Write [Stdout]] main : Program
main = \_args -> main = Program.noArgs mainTask
mainTask : Task ExitCode [] [Read [Stdin], Write [Stdout]]
mainTask =
_ <- await (Stdout.line "What's your first name?") _ <- await (Stdout.line "What's your first name?")
firstName <- await Stdin.line firstName <- await Stdin.line
_ <- await (Stdout.line "What's your last name?") _ <- await (Stdout.line "What's your last name?")
lastName <- await Stdin.line lastName <- await Stdin.line
Stdout.line "Hi, \(firstName) \(lastName)! 👋" Stdout.line "Hi, \(firstName) \(lastName)! 👋"
|> Program.exit 0