|
1 |
| -# application |
2 |
| -Application template repository |
| 1 | +# minicli |
| 2 | + |
| 3 | +[Minicli](https://github.com/minicli/minicli) is an experimental dependency-free toolkit for building CLI-only applications in PHP created by @erikaheidi. |
| 4 | +This repository is a template you can use to create a new application that has a single dependency: `minicli/minicli`. |
| 5 | + |
| 6 | +### Why minicli |
| 7 | + |
| 8 | +The current trend in software development is basing your project on a big pile of unknowns. There is nothing wrong in using third party software, but if more than 80% of your application is out of your control, things can get messy. |
| 9 | +What usually happens is that you don't even know what packages you're depending on, when using the most popular frameworks. |
| 10 | + |
| 11 | +Minicli was created as [an educational experiment](https://dev.to/erikaheidi/bootstrapping-a-cli-php-application-in-vanilla-php-4ee) 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. |
| 12 | + |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +You'll need `php-cli` and [Composer](https://getcomposer.org/) to get started. |
| 17 | + |
| 18 | +Create a new project with: |
| 19 | + |
| 20 | +``` |
| 21 | +composer create-project --prefer-dist minicli/application myapp |
| 22 | +``` |
| 23 | + |
| 24 | +Once the installation is finished, you can run `minicli` it with: |
| 25 | + |
| 26 | +``` |
| 27 | +cd myapp |
| 28 | +./minicli |
| 29 | +``` |
| 30 | + |
| 31 | +This will show you the default app signature: |
| 32 | + |
| 33 | +``` |
| 34 | +usage: ./minicli help |
| 35 | +``` |
| 36 | + |
| 37 | +The default `help` command that comes with minicli (`app/Command/Help/DefaultController.php`) auto-generates a tree of available commands: |
| 38 | + |
| 39 | +``` |
| 40 | +./minicli help |
| 41 | +``` |
| 42 | + |
| 43 | +``` |
| 44 | +Available Commands |
| 45 | +
|
| 46 | +help |
| 47 | +└──test |
| 48 | +
|
| 49 | +``` |
| 50 | + |
| 51 | +The `help test` command, defined in `app/Command/Help/TestController.php`, shows an echo test of parameters: |
| 52 | + |
| 53 | +``` |
| 54 | +./minicli help test user=erika name=value |
| 55 | +``` |
| 56 | + |
| 57 | +``` |
| 58 | +Hello, erika! |
| 59 | +
|
| 60 | +Array |
| 61 | +( |
| 62 | + [user] => erika |
| 63 | + [name] => value |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +### The simplest app |
| 68 | + |
| 69 | +The simplest minicli script doesn't require using Command Controllers at all. You can delete the `app` folder and use `registerCommand` with an anonymous function, like this: |
| 70 | + |
| 71 | +``` |
| 72 | +#!/usr/bin/php |
| 73 | +<?php |
| 74 | +
|
| 75 | +if (php_sapi_name() !== 'cli') { |
| 76 | + exit; |
| 77 | +} |
| 78 | +
|
| 79 | +require __DIR__ . '/vendor/autoload.php'; |
| 80 | +
|
| 81 | +use Minicli\App; |
| 82 | +use Minicli\Command\CommandCall; |
| 83 | +
|
| 84 | +$app = new App(); |
| 85 | +$app->setSignature('./minicli mycommand'); |
| 86 | +
|
| 87 | +$app->registerCommand('mycommand', function(CommandCall $input) { |
| 88 | + echo "My Command!"; |
| 89 | +
|
| 90 | + var_dump($input); |
| 91 | +}); |
| 92 | +
|
| 93 | +$app->runCommand($argv); |
| 94 | +``` |
| 95 | + |
| 96 | +## Created with Minicli |
| 97 | + |
| 98 | +- [Dolphin](https://github.com/do-community/dolphin) - a CLI tool for managing DigitalOcean servers with Ansible. |
0 commit comments