Skip to content
Merged
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
7 changes: 5 additions & 2 deletions examples/command-line-args.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.23.0-rc1/3hT3SoHZ6qbEsa9qVFLUW3547U5LeoNd1KbpqLpz4r1i.tar.zst" }

import pf.OsStr
import pf.Env
import pf.Stdout
import pf.Stderr

main! : List(OsStr) => Try({}, _)
main! = |args| {
# Skip first arg (executable path), get the remaining args
match args.drop_first(1) {
program_name = Env.program_name!()?
Stdout.line!("program name: ${OsStr.display(program_name)}")?

match args {
[first_arg, ..] => {

Stdout.line!("received argument: ${OsStr.display(first_arg)}")?
Expand Down
2 changes: 1 addition & 1 deletion examples/file-accessed-modified-created-time.roc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ main! = |args| {
## Parse the first argument into a Path
path_argument : List(OsStr) -> Try(Path, _)
path_argument = |args|
match args.drop_first(1) {
match args {
[first, ..] => Ok(Path.from_os_str(first))
[] => Err(MissingPathArgument)
}
2 changes: 1 addition & 1 deletion examples/file-permissions.roc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ main! = |args| {
## Parse the first argument into a Path
path_argument : List(OsStr) -> Try(Path, _)
path_argument = |args|
match args.drop_first(1) {
match args {
[first, ..] => Ok(Path.from_os_str(first))
[] => Err(MissingPathArgument)
}
2 changes: 1 addition & 1 deletion examples/file-size.roc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main! = |args| {

path_argument : List(OsStr) -> Try(Path, [MissingPathArgument, ..])
path_argument = |args|
match args.drop_first(1) {
match args {
[first, ..] => Ok(Path.from_os_str(first))
[] => Err(MissingPathArgument)
}
2 changes: 1 addition & 1 deletion examples/filesystem-tools.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pf.Stdout

main! : List(OsStr) => Try({}, _)
main! = |args| {
(source_arg, destination_arg) = match args.drop_first(1) {
(source_arg, destination_arg) = match args {
[source, destination] => (source, destination)
_ => return Err(MissingPaths)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ main! = |args| {

greeting_name : List(OsStr) -> Str
greeting_name = |args|
match args.drop_first(1) {
match args {
[first, ..] => OsStr.display(first)
[] => "friend"
}
2 changes: 1 addition & 1 deletion examples/path.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ main! = |args| {
}

path_argument = |args|
match args.drop_first(1) {
match args {
[first, ..] => Ok(Path.from_os_str(first))
[] => Err(MissingPathArgument)
}
2 changes: 1 addition & 1 deletion examples/process-control.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pf.Stdout

main! : List(OsStr) => Try({}, _)
main! = |args| {
(program, arguments) = match args.drop_first(1) {
(program, arguments) = match args {
[command, .. as rest] => (command, rest)
[] => return Err(MissingCommand)
}
Expand Down
13 changes: 13 additions & 0 deletions platform/Env.roc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ Env :: [].{
Err(ExePathUnavailable) => Err(ExePathUnavailable)
}

## Gets the program name supplied by the process launcher as its first argument.
##
## This is conventionally the executable name or path, but launchers may supply
## another value. Unlike [`exe_path!`](#exe_path!), it is returned as an
## [`OsStr`](OsStr), preserving the value exactly without treating it as a path.
## Returns `Err(ProgramNameUnavailable)` if the launcher supplied no first argument.
program_name! : () => Try(OsStr, [ProgramNameUnavailable, ..])
program_name! = ||
match Host.env_program_name!() {
Ok(raw) => Ok(OsStr.from_raw(raw))
Err(ProgramNameUnavailable) => Err(ProgramNameUnavailable)
}

## Atomically create a private directory in the system temporary directory.
## The caller owns cleanup. Unix directories have mode 0700.
create_temp_dir! : () => Try(Path.Path, [TempDirErr(IOErr), ..])
Expand Down
2 changes: 2 additions & 0 deletions platform/Host.roc
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@ Host :: [].{
tcp_local_port! : TcpListener => Try(U16, Str)
tcp_accept! : TcpListener, U64 => Try(TcpStream, Str)
tcp_listener_close! : TcpListener => Try({}, Str)

env_program_name! : () => Try(NativeOsStr, [ProgramNameUnavailable])
}
5 changes: 5 additions & 0 deletions platform/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
## SQLite, environment, random, and UTC effects.
platform ""
requires {
## The application entry point receives command-line arguments only; the
## launcher-supplied program name is available from Env.program_name!.
main! : List([Utf8(Str), UnixBytes(List(U8)), WindowsU16s(List(U16))]) => Try({}, [Exit(I32), ..])
}
exposes [Cmd, Env, File, Http, IOErr, Locale, Monotonic, OsStr, Path, Random, Sleep, Sqlite, Stdin, Stdout, Stderr, Tcp, Tty, Url, Utc]
Expand Down Expand Up @@ -100,6 +102,7 @@ platform ""
"hosted_tcp_local_port": Host.tcp_local_port!,
"hosted_tcp_accept": Host.tcp_accept!,
"hosted_tcp_listener_close": Host.tcp_listener_close!,
"hosted_env_program_name": Host.env_program_name!,
}
targets: {
inputs_dir: "targets/",
Expand Down Expand Up @@ -132,6 +135,8 @@ import Tty
import Url
import Utc

## The native host removes the invocation name (argv[0]) before calling this
## function. Applications can read that value separately with Env.program_name!.
main_for_host! : List(OsStr.OsStr) => I32
main_for_host! = |args|
match main!(args) {
Expand Down
2 changes: 2 additions & 0 deletions scripts/test_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
"name": "one-argument",
"args": ["foo"],
"contains": [
"program name:",
"received argument: foo",
"back to OsStr: OsStr.",
"argument 1 is valid UTF-8: foo"
],
"regex": [
"program name: .*command-line-args(?:\\.exe)?",
"(?:Unix argument, bytes|Windows argument, UTF-16 code units): \\[102, 111, 111\\]"
]
},
Expand Down
Loading
Loading