Skip to content

Commands and finding classes

Core\Program::implementing<I>() returns one new object of every class that implements I. You do not need to list the classes yourself. The compiler finds them while it compiles.

Every class that implements an interface

Core\Program::implementing<I>() gives you one object for every class in your program that implements the interface I. Novis works that list out while it compiles your program, so nothing is registered while it runs and no list of classes is written anywhere. A class joins the list by implementing the interface.

The array is sorted by the full class name. The order is the same on every machine and never depends on the file system. An abstract class is not in the list, because new cannot create one. Every class in the list needs a constructor that takes no arguments, and what a class needs arrives through the interface's own methods instead. An interface that no class implements gives an empty array.

The objects are created where the call is, each time it runs, like any other new. Two calls give you two separate sets of objects.

The examples show a list of checks, which classes end up in the list, and one table written in every format a program knows. The last example is a class whose constructor needs an argument, which does not compile.

Every class that implements an interface

<?nvs
// Shows how to reach every class that implements an interface.
// `Core\Program::implementing` gives you one new object per class. Novis finds
// the classes while it compiles your program, so a new class joins the list by
// existing.
interface Check
{
public function describe(): string;
}
class DiskSpace implements Check
{
public function describe(): string
{
return 'disk space is fine';
}
}
class Certificates implements Check
{
public function describe(): string
{
return 'certificates are valid';
}
}
class Backups implements Check
{
public function describe(): string
{
return 'backups ran last night';
}
}
// The list is sorted by class name, so `Backups` comes first. The order never
// depends on the order the classes are written in.
foreach (Core\Program::implementing<Check>() as Check $check) {
echo $check->describe(), "\n";
}
Output
backups ran last night
certificates are valid
disk space is fine

Which classes the list contains

<?nvs
// Shows which classes end up in the list. A class is in it when it implements
// the interface and `new` needs no arguments for it. An interface no class
// implements gives you an empty array.
interface Check
{
public function name(): string;
}
// An `abstract` class cannot be created with `new`, so it is not in the list.
abstract class BaseCheck implements Check
{
public function name(): string
{
return 'base';
}
}
// This one extends the abstract class, so it implements `Check` as well.
class DiskCheck extends BaseCheck
{
public function name(): string
{
return 'disk';
}
}
class MailCheck implements Check
{
public function name(): string
{
return 'mail';
}
}
// This class has the same method and implements nothing, so it stays out.
class Report
{
public function name(): string
{
return 'report';
}
}
interface Rule
{
public function apply(): string;
}
// This prints `disk` and then `mail`.
foreach (Core\Program::implementing<Check>() as Check $check) {
echo $check->name(), "\n";
}
// No class implements `Rule`, so this prints 0.
echo Core\Arr::count(Core\Program::implementing<Rule>()), "\n";
Output
disk
mail
0

One table in every format

<?nvs
// Writing one table in every format a program knows. Adding a format means
// adding a class. There is no list of formats to keep up to date, and nothing
// has to be registered.
interface Exporter
{
public function format(): string;
public function render(array<string> $rows): string;
}
class CsvExporter implements Exporter
{
public function format(): string
{
return 'csv';
}
public function render(array<string> $rows): string
{
string $out = '';
foreach ($rows as string $row) {
$out = $out == '' ? $row : $out . ',' . $row;
}
return $out;
}
}
class JsonExporter implements Exporter
{
public function format(): string
{
return 'json';
}
public function render(array<string> $rows): string
{
return Core\Json::encode($rows);
}
}
class TextExporter implements Exporter
{
public function format(): string
{
return 'text';
}
public function render(array<string> $rows): string
{
string $out = '';
foreach ($rows as string $row) {
$out = $out . '- ' . $row . ' ';
}
return Core\Str::trim($out);
}
}
array<string> $rows = ['cup', 'plate', 'bowl'];
// One line per format, in alphabetical order by class name.
foreach (Core\Program::implementing<Exporter>() as Exporter $exporter) {
echo $exporter->format(), ': ', $exporter->render($rows), "\n";
}
Output
csv: cup,plate,bowl
json: ["cup","plate","bowl"]
text: - cup - plate - bowl

A constructor that needs an argument

