Skip to content

Instantly share code, notes, and snippets.

@getinstancemz
Last active February 5, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getinstancemz/09900b3a496e3ffb9fd6f5dac4c7faeb to your computer and use it in GitHub Desktop.
Save getinstancemz/09900b3a496e3ffb9fd6f5dac4c7faeb to your computer and use it in GitHub Desktop.
#!/usr/local/bin/php
<?php
function usage(?string $msg = null): string
{
$argv = $GLOBALS['argv'];
$usage = "\n";
$usage .= sprintf("usage: %s <what>\n", $argv[0]);
$usage .= sprintf("%6s %-6s %s\n", "-h", "", "this help message");
$usage .= sprintf("%6s %-6s %s\n", "-a", "<more>", "add a little more");
$usage .= "\n";
if (! is_null($msg)) {
$usage .= "$msg\n\n";
}
return $usage;
}
function errorUsage(string $msg): void
{
fputs(STDERR, usage($msg));
exit(1);
}
$options = getopt("ha:", [], $rest_index);
$myargs = array_slice($argv, $rest_index);
// check options
if (isset($options['h'])) {
print usage();
exit(0);
}
$more = "";
if (isset($options['a'])) {
$more = ", {$options['a'] }";
}
// basic argument checks
if (count($myargs) < 1) {
errorUsage("too few arguments");
}
// assignment
$world = $myargs[0];
// run the (hello) world
print "hello {$world}{$more}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment