Skip to content

MandaNyAina/cli-php-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

CLI for PHP

Allows you to run a command line with PHP. Makes your life easier to add functionality with CLI command. This project is similar to inquirer.js for PHP.

Requirement

To use cli-starter for PHP, you need to have :

  • PHP 7+ : to install PHP, visit ici

Installation

Clone project from git and copy the file cloned to your project root path

  git clone https://github.com/MandaNyAina/cli-php-starter your-project-bin
  cp -r your-project-bin/ <your-project-path>

How to get argument in CLI (ex : bin/cli --key value)

1 - Create an Options instance

  use bin\lib\Instance\Options;

  $options = new Options();

2 - Add new option for CLI, In the example below, we will add a "name" option for the command to retrieve the project name, and call the "get_name" function in "lib/Command/ArgumentFunctionList.php".

  $options->addOption('name', 'Name of your project', 'get_name');

And in "lib/Command/ArgumentFunctionList.php", we have "get_name" with one parameter (user response) :

  function get_name(string $name) {
    echo "Your name is $name";
  }

3 - To retrieve the list of existing options

  $options->getOptionList();

4 - To execute options with argv from CLI

  (new CommandArgument($argv, $options))->execute();

Once the --name project_name command is specified, the get_name function will be called.

How to use prompt command

1 - Initiate the CommandPromp instance

  use bin\lib\PrompCommande;

  $prompt = new PrompCommande();

2 - Create a Question instance to be able to ask the user to enter variable. For example, if we want to create a question to ask for the name of a project.

  $question = new Question('string', 'Name of your project : ');

For the Question class parameter, we need :

  • Params 1 : type of question, we have 2 types of question:
    • string : to request a text, number, ... from the user
    • list : to ask to choose from a list for the user

For the example above, the question is of type text

  • Params 2 : the question for the user.
  • Params 3 : List of choice, only available for list question type, the type of params is an array

Example of list type :

  $question_list = new Question('list', 'Your stack : ', ['php', 'js']);

Result :

  Your stack : 
  [1] php
  [2] js
  Choose number [] > 1 (for php)

3 - To execute the question and retrieve the answer

  $name = $prompt->execute($question)->getReponse();

Authors

License

MIT

About

Allows you to run a command line with PHP. Makes your life easier to add functionality with CLI command

Resources

License

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages