Macrame is a library that provides features that allow developers to create interactive, good-looking command-line scripts.
Macrame objects
Macrame functionality is provided by objects, each of which focuses on a particular feature set. The objects are:
- MacrameArgs: Command line argument parsing and reading.
- MacrameDownload: Remote file downloading with dynamic progress display.
- MacrameFiglet: ASCII art headlines.
- MacrameFile: Safer file access.
- MacrameInput: User text input.
- MacrameIO: Low-level input/output functionality.
- MacrameMenu: Interactive menus.
- MacrameSpinner: Animated spinner display for functions run in the background.
- MacrameTable: ASCII table output.
- MacrameText: Text styling and output.
- MacrameValidator: Input validation rules.
Note
The
MacrameIO
object is designed for internal use by Macrame. It is recommended to use the higher-level functionality in, ie. MacrameInput
or `MacrameText` instead.
Note
The
MacrameValidator
object is not designed to be used independently. See the documentation on MacrameInput
on how to validate user input.
Instantiating objects
Each object can be instantiated by calling a function in the Macrame
object.
// create a new Macrame object
$macrame = new Macrame();
$macrameArgs = $macrame->args("somearg");
$macrameDownload = $macrame->download("https://someurl.ca/somefile");
$macrameFiglet = $macrame->figlet("Some headline");
$macrameInput = $macrame->input();
$macrameMenu = $macrame->menu();
$macrameSpinner = $macrame->spinner();
$macrameTable = $macrame->table(['header 1', 'header 2'], [['data 1', 'data 2']]);
$macrameText = $macrame->text("Some text");
Chaining method calls
Macrame calls are chainable. For instance, text can be output using both these methods.
$macrame = new Macrame();
// chained call
$macrame->text("Some text")->write();
// no chaining
$macrameText = $macrame->text("Some text");
$macrameText->write();
Script structure
Macrame has a recommended basic script structure. An explanation of this structure is given in the Quick Start section.
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Gbhorwood\Macrame\Macrame;
// instantiate a Macrame object with the script name
$macrame = new Macrame("My Script Name");
// only execute if run from the command line
if($macrame->running()) {
// confirm host is good. die on failure.
$macrame->preflight();
// user code here
// exit cleanly
$macrame->exit();
}