Thanks to visit codestin.com
Credit goes to github.com

Skip to content

minicli/minicli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minicli 2

Minicli is an experimental dependency-free toolkit for building CLI-centric applications in PHP. Minicli was created as an educational experiment and a way to go dependency-free when building simple command-line applications in PHP. It can be used for microservices, personal dev tools, bots and little fun things.

Minicli 2 has a lot of improved features, and about 98% test coverage with Pest PHP.

Dependency-free: What Does it Mean

What does it mean to be dependency-free? It means that you can build a working CLI PHP application without dozens of nested user-land dependencies. The basic minicli/minicli package has only testing dependencies, and a couple system requirements:

  • PHP >= 7.2
  • ext-readline for obtaining user input

It gives you a lot of room to choose your own dependencies.

Getting Started

There are two ways to get started. If you want the bare minimum:

Minimalist App

If you just want to set up a few simple commands to run through minicli, all you need to do is to create an App and register your commands as anonymous functions.

  1. Create an empty project
  2. Run composer require minicli/minicli - this will generate a new composer.json file.
  3. Create a minicli script with the following content:
#!/usr/bin/php
<?php

if (php_sapi_name() !== 'cli') {
    exit;
}

require __DIR__ . '/vendor/autoload.php';

use Minicli\App;
use Minicli\Command\CommandCall;

$app = new App();
$app->setSignature('./minicli mycommand');

$app->registerCommand('mycommand', function(CommandCall $input) {
    echo "My Command!";

    var_dump($input);
});

$app->runCommand($argv);

Then, make it executable and run minicli with your command:

chmod +x minicli
./minicli mycommand

Structured App

For a more structured application using Controllers and Services, it's best to use Command Namespaces. Our application template repository is a great starting point / template to set up Minicli that way.

To create a new project using the minicli/application template, run:

composer create-project --prefer-dist minicli/application myapp

This will generate a directory structure like the following:

.
app
└── Command
    └── Help
        ├── DefaultController.php
        ├── TableController.php
        └── TestController.php
├── composer.json
├── docs
├── LICENSE
├── minicli
├── mkdocs.yml
└── README.md

Each directory inside app/Command represents a Command Namespace. The classes inside app/Command/Help represent subcommands that you can access through the main help command.

You can now run the boostrapped application with:

./minicli help

The documentation contains more detailed information about creating commands and working with output.

Building Minicli

The following tutorials on dev.to compose a series named "Building Minicli", where we create minicli from scratch:

Note: Minicli has evolved a lot since that series was initially written, but that was the base for what Minicli is today.

Created with Minicli

  • Dolphin - a CLI tool for managing DigitalOcean servers with Ansible.

About

A minimal framework for command-line applications in PHP. Join our Discord: https://discord.gg/hY5UNC4XPf

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 26

Languages