<?nvs
// Shows that every class in the list needs a constructor without arguments.
// `SmtpSender` needs a host name, so this program does not compile.
// proof: compile-error
interface Sender
{
public function send(string $text): string;
}
class LogSender implements Sender
{
public function send(string $text): string
{
return 'logged: ' . $text;
}
}
class SmtpSender implements Sender
{
public function constructor(private string $host)
{}
public function send(string $text): string
{
return 'sent to ' . $this->host . ': ' . $text;
}
}
foreach (Core\Program::implementing<Sender>() as Sender $sender) {
echo $sender->send('hello'), "\n";
}
Output
error[E0744]: `SmtpSender` is a `Sender` and its constructor takes 1 required argument(s)
--> 04-a-constructor-that-needs-an-argument.nvs:30:10
|
30 | foreach (Core\Program::implementing<Sender>() as Sender $sender) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this enumeration would write `new SmtpSender()`
= help: every class in the result is created with no arguments. Give `SmtpSender` a `constructor` with no required parameters. Or use `Core\Program::constructors<Sender, callable(...): Sender>()`, which passes the arguments you give it
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

I can also be a class. Then the list has every class that extends it, and I itself when it is not abstract. An abstract base class is a good I when the classes share code. A class that implements an interface which extends I is in the list too. The test for each class is the same as $object is I.

I must be an interface or a class that your program declares. An enum, a shape, a scalar, mixed and object are E0743. A built-in interface or class, such as Stringable or RuntimeError, is E0743 as well. A name that nothing declares is E0303.

The call takes exactly one type argument, so implementing() and implementing<I, J>() are E0442. It takes no value argument, so implementing<I>(1) is E0402.

A class whose constructor has a parameter without a default is E0744, and the message names the class. A constructor whose parameters all have defaults is allowed, and each parameter gets its default. To pass arguments to the constructors, use Core\Program::constructors instead.

The result is an array<I>. Storing it in an array<J> of another type is E0401, and var gives the variable the type array<I>.

Each call runs every constructor again. Two calls return two different objects for each class, so == between them is false.

The full class name includes the namespace, so Alpha\Zebra comes before Middle, and Middle before Zed\Apple. The list has the classes of every file in the program. It also has the classes under each autoload folder, even when no code names those classes or their files.

Classes with an attribute

Core\Program::implementingWith<I, T>($member) gives you one row for every class in your program that implements the interface I, with one of that class's attributes beside it.

T is the name of an attribute declaration, such as Menu after attribute Menu on method { … }. Each row has two fields. instance is the object, and attribute is the T attribute on that class's $member. Without one, it is null. $member is the name of a method or a property, or an empty string for the attributes written on the class itself. Rows are sorted by full class name.

Novis finds the classes and their attributes while it compiles your program. A framework uses this to read the settings of the classes it found.

Good to know: a class with two T attributes on one member is an error. Read that class with Core\Attributes::all<T>.

The examples show a report label, class attributes, an admin menu, two attributes with the same field, and a missing member, which does not compile.

Every class with the attribute it declares

<?nvs
// Shows how to read one attribute from every class that implements an
// interface. Each row has the object and the attribute found on it.
attribute Label on method { text: string }
interface Report
{
public function rows(): int;
}
class SalesReport implements Report
{
#[Label(text: 'Sales')]
public function rows(): int
{
return 12;
}
}
class StockReport implements Report
{
#[Label(text: 'Stock')]
public function rows(): int
{
return 7;
}
}
// The rows are sorted by class name, so `SalesReport` comes first.
// This prints `Sales: 12` and then `Stock: 7`.
foreach (Core\Program::implementingWith<Report, Label>('rows') as var $row) {
echo $row->attribute?->text ?? '(none)', ': ', $row->instance->rows(), "\n";
}
Output
Sales: 12
Stock: 7

A class that carries no attribute

<?nvs
// Shows the two things that are not a method attribute: an empty member name,
// which reads the attributes on the class itself, and a class carrying none.
attribute Owner on class { team: string }
interface Job
{
public function run(): string;
}
#[Owner(team: 'billing')]
class Invoices implements Job
{
public function run(): string
{
return 'invoices';
}
}
class Cleanup implements Job
{
public function run(): string
{
return 'cleanup';
}
}
// `Cleanup` has no `#[Owner]`, so its attribute is `null` and `??` supplies
// the text instead. This prints `cleanup: nobody` and then `invoices: billing`.
foreach (Core\Program::implementingWith<Job, Owner>('') as var $row) {
echo $row->instance->run(), ': ', $row->attribute?->team ?? 'nobody', "\n";
}
Output
cleanup: nobody
invoices: billing

Build an admin menu from the pages

