Download the PHP package tarsana/command without Composer

On this page you can find all versions of the php package tarsana/command. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package command

Tarsana Command

Build Status Coverage Status Code Quality Donate Software License

A library to build command line applications using PHP. This is part of the Tarsana Project.

Table of Contents

Installation

Install it using Composer

Your First Command

Let's write a "Hello World" command. Create a file hello.php with the following content:

Then run it from the terminal:

Congratulations, you have just written your first command :D

As you see, Tarsana\Command\Command is a class providing the basic features of a command. Every command should extend it and implement the execute() method.

Initializing The Command

In addition, Command gives the init() method which is used the initialize the command general attributes. Let's rewrite our HelloWorld command:

Here we are overriding the init() method to define the command name, version and description.

Note that the setter of an attribute foo is named foo() instead of setFoo(). I know that this is not a common convention but it makes sense for me. :P

Showing the Help and Version of a Command

To show the version of a command, we use the --version flag (we will learn after that this is actually a sub command). We also have the --help to show the help message:

Show version and help message

Reading & Writing to the Console

The attribute console is used to handle the reading and writing operations to the console.

Let's update our command to read the user name:

The Console class provides some tags to control the output:

Show colors in the console

The <background:$number> and <color:$number> tags allows to set the background and foreground colors of the text to be written; the <reset> tag resets the default values. The colors are given as numbers from the 256-color mode.

List of supported tags

Console allows you also to define styles using aliases:

Predefined aliases are:

Console output aliases

Note: tags and aliases can be used in all strings printed to the console, including the command and arguments descriptions.

Defining Arguments and Options

The command syntax is defined using the Syntax library. Let's start with a command that repeats a word a number of times:

We are using the method syntax() to define the syntax of arguments. The string given to this method follows the rules described here

The describe() method is used to describe an argument.

When you define the syntax of the command; arguments are parsed automatically and available in the execute() method via the args attribute.

The help subcommand shows full description of the arguments and options:

Help message example

And the result is:

In the second example, the count argument takes automatically its default value.

Warning: Giving wrong arguments generates an error

Parse error example

Reading Arguments and Options Interactively

Some commands can have long and complicated list of arguments. Defining the syntax of such command is easy thanks to Syntax but typing the arguments in the command line becomes challenging.

Let's take the following command for example:

if you run the command using the -i flag, it will let you enter the arguments interactively:

Interactive Arguments Reader

After reading all args, the command will show the command line version of the entered args:

which means that running

would produce the same result.

Handling The Filesystem

The fs attribute is an instance of Tarsana\IO\Filesystem that you can use to handle files and directories. Read the documentation for the full API.

By default, the Filesystem instance points to the directory from which the command is run. You can also initialize it to any directory you want:

Loading Configuration

In addition to the command line arguments, the user can provide data to your command via configuration files. This is useful because it lets you define a default configuration file and lets the user change some values with a custom configuration file.

Let's write an example command which have a global configuration file at /home/user/.config.json. It lets the user customize value via the file config.json in the current directory:

Note that:

Rendering Templates

The Command class gives also possibility to render templates. The default template engine is Twig but you can use your favorite one by implementing the interfaces TemplateLoaderInterface and TemplateInterface.

Let's make a command which renders a simple template. For this we will create two files:

hello.twig

This is a simple template that print a hello message.

render-hello.php

Result

Adding SubCommands

You can add subcommands while initializing your command.

Now when you run

The FooCommand will be run with other arguments here as arguments.

Note: subcommands will always have the attributes console, fs and templatesLoader pointing to the same objects as their parent, as long as you don't change them explicitly in the subcommand's code.

Testing Commands

The class Tarsana\Tester\CommandTestCase extends PHPUnit\Framework\TestCase and adds useful methods to test Tarsana Commands.

Testing the Input and Output

Let's write a test for our HelloWorld command above which reads the user name than shows the hello message.

Sets the content of the standard input of the command.

Runs the command $c with the standard input and $args then stores its outputs for further assertions.

Testing the Arguments and Options

Let's now test the RepeatCommand above.

Assert that the parsed arguments and options of the command are equal to the given values.

Testing the Filesystem

Let's take the following command:

The test can be written as follows:

The CommandTestCase run the command with a virtual filesystem. The methods havingFile and havingDir can be used to create files and directories on that filesystem before running the command.

What's Next

Please take a look at the examples in the examples directory, and try using the library to build some awesome commands. Any feedback is welcome!

Development Notes


All versions of command with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
tarsana/io Version ^2.0
tarsana/syntax Version ^2.0
twig/twig Version ^2.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tarsana/command contains the following files

Loading the files please wait ....