Creating a new Minicli application¶
Minicli is a minimalist framework for building CLI-centric PHP applications. Minicli has no external package dependencies, and a single system requirement:
- PHP 8.1+ (cli)
Note: If you want to obtain user input, then the
readline
PHP extension is required as well.
Apart from that, you'll need Composer to install and use Minicli.
Creating a Simple App¶
If all you need is to set up a few simple commands to run through Minicli, you can use a singe file for your application.
- Create a directory for your new project
- Run
composer require minicli/minicli
- this will create acomposer.json
file and download Minicli. - Create an executable PHP script with the following content:
This script will set up a command called mycommand
. To execute it, first make the file executable with:
Bash | |
---|---|
Then, run your command with:
Bash | |
---|---|
Creating a Structured App Using the Application Template¶
If you'd like to create an application with many commands, or you would prefer to have more structure to your commands, with support to subcommands, than you should use Command Namespaces.
To facilitate setting up such an application, we have the minicli/application
repository, which serves as an application template you can use with composer create-project
.
Create a new project with:
Bash | |
---|---|
This will generate a directory structure like the following:
Each directory inside app/Command
represents a Command Namespace.
The classes inside app/Command/Demo
represent subcommands that you can access through the main demo
command.
You can now run the boostrapped application with:
This will show you the default app signature.
You can use the included help
command to see a list of available commands. This should exhibit also the application-defined commands, in this case, the "demo" command.
Bash | |
---|---|
demo
command that comes with the application template, has a couple subcommands to demonstrate Minicli usage with Command Controllers.
The demo test
command, defined in app/Command/Demo/TestController.php
, shows an echo test of parameters:
Text Only | |
---|---|
The demo table
commands prints a table demo:
Bash | |
---|---|
Bash | |
---|---|
Check out the Creating Controllers section for more information about how command namespaces and controllers work.
Creating a Structured App Using MiniTerm¶
MiniTerm is a template based in the Application Template above but extended with:
Create a new project with:
Bash | |
---|---|
This will generate a directory structure like the following:
Each directory inside app/Command
represents a Command Namespace.
The classes inside app/Command/Demo
represent subcommands that you can access through the main demo
command.
You can now run the boostrapped application with:
This will show you the default app signature.
You can use the included help
command to see a list of available commands. This should exhibit also the application-defined commands, in this case, the "demo" command.
Bash | |
---|---|
demo
command that comes with the application template, has a couple subcommands to demonstrate MiniTerm usage with Command Controllers.
The demo ask
command, defined in app/Command/Demo/AskController.php
, shows how to ask for user input and render a message styled with Termwind:
Bash | |
---|---|
The demo color
command, defined in app/Command/Demo/ColorController.php
, shows how to render a message styled with Termwind using color helpers:
Bash | |
---|---|
The demo test
command, defined in app/Command/Demo/TestController.php
, shows how to render a message styled with Termwind using param inputs:
Text Only | |
---|---|
Text Only | |
---|---|
The demo table
commands prints a table demo using Plates to render a view file:
Bash | |
---|---|
All your application commands should extend the BaseController
class, which provides helper methods for rendering output with Termwind and Plates:
render
: Used to render an HTML string using Termwind.style
: Used to add own custom styles and also update colors.ask
: Used to ask for user input.terminal
: returns an instance of the Terminal class, with the following methods:width
: returns the full width of the terminal.height
: returns the full height of the terminal.clear
: clears the terminal.
view
: Used to render a view file using Termwind and Plates.
Configuring the Output Styles¶
You can configure the output styles by editing the app/Config/TermwindOutputConfig.php
file.
You can update the css classes in the styles
method to change the appearance of the output.
You can configure if you want to show labels for the commands and subcommands by updating the enableLabels
method.
When set to true the output will look like this:
You can also configure which styles should display labels by updating the stylesWithLabels
method.
Creating a View¶
You can create a view file in the app/Views
directory and use it in a command controller like this:
Bash | |
---|---|
HTML | |
---|---|