<?nvs
// Shows what a framework does with this: every page in the program, beside the
// menu entry that page declares for itself.
attribute Menu on method { title: string, order: int }
interface AdminPage
{
public function path(): string;
}
class Dashboard implements AdminPage
{
#[Menu(title: 'Dashboard', order: 1)]
public function path(): string
{
return '/admin';
}
}
class Users implements AdminPage
{
#[Menu(title: 'Users', order: 2)]
public function path(): string
{
return '/admin/users';
}
}
class Health implements AdminPage
{
// This page is reachable and stays out of the menu, because it declares
// no `#[Menu]`.
public function path(): string
{
return '/admin/health';
}
}
// Adding a page to the menu is adding the attribute to the page, and no list
// of pages can go out of date. This prints `1. Dashboard -> /admin` first.
foreach (Core\Program::implementingWith<AdminPage, Menu>('path') as var $row) {
var $entry = $row->attribute;
if ($entry != null) {
echo $entry->order, '. ', $entry->title, ' -> ', $row->instance->path(), "\n";
}
}
Output
1. Dashboard -> /admin
2. Users -> /admin/users

A class without the member

<?nvs
// Shows that every class must have the member you name.
// `Archive` has no `rows` method, so this program does not compile.
// proof: compile-error
attribute Label on method { text: string }
interface Report
{
}
class Sales implements Report
{
#[Label(text: 'Sales')]
public function rows(): int
{
return 12;
}
}
class Archive implements Report
{
public function count(): int
{
return 3;
}
}
var $rows = Core\Program::implementingWith<Report, Label>('rows');
Output
error[E0798]: `Core\Program::implementingWith` names `rows`, which `Archive` does not declare
--> 04-a-class-without-the-member.nvs:29:59
|
29 | var $rows = Core\Program::implementingWith<Report, Label>('rows');
| ^^^^^^ no method, parameter or property of that name
= help: the name is checked against every class that implements `Report`. Write a method or property that `Report` declares. Write an empty string for the attributes of the class itself
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Each call reads only the attribute it names

<?nvs
// Shows that `implementingWith` reads only the attribute you name. `Menu` and
// `Footer` have the same field, and each call finds only its own attribute.
attribute Menu on method { title: string }
attribute Footer on method { title: string }
interface Page
{
public function render(): string;
}
class Contact implements Page
{
#[Menu(title: 'Contact us')]
public function render(): string
{
return '/contact';
}
}
class Imprint implements Page
{
#[Footer(title: 'Imprint')]
public function render(): string
{
return '/imprint';
}
}
// `Imprint` has no `Menu`, so its `attribute` is `null`.
// This prints `/contact -> Contact us` and then `/imprint -> (not in the menu)`.
foreach (Core\Program::implementingWith<Page, Menu>('render') as var $row) {
echo $row->instance->render(), ' -> ', $row->attribute?->title ?? '(not in the menu)', "\n";
}
// The same pages, read for `Footer`. This time, only `Imprint` has a title.
foreach (Core\Program::implementingWith<Page, Footer>('render') as var $row) {
echo $row->instance->render(), ' -> ', $row->attribute?->title ?? '(not in the footer)', "\n";
}
Output
/contact -> Contact us
/imprint -> (not in the menu)
/contact -> (not in the footer)
/imprint -> Imprint

Details

I follows the same rules as for Core\Program::implementing. The list has the same classes in the same order, and E0743, E0744 and E0442 are given for the same mistakes. The call takes two type arguments.

$member is a string, and an int is E0401. Without an argument, $member is the empty string, so the rows have the attributes on each class itself. A second argument is E0402.

Write $member as a string in quotes. Novis reads it while it compiles. A variable, a class constant or a joined string such as "m" . "m" also compiles, but every row's attribute is then null.

A method name finds the method in each class, also a static or a private one. "constructor" finds the attributes on the constructor. A property is written without the $, as "total". In double quotes, "$total" is a variable in the string, so it is E0301 when no such variable exists. A promoted constructor parameter is a property, and its name gives the attributes written on that parameter. A constructor parameter that is not promoted is not found. To read its attributes, use Core\Attributes::get with Report::constructor and the parameter's name.

A name that one of the classes does not have is E0798, and the message names that class. A method that a class inherits counts as one it has. Its attributes are not read, so that row's attribute is null. A class that overrides a method has only the attributes on its own version. An attribute on the interface's method is not read either.

T names an attribute declaration. The row's attribute is an attribute written with that name. Another attribute with the same fields is not found. Two T attributes on one member of one class are E0728, and the message names the class, the member and the count. A T that is not an attribute declaration is E1008. That includes a shape such as { text: string }, int and a class.

