Skip to content

Commit

Permalink
Start of a CLI application skeleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed May 22, 2017
1 parent 5b003f5 commit 8114281
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 0 deletions.
8 changes: 8 additions & 0 deletions framework/Cli_Application/.horde.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
id: Cli_Application
name: Cli_Application
full: Library for creating command line applications
description: >
A base class with supporting classes that builds a complete command line
application.
list: dev
24 changes: 24 additions & 0 deletions framework/Cli_Application/doc/Horde/Cli/Application/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright 2017 Horde LLC. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HORDE PROJECT
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Example for an encapsulated CLI application.
*/

use Horde\Cli\Application;

/**
* Application class.
*/
class MyApplication extends Application
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(
null,
array(
'epilog' => 'Please report any bugs to https://bugs.horde.org.',
'description' => 'This is a Horde CLI application',
'version' => '%prog 1.0.0',
'optionList' => array(
new Horde_Argv_Option(
'-f', '--foo',
array(
'action' => 'store_const',
'const' => 42,
'dest' => 'bar',
'help' => 'Enable bar.',
)
),
new Horde_Argv_Option(
'-i', '--int',
array(
'action' => 'store',
'type' => 'int',
'help' => 'An integer.',
)
)
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env php
<?php
/**
* This is a CLI application example that keeps all application logic in a
* separate class.
*/

/* We need some autoloader, use Horde's by default. */
require 'Horde/Autoloader/Default.php';

require __DIR__ . '/MyApplication.php';

$app = new MyApplication();
$app->run();

$app->cli->writeln($app->values->int);
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env php
<?php
/**
* This is a CLI application example that runs on a configured Horde
* installation.
*/

/* Load the Horde bootstrapping code from a PEAR installation. */
require_once 'PEAR/Config.php';
require_once PEAR_Config::singleton()
->get('horde_dir', null, 'pear.horde.org') . '/lib/Application.php';

Horde_Registry::appInit(
/* The application to initialize. */
'horde',
array(
/* Always true. */
'cli' => true,
/* Don't authenticate. Leave out if using user_admin below. */
'authentication' => 'none',
/* Authenticate as an administrator. Optional. */
'user_admin' => false,
)
);

use Horde\Cli\Application;

$app = new Application(
$cli,
array(
'epilog' => 'Please report any bugs to https://bugs.horde.org.',
'description' => 'This is a Horde CLI application',
'version' => '%prog 1.0.0'
)
);
$app->addOption(
'-f', '--foo',
array(
'action' => 'store_const',
'const' => 42,
'dest' => 'bar',
'help' => 'Enable bar.',
)
);
$app->addOption(
'-i', '--int',
array(
'action' => 'store',
'type' => 'int',
'help' => 'An integer.',
)
);
$app->run();

$app->cli->message($app->values->bar, 'cli.success');
$app->cli->writeln($app->values->int);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php
/**
* This is a simple CLI application example that doesn't require a Horde
* installation.
*
* See example-horde for a more complex example.
*/

/* We need some autoloader, use Horde's by default. */
require 'Horde/Autoloader/Default.php';

use Horde\Cli\Application;

$app = new Application();
$app->addOption('-f', '--foo');
$app->run();

$app->cli->writeln($app->values->foo);
148 changes: 148 additions & 0 deletions framework/Cli_Application/lib/Horde/Cli/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Cli_Application
*/

namespace Horde\Cli;

use Horde_Argv_Parser as Parser;
use Horde_Cli as Cli;
use Horde\Cli\Application\Exception;
use Horde\Cli\Application\Translation;

/**
* This class implements a complete command line application.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Cli_Application
*
* @property-read array $arguments Additional arguments.
* @property-read Horde_Cli $cli CLI helper.
* @property Horde_Argv_Parser $parser Argument parser.
* @property-read Horde_Argv_Values $values Argument option values.
*/
class Application
{
/**
* @var Horde_Cli
*/
protected $_cli;

/**
* @var Horde_Argv_Parser
*/
protected $_parser;

/**
* @var Horde_Argv_Values
*/
protected $_values;

/**
* @var array
*/
protected $_arguments;

/**
* Constructor.
*
* @param Horde_Cli $cli A Horde_Cli instance.
* @param array $parserArgs Any additional arguments for
* Horde_Argv_Parser, e.g. 'usage', 'version',
* 'description', 'epilog'.
*/
public function __construct(Cli $cli = null, array $parserArgs = array())
{
if ($cli) {
$this->_cli = $cli;
} else {
/* Make sure no one runs from the web. */
if (!Cli::runningFromCLI()) {
throw new Exception(
Translation::t("Script must be run from the command line")
);
}

/* Load the CLI environment - make sure there's no time limit, init
* some variables, etc. */
$this->_cli = Cli::init();
}

$this->_parser = new Parser($parserArgs);
}

/**
* Getter.
*
* @param string $property Property to return.
*/
public function __get($property)
{
switch ($property) {
case 'arguments':
case 'cli':
case 'parser':
case 'values':
return $this->{'_' . $property};
}
}

/**
* Setter.
*
* @param string $property Property to set.
* @param mixed $value Value to set.
*/
public function __set($property, $value)
{
switch ($property) {
case 'parser':
if (!($value instanceof Horde_Argv_Parser)) {
throw new InvalidArgumentException();
}
$this->_parser = $value;
}
}

/**
* Runs the application.
*/
public function run()
{
list($this->_values, $this->_arguments) = $this->_parser->parseArgs();
}

/**
* Overloaded method to add an argument option.
*
* <code>
* addOption(Horde_Argv_Option $option);
* addOption(string $option[, string $option...][, array $attributes]);
* </code>
* - string $option could be any number of short (-v) or long (--verbose)
* options.
* - $attributes is a hash of option attributes. See
* https://wiki.horde.org/Doc/Dev/HordeArgv for details.
*
* @param Horde_Argv_Option
* @return Horde_Argv_Option
*/
public function addOption()
{
return call_user_func_array(
array($this->_parser, 'addOption'),
func_get_args()
);
}
}
Loading

0 comments on commit 8114281

Please sign in to comment.