Skip to content

Core\Cli::arguments

Core\Cli::arguments(): array<tainted string>

Gives the words a program was started with, so the program can read its own command line.

When you start a program in a terminal you may write extra words after its name. Those words are how a person tells the program what to do: a file to open, a name to look up, an option such as --verbose. Core\Cli::arguments returns those words as a list of strings, in the order they were written. A program started with no extra words gets an empty list. The name of the program itself is never in the list, and a word that contains spaces stays one element.

Good to know: every word in the list is tainted. That means the text came from outside the program, so anything can be in it. You may print such a word or compare it with text of your own. Before you use one as a file path, a web address or part of a database query, check it against the values you expect.

The examples below print the words, read an option from them, and choose a job with a fixed list of allowed names.

Return value

Returns array<tainted string>. An array<tainted string>, in the order the shell passed them. It is empty if the program was started with no arguments. Every element is tainted, because the words come from outside the program. A path, a URL or a query built from one must pass the check for that use first. The program name is not in the array, so you do not need to skip it. There is no $argv[0].

Examples

Read the words

<?nvs
// Shows how to read the words a program was started with. Start it with
// `nvs run 01-read-the-words.nvs report march` to see those two words.
array<tainted string> $words = Core\Cli::arguments();
echo 'words: ', Core\Arr::count($words), "\n";
// The words come in the order the person wrote them. This loop prints one line
// per word, and nothing at all for a program started with no words.
foreach ($words as tainted string $word) {
echo '- ', $word, "\n";
}
Output
words: 0

An option on the command line

<?nvs
// Shows how a program reads an option from its command line. Start it with
// `nvs run 02-an-option-on-the-command-line.nvs --verbose` to see the extra
// lines.
array<tainted string> $words = Core\Cli::arguments();
// Comparing a word with text of your own is always allowed. This is `true` only
// when one of the words is exactly `--verbose`.
bool $verbose = false;
foreach ($words as tainted string $word) {
if ($word == '--verbose') {
$verbose = true;
}
}
array<string> $files = ['orders.csv', 'customers.csv'];
foreach ($files as string $file) {
echo 'copied ', $file, "\n";
if ($verbose) {
echo ' from var/export/', $file, "\n";
}
}
Output
copied orders.csv
copied customers.csv

Choose a job by name

<?nvs
// Shows the everyday use: the first word says which job to run. The word came
// from outside the program, so only these three names are accepted. Start it
// with `nvs run 03-choose-a-job-by-name.nvs weekly` to build the weekly report.
class Reports
{
public static function run(array<tainted string> $words): void
{
array<string> $known = ['daily', 'weekly', 'monthly'];
if (Core\Arr::isEmpty($words)) {
Reports::usage($known);
return;
}
tainted string $asked = $words[0];
foreach ($known as string $name) {
if ($asked == $name) {
// `$name` is the program's own text, so it is safe to use as a
// file name. The word from the command line is only compared.
echo 'building the ', $name, " report\n";
return;
}
}
echo 'there is no report called ', $asked, "\n";
Reports::usage($known);
}
public static function usage(array<string> $known): void
{
echo "usage: reports <name>\n";
foreach ($known as string $name) {
echo ' ', $name, "\n";
}
}
}
Reports::run(Core\Cli::arguments());
Output
usage: reports <name>
daily
weekly
monthly