ordinary/command

Library used to normalize the structure of files, functions, and classes intended to be ran from a shell.

2.3.0 2023-05-12 20:55 UTC

This package is auto-updated.

Last update: 2024-05-12 23:16:10 UTC


README

Getting Started

Install using composer.

composer require ordinary/command

Examples

Directly in executable file

Create the class.

class MyCommand extends \Ordinary\Command\Command
{
    public function run() : int {
        // do something
        return 0; // int 0-255 exit status
    }
    
    public function showHelp() : void {
        fwrite($this->stdout(), <<<HELP
        My Help Content
        HELP);
    }
    
    public function beforeExecute() : ?int {
        // do stuff before help screen and before run
        return null; // return null to continue or int error status for early exit
    }
}

Make the executable file with execute permissions.

#!/usr/bin/env php
<?php
## my-cmd.php
use Ordinary\Command\CommandExec;
use Ordinary\Command\Command;

$exec = new CommandExec();

/** @var Command $cmd */
$cmd = new MyCommand();

exit($exec->execute(
    $cmd->withArgs($_SERVER['argv'])
        ->withStreams(STDIN, STDOUT, STDERR)
));

Run the file

./my-cmd.php