2017 © Pedro Peláez
 

library command-line-interface

A php script command setter

image

senhung/command-line-interface

A php script command setter

  • Sunday, April 29, 2018
  • by senhung
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Command Line Interface

Description

A package imitating Aritsan Console for writing command easier., (*1)

Setup

  1. Add Dependency
$ composer require senhung/command-line-interface
  1. Add a Entry for Command

Create a file in root directory and name it the command you want to call, (*2)

For example create a file named example; calling the command line will be, (*3)

$ php example <command>
  1. Edit Command Entry

Open the command entry file just created and add the following, (*4)

<?php

require_once 'vendor/autoload.php';

use Senhung\CLI\CommandEntry;

/* Read through all commands in <change-folder-to-your-command-folder> */
CommandEntry::load('<change-folder-to-your-command-folder>');

/* When calling the script, execute the target command */
CommandEntry::entry($argv);

Usage

Create Command

Open the command folder and create a php script, (*5)

<?php

namespace Some\Name\Space;

use Senhung\CLI\Command;

class YourClassName extends Command
{
    /**
     * @var string $signature
     *
     * Set the signature of your command
     *
     * <command> how you call the command
     * {:arguments} arguments will be filled in in order when call
     * {--options} options are like flags or parameters
     */
    public $signature = '<command> {:arguments} {--options}';

    /**
     * @var string $description
     *
     * Description of your command
     *
     * Will be used in help command
     */
    public $description = 'Description for <command>';

    /**
     * The executing function when calling the command
     */
    public function handle()
    {
        /**
         * Write your handling function here
         *
         * You can get argument by: $this->getArgument('<argument-key>')
         * You can get option by: $this->getOption('<option-key>')
         */
    }
}

Default Values

You can set default value for argument and option by setting the signature like:, (*6)

public $signature = '<command> {:arguments=some-default-value} {--options=some-default-value}';

Calling Command

You can call your command by:, (*7)

$ php <entry-file-name> <command> <argument> <--opiton-as-a-flag> <--option-as-a-param=some-value>

You can mix up arguments and options, the package will recognize argument/options, (*8)

Note: The order of arguments is important, but the order of options is not, (*9)

Help

Call the following to get all commands:, (*10)

$ php <entry-file-name> help

You can add one more argument for getting specific function's description, (*11)

$ php <entry-file-name> help <command>

Example

Example Entry

<?php

/* file: example */

require_once 'vendor/autoload.php';

use Senhung\CLI\CommandEntry;

CommandEntry::load('Commands');

CommandEntry::entry($argv);

Example Command

<?php

/* file: Commands/Greet.php */

namespace Example;

use Senhung\CLI\Command;

class Greet extends Command
{
    protected $signature = 'greet {:name} {--with-exclamation} {--number-of-times=1}';

    protected $description = 'Greet people';

    public function handle()
    {
        /* Repeat Greeting Times */
        $numberOfTimes = $this->getOption('number-of-times');

        /* Get Greeting Person's Name */
        $name = $this->getArgument('name');

        /* Check If Using Exclamation Point */
        $withExclamation = $this->getOption('with-exclamation');

        /* Repeat $numberOfTimes Times */
        for ($index = 0; $index < $numberOfTimes; $index++) {
            $greet = "Hello " . $name;

            if ($withExclamation) {
                $greet .= "!";
            }

            $greet .= "\n";

            echo $greet;
        }
    }
}

Using Command

With Argument

$ php example greet Senhung

Output:, (*12)

Hello Senhung

Note: If no argument is passed in, {:name} will be null, (*13)

With Option (Flag)

$ php example greet Senhung --with-exclamation

Output:, (*14)

Hello Senhung!

With Option (Parameter)

$ php example greet Senhung --with-exclamation --number-of-times=3

Output:, (*15)

Hello Senhung!
Hello Senhung!
Hello Senhung!

The Versions

29/04 2018

dev-master

9999999-dev

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

0.4.1

0.4.1.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

v0.4.1

0.4.1.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

0.4.0

0.4.0.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

v0.4.0

0.4.0.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

0.3.0

0.3.0.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

29/04 2018

v0.3.0

0.3.0.0

A php script command setter

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Senhung Wong

28/04 2018

0.2.0

0.2.0.0

A php script command setter

  Sources   Download

MIT

by Senhung Wong

28/04 2018

v0.2.0

0.2.0.0

A php script command setter

  Sources   Download

MIT

by Senhung Wong

25/04 2018

0.1.0

0.1.0.0

A php script command setter

  Sources   Download

MIT

by Senhung Wong

25/04 2018

v0.1.0

0.1.0.0

A php script command setter

  Sources   Download

MIT

by Senhung Wong