Skip to content

File paths and ending a program

A relative path in quotes starts at the folder of the file you write it in. So a program reads the same file from a terminal and from a server. exit ends a program before its last line.

File paths

A relative file path written in quotes starts at the folder of the file that contains it.

Core\IO::read("data/prices.txt") reads the data folder next to your program. It reads the same file when you start the program from another folder, under nvs serve and as a service. A full path, such as /srv/shop/prices.txt, is used as you wrote it. Add #[Core\Path] to a string parameter of your own method, and a path in quotes passed to it works the same way.

Good to know: this works only for a path written in quotes in the call itself. A path such as 'data/' . $name starts with a relative path, so it does not compile. Start it with the folder of your file: Core\Path::thisDirectory('data') . '/' . $name. Core\Path::join also adds a name to a full folder path. A relative path in a variable throws a RuntimeError when a file method gets it.

The examples below read a file next to the program, pass a path to a method of your own, and build a path from a name that is only known while the program runs.

Read a file next to the program

<?nvs
// Shows how to read a file that is stored next to the program.
// The path starts at the folder of this file. `nvs run` gives the same result
// from any folder. This prints the two lines of `data/prices.txt`.
echo Core\IO::read('data/prices.txt');
Output
coffee 3.50
tea 2.80

Your own method takes a path

<?nvs
// Shows how to make a parameter of your own method a file path.
class Mail
{
// `#[Core\Path]` says that `$template` is a file path. A path in quotes
// passed here starts at the folder of the file that calls the method.
public static function render(#[Core\Path] string $template, string $name): string
{
return Core\Str::replace(Core\IO::read($template), '{name}', $name);
}
}
// This prints "Hello, Ada! Your order is on its way.".
echo Mail::render('templates/welcome.txt', 'Ada');
Output
Hello, Ada! Your order is on its way.

A file name known only at run time

<?nvs
// Shows how to build a path for a file name that the program only knows while
// it runs.
string $name = 'prices.txt';
// `Core\Path::thisDirectory('data')` returns the full path of the `data` folder next
// to this file. The name is added to it, so this read works.
echo Core\IO::read(Core\Path::thisDirectory('data') . '/' . $name);
// `Core\Path::join` adds the name to a full folder path. This prints the same file.
string $folder = Core\Path::thisDirectory('data');
echo Core\IO::read(Core\Path::join($folder, $name));
Output
coffee 3.50
tea 2.80
coffee 3.50
tea 2.80

Details

Every Core parameter that names a file works this way. Examples are the path methods of Core\IO, Core\Process::run, Core\Response::sendFile, the script of spawn script, Core\Socket::upgrade and Core\Sse::upgrade. The methods of Core\Path work on text, and their parameters are not file paths.

#[Core\Path] is allowed on a string or ?string parameter. On a parameter of any other type, the program does not compile (E0836). A default value of such a parameter, such as string $path = 'rates.json', starts at the folder of the file that declares the method.

A class constant is not joined to the folder, just like a variable. If it is a relative path, the file method throws a RuntimeError. The error for 'data/' . $name is E0840, and its message shows how to start the path with the folder of the file.

A program started with nvs run may read any file. A web request may read the files in its app folder, and nvs.toml can allow less. Core\Path::fromWorkingDirectory joins a path to the folder the program was started from. Use it for a path typed on the command line. It throws an error while the program handles a web request.

Ending a program

A program ends when its last line has run. Its exit status is then 0, which tells the shell that all went well.

exit ends the program early. exit; ends it with status 0. exit(3); ends it with status 3, and the shell can read that number. A status is a number from 0 to 255. A program that checks or imports something can use it to report how many problems it found. exit("Nothing to do."); prints the text and ends with status 0.

Good to know: exit is not an error. A catch does not stop it, and a finally block does not run. die does not exist in Novis. If an error is thrown and nothing catches it, the program ends with status 1 and prints where the error came from. The last example shows that a line after exit does not run.

Stop early when there is nothing to do

<?nvs
// A program does not have to run to its last line. This one prints today's
// orders; when there are none, there is nothing left to do, so it says so
// and stops right there. On its own, `exit` ends the program as a success.
array<string> $orders = [];
echo "Nightly order report\n";
if (Core\Arr::count($orders) == 0) {
echo "No orders came in today.\n";
exit;
}
foreach ($orders as string $order) {
echo '- ', $order, "\n";
}
echo "That is every order.\n";
Output
Nightly order report
No orders came in today.

Say why you are stopping

<?nvs
// `exit` can be handed a line of text. The text is written out first, and
// then the program ends as a success. It is the short way to stop and tell
// whoever reads the output why you stopped.
string $importFolder = '';
echo "Customer import\n";
if ($importFolder == '') {
exit("No import folder was set, so there is nothing to read.\n");
}
echo 'Reading from ', $importFolder, "\n";
Output
Customer import
No import folder was set, so there is nothing to read.

Tell the shell whether the check passed

<?nvs
// The number you pass to `exit` is the exit status that the shell reads.
// This program checks some addresses and exits with the number of problems.
// Every address below has an `@`, so the exit status is `0`.
array<string> $addresses = ['ada@example.com', 'grace@example.com'];
int $problems = 0;
foreach ($addresses as string $address) {
if (Core\Str::contains($address, '@')) {
echo 'ok ', $address, "\n";
} else {
echo 'bad ', $address, "\n";
$problems = $problems + 1;
}
}
echo 'checked ', Core\Arr::count($addresses), ', problems ', $problems, "\n";
exit($problems);
Output
ok ada@example.com
ok grace@example.com
checked 2, problems 0

A line after exit does not run

<?nvs
// Shows that `exit(3)` ends the program with exit status 3.
// The last line does not run.
// proof: exit 3
echo 'stopping', "\n";
exit(3);
echo 'not reached', "\n";
Output
stopping

Details

The status is one byte. A number outside 0 to 255 keeps only its lowest eight bits, so exit(300) ends with status 44 and exit(-1) ends with status 255. This is the same on every system.

exit("bye") prints bye to standard output and adds no newline. Put "\n" at the end of the text if you want one.

die does not compile (E0228). The message names exit, which takes the same argument.

A thrown error that nothing catches ends the program with status 1. The error and the lines it passed through are printed to standard error.

  • Do write a file path in quotes, relative to the file that uses it.
  • Do use Core\Path::join with Core\Path::thisDirectory() for a path you build from parts.
  • Do use exit(1) or another status that is not 0 when a command-line program fails.
  • Don’t use Core\Path::fromWorkingDirectory in code that handles web requests.
  • Don’t write die. It does not compile.