mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Pass cli arguments to main required by cli platform
This commit is contained in:
parent
6e64bfc513
commit
ec628ed5c6
6 changed files with 15 additions and 12 deletions
|
@ -1,9 +1,9 @@
|
||||||
platform "cli"
|
platform "cli"
|
||||||
requires {} { main : Task {} [] * }
|
requires {} { main : List Str -> Task {} [] * }
|
||||||
exposes []
|
exposes []
|
||||||
packages {}
|
packages {}
|
||||||
imports [Task.{ Task }, InternalTask, Effect.{ Effect }]
|
imports [Task.{ Task }, InternalTask, Effect.{ Effect }]
|
||||||
provides [mainForHost]
|
provides [mainForHost]
|
||||||
|
|
||||||
mainForHost : Effect (Result {} []) as Fx
|
mainForHost : List Str -> (Effect (Result {} []) as Fx)
|
||||||
mainForHost = InternalTask.toEffect main
|
mainForHost = \args -> InternalTask.toEffect (main args)
|
||||||
|
|
|
@ -20,7 +20,7 @@ use file_glue::WriteErr;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[link_name = "roc__mainForHost_1_exposed_generic"]
|
#[link_name = "roc__mainForHost_1_exposed_generic"]
|
||||||
fn roc_main(output: *mut u8);
|
fn roc_main(output: *mut u8, args: RocList<RocStr>);
|
||||||
|
|
||||||
#[link_name = "roc__mainForHost_size"]
|
#[link_name = "roc__mainForHost_size"]
|
||||||
fn roc_main_size() -> i64;
|
fn roc_main_size() -> i64;
|
||||||
|
@ -84,11 +84,14 @@ pub extern "C" fn rust_main() -> i32 {
|
||||||
let size = unsafe { roc_main_size() } as usize;
|
let size = unsafe { roc_main_size() } as usize;
|
||||||
let layout = Layout::array::<u8>(size).unwrap();
|
let layout = Layout::array::<u8>(size).unwrap();
|
||||||
|
|
||||||
|
// TODO: can we be more efficient about reusing the String's memory for RocStr?
|
||||||
|
let args: RocList<RocStr> = std::env::args().map(|s| RocStr::from(s.as_str())).collect();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// TODO allocate on the stack if it's under a certain size
|
// TODO allocate on the stack if it's under a certain size
|
||||||
let buffer = std::alloc::alloc(layout);
|
let buffer = std::alloc::alloc(layout);
|
||||||
|
|
||||||
roc_main(buffer);
|
roc_main(buffer, args);
|
||||||
|
|
||||||
let result = call_the_closure(buffer);
|
let result = call_the_closure(buffer);
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ app "countdown"
|
||||||
imports [pf.Stdin, pf.Stdout, pf.Task.{ await, loop, succeed }]
|
imports [pf.Stdin, pf.Stdout, pf.Task.{ await, loop, succeed }]
|
||||||
provides [main] to pf
|
provides [main] to pf
|
||||||
|
|
||||||
main =
|
main = \_args ->
|
||||||
_ <- await (Stdout.line "\nLet's count down from 10 together - all you have to do is press <ENTER>.")
|
_ <- await (Stdout.line "\nLet's count down from 10 together - all you have to do is press <ENTER>.")
|
||||||
_ <- await Stdin.line
|
_ <- await Stdin.line
|
||||||
loop 10 tick
|
loop 10 tick
|
||||||
|
|
|
@ -3,8 +3,8 @@ app "echo"
|
||||||
imports [pf.Stdin, pf.Stdout, pf.Task]
|
imports [pf.Stdin, pf.Stdout, pf.Task]
|
||||||
provides [main] to pf
|
provides [main] to pf
|
||||||
|
|
||||||
main : Task.Task {} [] [Read [Stdin], Write [Stdout]]
|
main : List Str -> Task.Task {} [] [Read [Stdin], Write [Stdout]]
|
||||||
main =
|
main = \_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)
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ app "form"
|
||||||
imports [pf.Stdin, pf.Stdout, pf.Task.{ await, Task }]
|
imports [pf.Stdin, pf.Stdout, pf.Task.{ await, Task }]
|
||||||
provides [main] to pf
|
provides [main] to pf
|
||||||
|
|
||||||
main : Task {} * [Read [Stdin], Write [Stdout]]
|
main : List Str -> Task {} * [Read [Stdin], Write [Stdout]]
|
||||||
main =
|
main = \_args ->
|
||||||
_ <- 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?")
|
||||||
|
|
|
@ -3,8 +3,8 @@ app "http-get"
|
||||||
imports [pf.Http, pf.Task, pf.Stdin, pf.Stdout]
|
imports [pf.Http, pf.Task, pf.Stdin, pf.Stdout]
|
||||||
provides [main] to pf
|
provides [main] to pf
|
||||||
|
|
||||||
main : Task.Task {} [] [Read [Stdin], Write [Stdout], Network [Http]]
|
main : List Str -> Task.Task {} [] [Read [Stdin], Write [Stdout], Network [Http]]
|
||||||
main =
|
main = \_args ->
|
||||||
_ <- Task.await (Stdout.line "Please enter a URL to fetch")
|
_ <- Task.await (Stdout.line "Please enter a URL to fetch")
|
||||||
|
|
||||||
url <- Task.await Stdin.line
|
url <- Task.await Stdin.line
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue