This snippet allows for very simple argument parsing. Use it like
$args = require_once 'args.php';Features:
- arguments in quotes are handled correctly
- long names:
php app.php --name foo→$args['name'] === "foo" - short names:
php app.php -n foo→$args['n'] === "foo" - values without names:
php app.php foo bar→$args['_'] === [ "foo", "bar" ] - names without subsequent values are booleans (
true):php app.php --name→$args['name'] === true - combined short names are always booleans (
true):php app.php -wvb→$args['w'] === true, $args['v'] === true, $args['b'] === true
Non-Features:
No error handling at all. Being in a CLI environment is assumed and arguments are trusted to be well-formatted.