You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Julien Rosset 7351f90e28 V2 4 years ago
Tests Suppression support PHP 5 (min PHP 7.4) 4 years ago
src/CommandLine V2 4 years ago
.gitignore Configuring Composer & using autoloader 6 years ago
LICENSE.md Initial commit 6 years ago
README.md Déplacement vers namespace global "jrosset" + support PHP 8 4 years ago
composer.json V2 4 years ago
composer.lock Suppression support PHP 5 (min PHP 7.4) 4 years ago

README.md

PHPCommandLine

Command line management library in PHP (CLI)

last release licence nb releases

A command-line options parser with configurable expects and help auto generation.

There is two kind of arguments :

  • options (short/long tag) : -h or --version
  • values : my/path/to/a/file

Installation

composer require jrosset/commandline

Description

At first, create a new CommandLine with program name and description. Add options and values by using respectively addOption and addValue. Default options for help (-h / --help) and version (--version) can be add with addDefaultArguments and use treatDefaultArguments for launching associated treatments.

Treat script arguments with parse : return an object of variables.

Example

<?php

use jrosset\CommandLine\CommandLine;
use jrosset\CommandLine\Argument\Option\Flag;
use jrosset\CommandLine\Argument\Parser\StringParser;
use jrosset\CommandLine\Argument\Value\Value;

$cmdline = new CommandLine('Checker', 'File checker', 'php checker.php');
$cmdline->addDefaultArguments();

$cmdline->addOption(new Flag('enhanced', false, 'Deep check ?'));

$cmdline->addValue(new Value('path', 'File path', new StringParser()));

var_dump($args = $cmdline->parse());
$cmdline->treatDefaultArguments($args);

Would display something like that :

class stdClass#13 (1) {
  public $enhanced => bool(false)
  public $path     => string("my/path/to/a/file")
}

Automatic help generation

CommandLine provide a generator for command-line of programme. Example :

Checker
File checker

php checker.php [OPTIONS] path
Arguments :
    path    string    File path

Options :
        -h --help       X     Affiche cette aide
           --enhanced         Deep check ?