USAGE: test [option]... [FILE]...

Options:
  -a something
  -b something
  -c something

위 스타일의 CLI를 php로 구현해 보자.

<?php

$options = getopt('a:b:c:');

$argx = 0;
while (++$argx < $argc && preg_match('/^-/', $argv[$argx]));
$arguments = array_slice($argv, $argx);

if (count($arguments) < 1) {
    die('Missing FILE argument');
	usage();
}

// check option a
if (isset($options['a'])) {
}

// check option b
if (isset($options['b'])) {
}

// check option b
if (isset($options['c'])) {
}

$files = $arguments;

// do something with $files file
var_dump($files);

function usage()
{
	echo "blur blur ...";
}