Attention needed

Saeghe is a difficult name to pronounce. Therefore, Saeghe project has been renamed to phpkg.

This website no longer receives updates.

Please visit phpkg website at phpkg.com

Read Library

This document describes included functions in the Saeghe\Cli\IO\Read library.

argument

function argument(int $number, ?string $default = null): ?string

Description

Returns the numberth argument passed to the PHP file from CLI. Default passed value or null will be returned if the given nth argument is not passed.

Usages

use function Saeghe\Cli\IO\Read\argument;

$name = argument(1);
$email = argument(2, 'my-email@example.com');
use Saeghe\Cli\IO\Read;

$name = Read\argument(1);
$email = Read\argument(2, 'my-email@example.com');

Examples

php my-file.php // argument(1) => `null`
php my-file.php command add // argument(1) => `command`
php my-file.php command add // argument(2) => `add`
php my-file.php command -f add // argument(2) => `add`
php my-file.php command -f --parameter=anything add // argument(2) => `add`
php my-file.php command -f --parameter=anything add // argument(3) => `null`

command

function command(): ?string

Description

Returns the first command passed to the PHP file. It will return null if the first argument start with - or --. This is a shorthand for argument(1, null).

Usages

use function Saeghe\Cli\IO\Read\command;

$command = command();
use Saeghe\Cli\IO\Read;

$command = Read\command();

Examples

php my-file.php -v // command() => `null`
php my-file.php --version // command() => `null`
php my-file.php add anything // command() => `add`
php my-file.php help // command() => `help`
php my-file.php -version help // command() => `null`

parameter

function parameter(string $name, ?string $default = null): ?string

Description

Returns the name parameter passed to the PHP file. If name not passed, then it returns null or passed default value.

Usages

use function Saeghe\Cli\IO\Read\command;

$name = parameter('name');
$email = parameter('email', 'john@example.com');
use Saeghe\Cli\IO\Read;

$name = Read\parameter('name');
$email = Read\parameter('email', 'john@example.com');

Examples

php my-file.php -v // parameter('name') => `null`
php my-file.php -v name // parameter('name') => `null`
php my-file.php -v -name // parameter('name') => `null`
php my-file.php -v --name=John // parameter('name') => `John`
php my-file.php -v --name="John Doe" // parameter('name') => `John Doe`

option

function option(string $name): bool

Description

Returns boolean to detect if the given name option passed to the PHP file.

Usages

use function Saeghe\Cli\IO\Read\command;

$versionPassed = option('version');
use Saeghe\Cli\IO\Read;

$versionPassed = Read\option('version');

Examples

php my-file.php // option('version') => `false`
php my-file.php -v // option('version') => `false`
php my-file.php -version // option('version') => `true`
php my-file.php --version // option('version') => `true`
php my-file.php --version= // option('version') => `true`
php my-file.php --version=false // option('version') => `true`