mirror of
https://github.com/boostorg/boost-tasks.git
synced 2026-02-01 08:22:13 +00:00
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:
30
mirror
30
mirror
@@ -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']);
|
||||
|
||||
51
src/CommandLineOptions.php
Normal file
51
src/CommandLineOptions.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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']);
|
||||
|
||||
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user