<?php
function build_args_simple(): array {
    $args = [];
    $args['device'] = '/dev/ttyr00';
    $args['baud'] = 115200;
    $args['shift'] = true;
    return $args;
}

function build_args_from_argv(array $argv): array {
    $args = [];
    for ($i = 1; $i < count($argv); $i++) {
        $arg = $argv[$i];
        $next = function () use ($argv, &$i, $arg): string {
            if (!isset($argv[$i + 1])) {
                throw new RuntimeException('Missing value for ' . $arg);
            }
            return $argv[++$i];
        };
        switch ($arg) {
            case '-d':
            case '--device':
                $args['device'] = $next();
                break;
            case '-b':
            case '--baud':
                $args['baud'] = (int) $next();
                break;
            case '--timeout':
                $args['timeout'] = (float) $next();
                break;
            case '--shift':
                $args['shift'] = true;
                break;
        }
    }
    return $args;
}

$simple = build_args_simple();
$parsed = build_args_from_argv(['script', '-d', '/dev/ttyr01', '--baud', '9600', '--timeout', '1.5', '--shift']);

echo $simple['device'], ' ', $simple['baud'], ' ', ($simple['shift'] ? 'shift' : 'no-shift'), PHP_EOL;
echo $parsed['device'], ' ', $parsed['baud'], ' ', $parsed['timeout'], ' ', ($parsed['shift'] ? 'shift' : 'no-shift'), PHP_EOL;
