Download the PHP package asika/simple-console without Composer

On this page you can find all versions of the php package asika/simple-console. 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 simple-console

PHP Simple Console V2.0

GitHub Actions Workflow Status Packagist Version Packagist Downloads

Single file console framework to help you write scripts quickly, v2.0 requires PHP 8.4 or later.

This package is highly inspired by Symfony Console and Python argparse.

Installation

Use composer:

Download single file: Download Here

CLI quick download:

Getting Started

Run Console by Closure

Use closure to create a simple console script.

The closure can receive a Console object as parameter, so you can use $this or $app to access the console object.

The $argv can be omit, Console will get it from $_SERVER['argv']

Run Console by Custom Class

You can also use class mode, extends Asika\SimpleConsole\Console class to create a console application.

The Return Value

You can return true or 0 as success, false or any int larger than 0 as failure. Please refer to GNU/Linux Exit Codes.

Simple Console provides constants for success and failure:

Parameter Parser

Simple Console supports arguments and options pre-defined. Below is a parser object to help use define parameters and parse argv variables. You can pass the parsed data to any function or entry point.

Same as:

Parameter Definitions

After upgraded to 2.0, the parameter defining is required for Console app, if a provided argument or option is not exists in pre-defined parameters, it will raise an error.

Also same as:

Now if we enter

It shows:

Then, if we enter wrong parameters, Simple Console will throw errors:

Show Help

Simple Console supports to describe arguments/options information which follows docopt standards.

Add --help or -h to Console App:

Will print the help information:

Add your heading/epilog and command name:

The result:

Override Help Information

If your are using class extending, you may override showHelp() method to add your own help information.

Parameter Configurations

To define parameters, you can use addParameter() method.

The parameter name without - and -- will be treated as argument.

The parameter name starts with - and -- will be treated as options, you can use | to separate primary name and shortcuts.

Arguments' name cannot same as options' name, otherwise it will throw an error.

Get Parameters Value

To get parameters' value, you can use get() method, all values will cast to the type which you defined.

Array access also works:

If a parameter is not provided, it will return FALSE, and if a parameter provided but has no value, it will return as NULL.

So you can easily detect the parameter existence and give a default value.

Parameters Type

You can define the type of parameters, it will auto convert to the type you defined.

Type Argument Option Description
STRING String type String type
INT Integer type Integer type
FLOAT Float type Flot type Can be int or float, will all converts to float
NUMERIC Int or Float Int or Float Can be int or float, will all converts to float
BOOLEAN (X) Add --opt as TRUE Use negatable to supports --opt as TRUE and --no-opt as FALSE
ARRAY Array, must be last argument Array Can provide multiple as string[]
LEVEL (X) Int type Can provide multiple times and convert the times to int

All parameter values parsed from argv is default as string type, and convert to the type you defined.

ARRAY type

The ARRAY can be use to arguments and options.

If you set an argument as ARRAY type, it must be last argument, and you can add more tailing arguments.

Use -- to escape all following options, all will be treated as arguments, it is useful if you are writing a proxy script.

If you set an option as ARRAY type, it can be used as --tag a --tag b --tag c.

LEVEL type

The LEVEL type is a special type, it will convert the times to int. For example, a verbosity level of -vvv will be converted to 3, and -v will be converted to 1. You can use this type to define the verbosity level of your argv parser.

If you are using Console class, the verbosity is built-in option, you don't need to define it again.

Parameters Options

description

required

default

negatable

Parameters Parsing

Simple Console follows docopt style to parse parameters.

Error Handling

Just throw Exception in doExecute(), Console will auto catch error.

If Console app receive an Throwable or Exception, it will render an [ERROR] message:

Add -v to show backtrace if error.

Wrong Parameters

If you provide wrong parameters, Console will render a [WARNING] message with synopsis:

You can manually raise this [WARNING] by throwing InvalidParameterException:

Verbosity

You can set verbosity by option -v

or ser it manually in PHP:

If verbosity is larger than 0, it will show backtrace in exception output.

You can show your own debug information on different verbosity:

The Built-In Options

The --help|-h and --verbosity|-v options are built-in options if you use Console app.

If you parse argv by ArgvParser, you must add it manually. To avoid the required parameters error, you can set validate to false when parsing. Then validate and cast parameters after parsing and help content display.

Disable Built-In Options for Console App

If you don't want to use built-in options for Console App, you can set disableDefaultParameters to true:

Input/Output

STDIN/STDOUT/STDERR

Simple Console supports to read from STDIN and write to STDOUT/STDERR. The default stream can be set at constructor.

If you want to catch the output, you can set stdout to a file pointer or a stream.

Output Methods

To output messages, you can use these methods:

Input and Asking Questions

To input data, you can use in() methods:

Use ask() to ask a question, if return empty string, the default value will instead.

Use askConfirm() to ask a question with Y/n:

To add your boolean mapping, set values to boolMapping

Run Sub-Process

Use exec() to run a sub-process, it will instantly print the output and return the result code of the command.

Use mustExec() to make sure a sub-process should run success, otherwise it will throw an exception.

Hide Command Name

Bt default, exec() and mustExec() will show the command name before executing, for example.

if you want to hide it, set the arg: showCmd to false.

Custom Output

Simple Console use proc_open() to run sub-process, so you can set your own output stream by callback.

Disable the Output

Use false to disable the output, you can get full output from result object after sub-process finished.

Note, the output will only write to result object if output set to false. If you set output as closure or keep default NULL, the output will be empty in result object.

Override exec()

By now, running sub-process by prop_open() is in BETA, if prop_open() not work for your environment, simply override exec() to use PHP system() instead.

Delegating Multiple Tasks

If your script has multiple tasks, for example, the build script contains configure|make|clear etc...

Here is an example to show how to delegate multiple tasks and pass the necessary params to method interface.

Now run:

Contributing and PR is Welcome

I'm apologize that I'm too busy to fix or handle all issues and reports, but pull-request is very welcome and will speed up the fixing process.


All versions of simple-console with dependencies

PHP Build Version
Package Version
Requires php Version >=8.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 asika/simple-console contains the following files

Loading the files please wait ....