The type of attribute is the declaration's fields, and it may be null. Write var $row in the foreach to use this type. Storing the result in an array whose attribute cannot be null is E0401.

T can also be an attribute that Novis reads itself, such as Core\Route. The row's attribute is then the fields written in that attribute, such as its path.

When no class implements I, the result is [], whatever $member is.

Commands

#[Core\Command] makes a static method a command for the command line, and #[Core\Option] makes one of its parameters an option. The compiler collects every command into one table and checks it.

A command needs a name:, the word that selects it, and the name is unique in the program. A parameter is a positional argument unless it has #[Core\Option], and a bool option is a flag. Each parameter needs a type that text converts to: string, int, uint, decimal, bool, an enum, a time value, or a class that implements Parses. A time value is written as ISO text, such as 2026-03-14. A word that does not convert is a usage error. The method returns void or the exit status as a uint.

Core\Command::run() calls the command that the first word names. Core\Command::help returns the help page of one command, or the list of all commands. Core\Attributes::get reads the fields of a command or an option.

The fourth example shows an option that does not compile. The fifth reads a date and a moment in time from the command line.

Declare a command line command

<?nvs
// Shows how to declare a command for the command line. `#[Core\Command]` marks
// a static method as a command and gives it a name and a short description. The
// compiler collects every command into one table and checks it.
class Backup
{
// `$directory` is a positional argument. A parameter is positional unless
// it carries `#[Core\Option]`.
#[Core\Command(name: 'backup', about: 'Copy the database to a file')]
public static function backup(
tainted string $directory,
#[Core\Option(short: 'z', about: 'Compress the file')] bool $compress,
): uint
{
echo 'writing to ', $directory, $compress ? '/dump.sql.gz' : '/dump.sql', "\n";
return 0;
}
}
// You can read the table back with `Core\Attributes::get`. This prints
// `backup: Copy the database to a file`.
var $command = Core\Attributes::get<Core\Command>(Backup::backup);
if ($command != null) {
echo $command->name, ': ', $command->about ?? '', "\n";
}
// The command is a static method. You can call it yourself.
uint $status = Backup::backup('/var/backups', true);
echo 'exit status ', $status, "\n";
Output
backup: Copy the database to a file
writing to /var/backups/dump.sql.gz
exit status 0

Options with a short and a long spelling

<?nvs
// Shows the two ways to write an option. `short:` is the one letter form and
// `long:` is the word form. Without `long:` the word form is the parameter's
// own name. A `bool` option is a flag, and every other type takes a value.
class Serve
{
#[Core\Command(name: 'serve', about: 'Run the web server')]
public static function serve(
#[Core\Option(short: 'p', long: 'port', about: 'The port to listen on')] uint $listenPort,
#[Core\Option(short: 'q', long: 'quiet', about: 'Print nothing')] bool $silent,
): uint
{
echo 'listening on ', $listenPort, $silent ? ' (quiet)' : '', "\n";
return 0;
}
}
// This is what a program prints for `serve --help`. Each option shows both
// forms, its type and its description.
echo Core\Command::help('serve');
uint $status = Serve::serve(8080, false);
echo 'exit status ', $status, "\n";
Output
usage: serve [--port] [--quiet]
Run the web server
options:
-p, --port uint The port to listen on
-q, --quiet bool Print nothing
listening on 8080
exit status 0

A help screen built from the command classes

<?nvs
// Shows a help screen that Novis builds from the command classes. Adding a
// command means writing one class. There is no second list to keep up to date.
class Migrate
{
#[Core\Command(name: 'migrate', about: 'Apply pending migrations')]
public static function migrate(
#[Core\Option(short: 'n', about: 'Print what would happen')] bool $pretend,
): uint
{
if ($pretend) {
return 0;
}
return 1;
}
}
class Purge
{
#[Core\Command(name: 'purge', about: 'Delete the expired rows')]
public static function purge(
#[Core\Option(short: 'f', about: 'Do not ask first')] bool $force,
): uint
{
if ($force) {
return 0;
}
return 1;
}
}
class Seed
{
#[Core\Command(name: 'seed', about: 'Fill the database with test data')]
public static function seed(
#[Core\Option(short: 'r', about: 'How many rows')] uint $rows,
): uint
{
if ($rows > 0) {
return 0;
}
return 1;
}
}
// `Core\Command::help(null)` returns the page that lists every command. Each
// description comes from the `about:` of its attribute.
echo Core\Command::help(null);
Output
usage: <command> [arguments]
commands:
migrate Apply pending migrations
purge Delete the expired rows
seed Fill the database with test data

