Extract the common command line code into a class.

Should possible use it for everything, especially if I add more
functionality.
This commit is contained in:
Daniel James
2015-10-10 12:31:53 +01:00
parent 06c69934bc
commit 29dedb001e
4 changed files with 60 additions and 79 deletions

30
mirror
View File

@@ -3,33 +3,16 @@
require_once(__DIR__.'/vendor/autoload.php');
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
use GetOptionKit\Exception\InvalidOptionException;
function main($args) {
$specs = new OptionCollection;
$specs->add('help', "Diplay command line usage.")
->defaultValue(false);
$specs->add('no-fetch', "Don't fetch events from GitHub")
->defaultValue(false);
$specs->add('all', "Update all repos in mirror")
->defaultValue(false);
try {
$parser = new OptionParser($specs);
$options = $parser->parse($args)->toArray();
} catch (InvalidOptionException $e) {
usage($specs, $e);
exit(1);
}
if ($options['help']) {
usage($specs);
exit(0);
}
// Update the mirror.
$options = CommandLineOptions::process($args,
'Creates or updates the GitHub mirror',
$specs);
if (!$options['no-fetch']) {
GitHubEventQueue::downloadEvents();
@@ -44,11 +27,4 @@ function main($args) {
$mirror->fetchDirty();
}
function usage($specs, $message = null) {
if ($message) { echo "{$message}\n\n"; }
echo "Usage:\n";
$printer = new ConsoleOptionPrinter();
echo $printer->render($specs);
}
main($_SERVER['argv']);

View File

@@ -0,0 +1,51 @@
<?php
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
use GetOptionKit\Exception\InvalidOptionException;
// Very basic command line options handling thing.
class CommandLineOptions
{
var $args;
var $description;
var $specs;
function __construct($args, $description, $specs) {
$this->args = $args;
$this->description = $description;
$this->specs = $specs;
}
static function process($args, $description, $specs = null) {
if (!$specs) { $specs = new OptionCollection(); }
$specs->add('help', "Diplay command line usage.")
->defaultValue(false);
$x = new CommandLineOptions($args, $description, $specs);
try {
$parser = new OptionParser($specs);
$options = $parser->parse($args)->toArray();
} catch (InvalidOptionException $e) {
$x->usage($e);
exit(1);
}
if ($options['help']) {
$x->usage();
exit(0);
}
return $options;
}
function usage($message = null) {
if ($message) { echo "{$message}\n\n"; }
echo "{$this->description}\n\n";
echo "Usage:\n";
$printer = new ConsoleOptionPrinter();
echo $printer->render($this->specs);
}
}

View File

@@ -3,28 +3,13 @@
require_once(__DIR__.'/vendor/autoload.php');
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
use GetOptionKit\Exception\InvalidOptionException;
function main($args) {
$specs = new OptionCollection;
$specs->add('help', "Diplay command line usage.")
->defaultValue(false);
$specs->add('version:', "Version of update (e.g. develop, 1.57.0)");
try {
$parser = new OptionParser($specs);
$options = $parser->parse($args)->toArray();
} catch (InvalidOptionException $e) {
usage($specs, $e);
exit(1);
}
if ($options['help']) {
usage($specs);
exit(0);
}
$options = CommandLineOptions::process($args,
'Update the documentation list',
$specs);
// TODO: For some reason 'hasArgument' is always true, even when
// there isn't a version. Am I misunderstanding it? Doesn't really
@@ -52,11 +37,4 @@ function main($args) {
}
}
function usage($specs, $message = null) {
if ($message) { echo "{$message}\n\n"; }
echo "Usage:\n";
$printer = new ConsoleOptionPrinter();
echo $printer->render($specs);
}
main($_SERVER['argv']);

View File

@@ -3,31 +3,14 @@
require_once(__DIR__.'/vendor/autoload.php');
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
use GetOptionKit\Exception\InvalidOptionException;
function main($args) {
$specs = new OptionCollection;
$specs->add('help', "Diplay command line usage.")
->defaultValue(false);
$specs->add('no-fetch', "Don't fetch events from GitHub")
->defaultValue(false);
try {
$parser = new OptionParser($specs);
$options = $parser->parse($args)->toArray();
} catch (InvalidOptionException $e) {
usage($specs, $e);
exit(1);
}
if ($options['help']) {
usage($specs);
exit(0);
}
// Update the mirror.
$options = CommandLineOptions::process($args,
'Update the submodules in the super project',
$specs);
if (!$options['no-fetch']) {
GitHubEventQueue::downloadEvents();
@@ -36,11 +19,4 @@ function main($args) {
SuperProject::updateBranches();
}
function usage($specs, $message = null) {
if ($message) { echo "{$message}\n\n"; }
echo "Usage:\n";
$printer = new ConsoleOptionPrinter();
echo $printer->render($specs);
}
main($_SERVER['argv']);