Skip to content

Latest commit

 

History

History
114 lines (79 loc) · 2.59 KB

exec.md

File metadata and controls

114 lines (79 loc) · 2.59 KB

Execution

There are two ways to run a PHP script with pogo. These are very similar to how one invokes php.

Direct invocation

Suppose you've created a file my-script.php:

<?php
echo "Hello world.\n";

To execute this with standard PHP, you would say:

php my-script.php

To execute this with pogo, you would say:

pogo my-script.php

Executable files (indirect invocation)

Suppose you are creating an executable program /usr/local/bin/my-script.

The first line needs an instruction on how to execute the script. For standard PHP, the file my-script might be:

#!/usr/bin/env php
<?php
echo "Hello world.\n";

To use pogo, simply change the first line:

#!/usr/bin/env pogo
<?php
echo "Hello world.\n";

In either case, mark the file as executeable and run it:

chmod +x /usr/local/bin/my-script
my-script

Interpreter options

TIP: Most interpreter options can also be specified as in-file pragmas.

When you call pogo directly, it accepts a few options, such as:

  • --dl=<DIR> - Explicitly store dependencies in the given directory
  • -f/--force - Forcibly download fresh dependencies, even the dependencies are currently available
  • --allow-stale - The opposite of -f; do not expire/redownload dependencies

For example, if you wanted to inspect or debug the dependencies, you might explicitly call:

pogo -f --dl=/tmp/depdebug my-script.php

The --dl can be relative. By default, it's relative to the CWD (which is more intuitive for direct invocations), but you may make it relative to the script with the variable {SCRIPT_DIR}.

pogo --dl='{SCRIPT_DIR}/.my-script.deps' my-script.php

For more details about pogo command-line options, run pogo -h.

Script options

Additionally, scripts may receive command-line options. These work in the same way that PHP normally handles them -- with the global $argv variable. For example, suppose we have a script named greeter:

#!/usr/bin/env pogo
<?php
printf("Hello %s!\n", $argv[1]);

You can pass arguments in either style:

$ pogo greeter Alice
Hello Alice!

or:

$ ./greeter Bob
Hello Bob!

Of course, if you accept a lot of options, then you'll want an easier way to parse $argv. Fortunately, you can get a PHP library for that! One nice library is mnapoli/silly; you can just copy an example and include the pragma:

#!require mnapoli/silly: ~1.7

(Full example: examples/cli-silly.php)