An option needs a type text converts to

<?nvs
// Shows that an option needs a type that text converts to. A `float` option
// is not allowed, so the program does not compile.
// proof: compile-error
class Deploy
{
#[Core\Command(name: 'deploy', about: 'Push the build')]
public static function deploy(#[Core\Option] float $ratio): void
{}
}
Output
error[E0746]: `float` is not a type an option of `deploy` can be given at
--> 04-an-option-needs-a-type-text-converts-to.nvs:9:35
|
9 | public static function deploy(#[Core\Option] float $ratio): void
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ no conversion from an argument's text
= help: an argument arrives as text and its type comes from the parameter, so an option declares `string`, `int`, `uint`, `decimal`, `bool`, an enum, a set of allowed values like `'a'|'b'`, or a class implementing `Parses`
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

A date as an argument

<?nvs
// Shows a command whose argument is a date and whose option is a moment in time.
// Each is written as ISO text on the command line.
class Report
{
// `report 2026-03-14 --since 2026-03-14T09:00:00Z` on the command line runs this
// command. A word that is not a date is a usage error.
#[Core\Command(name: 'report', about: 'Count the orders of one day')]
public static function report(
Core\Time\Date $day,
#[Core\Option(about: 'Count only the orders after this moment')] Core\Time\Instant $since,
): uint
{
echo 'orders of ', $day->toIso(), ' since ', $since->toIso(), "\n";
return 0;
}
}
// The help page shows the type of each value.
echo Core\Command::help('report');
// The command is a static method. You can call it yourself with the same values.
uint $status = Report::report(
Core\Time\Date::fromIso('2026-03-14'),
Core\Time\Instant::fromIso('2026-03-14T09:00:00Z'),
);
echo 'exit status ', $status, "\n";
Output
usage: report <day> [--since]
Count the orders of one day
arguments:
<day> Core\Time\Date
options:
--since Core\Time\Instant Count only the orders after this moment
orders of 2026-03-14 since 2026-03-14T09:00:00Z
exit status 0

Details

#[Core\Command] has two fields, name and about. A command with no name is E0767, and a name that is not a string is E0401. about is optional. #[Core\Option] has three fields, short, long and about, and each is optional. Any other field is E0454. You can write #[Command(...)] and #[Option(...)] after use Core\Command; and use Core\Option;.

Two commands with one name are E0768. This is true when they are in two classes, and when one method has two #[Core\Command] lines with the same name. Two lines with different names on one method give one command two names, and Core\Command::help(null) lists both.

A method that is not static is E0789. A return type other than void or uint is E0789 too, and that includes int. #[Core\Option] on a parameter of a method with no #[Core\Command] is E0766.

Each option has a long form. It is -- and the long value, or -- and the parameter's name when there is no long. With short: "v", the option can also be written -v. Two options of one command that are written the same way are E0745. For example, one parameter has long: "name", and a second parameter is called $name. Both options are --name, so this is E0745. Case matters, so short: "v" and short: "V" can be used in one command.

An option of a type that text does not convert to is E0746. A ?string option is E0746 too. A positional parameter is not checked this way. A positional float compiles, and Core\Command::run throws LogicError when it reaches that command.

Core\Command::run fills the parameters from the words after the command's name. An option may come before, between or after the positional words. An option with a value is written --retries 7. The form --retries=7 is not an option. A bool option is true when it is written and false when it is not, even with the default value true. An option with a default value may be left out. An enum parameter takes the name of a case, written exactly, so Fast works and fast does not. --help is not an option of its own, so deploy --help is a usage error.

A time value is a Core\Time\Date, TimeOfDay, Instant or DateTime. A DateTime needs its time zone, as in 2026-03-14T09:00:00+01:00[Europe/Vienna].

Core\Attributes::get<Core\Command> returns the name and about of one command. Core\Attributes::get<Core\Option> returns the fields of one option.

An argument or an option can be a Core\Time\Date, TimeOfDay, Instant or DateTime. The user writes it as its ISO text, such as 2026-03-09. A DateTime needs its time zone, as in 2026-03-09T08:30:00+01:00[Europe/Vienna]. A text that is not valid is a usage error.

Core\Command::run() returns the exit status of the command. Its reference page has more examples.

  • Do pass what a class needs through the methods of I. Its constructor takes no arguments.
  • Do use implementingWith when you need the attribute of each class. implementing gives only the objects, without their attributes.
  • Do declare a positional argument tainted string, and check it before you use it.
  • Do call Core\Command::run() once, at the top level of the program, and pass its result to exit.