malenki/argile

CLI app now easy when using args!

Installs: 320

Dependents: 6

Stars: 3

Watchers: 1

Open Issues: 4

Language: PHP

3.0 2014-02-21 12:55 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

Create, get, write options and autogenerated help messages for your CLI PHP applications…

Basic usage about values and switches

Quick first simple example with only one switch:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();

$opt->newSwitch('switch')
    ->short('s')
    ->help('I am a very simple switch arg with only short form.')
    ;

$opt->parse();

if($opt->has('switch'))
{
    printf("\"switch\" arg selected!\n");
}
exit();

So, if your PHP CLI application is into yourapp executable file, then you can call it like that:

$ yourapp -s
"switch" arg selected!

Without options, calling it will give help message:

$ yourapp
Usage: complex.php [OPTIONS]…

  -s                         I am a very simple switch arg with only short
                             form.


  -h, --help                 Display this help message and exit

Second example with switch, values, short, long and required options:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();

$opt->newSwitch('switch')
    ->short('s')
    ->help('I am a very simple switch arg with only short form.')
    ;
$opt->newValue('foo')
    ->required()
    ->short('f')
    ->long('foo')
    ->help('I am a simple required arg with short and long forms.')
    ;

$opt->parse();

if($opt->has('switch'))
{
    printf("\"switch\" arg selected!\n");
}

if($opt->has('foo'))
{
    printf("\"foo\" arg selected! Its value is: \"%s\"\n", $opt->get('foo'));
}

exit();

Advanced usage of values and switches

You can customize variable name into help text of each option:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();

$opt->newValue('foo')
    ->required()
    ->short('f')
    ->long('foo')
    ->help('I am a simple required value with short and long forms.', 'FILE')
    ;

//...

You can group options together. Easy, you have to create group, and when you create options, you put the group name has second argument:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();

$opt->addGroup('one', 'Optional title for first group');
$opt->addGroup('two', 'Optional title for second group');

$opt->newSwitch('switch', 'one')
    ->short('s')
    ->help('I am a very simple switch arg with only short form.')
    ;
$opt->newValue('foo', 'one')
    ->required()
    ->short('f')
    ->long('foo')
    ->help('I am a simple required arg with short and long forms.')
    ;
$opt->newValue('bar', 'two')
    ->short('b')
    ->long('bar')
    ->help('I am a simple arg with short and long forms.')
    ;

//...

Later, if you can help, options will be grouped and group's name is shown if it has one.

Detect and get arguments

Detecting and getting arguments is simple, just use hasArgument() and getArguments() methods respectively. The second one returns an array.

Meta information about CLI app

You can define some informations about your app that will be used into help message:

  • Synopsis (you can add several synopsis lines, see example files)
  • Version
  • Description

More

You can have "flexible" help output message, that fit your terminal width, or you can leave at default with of 80 columns.

If you want this text adjustment, do just following:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();
$opt->flexible(); // yes, that's all

// and add your options and other thing…

You can apply color and bold on labels and options:

use Malenki\Argile\Options as Options;

$opt = Options::getInstance();
$opt->labelColor('red'); // color for labels
$opt->optColor('red'); // color for options
$opt->bold(); // labels and options becomes bold

// and add your options and other thing…

Colors’ names are the same as foreground colors defined into Ansi project.

To see Argile in action, please look at examples directory and run scripts inside it!

MIT Open Source License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.