CLI Args
Shows how to read command line arguments. To pass an argument:
roc main.roc some_argument # or roc -- some_argument # or roc build && ./main some_argument
We also have a more complex example that uses a filename as argument.
Code
# Run with `roc ./examples/CommandLineArgs/main.roc some_argument` app [main!] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br", } import cli.Stdout import cli.Arg exposing [Arg] main! : List Arg => Result {} _ main! = |raw_args| args = List.map(raw_args, Arg.display) # get the second argument, the first is the executable's path arg_result = List.get(args, 1) |> Result.map_err(ZeroArgsGiven) when arg_result is Err(ZeroArgsGiven(_)) -> Err(Exit(1, "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`")) Ok(first_argument) -> Stdout.line!("received argument: ${first_argument}")
Output
Run this from the directory that has main.roc
in it:
$ roc main.roc some_argument received argument: some_argument