diff --git a/mirror b/mirror index 92ab9c6..aeabb21 100755 --- a/mirror +++ b/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']); diff --git a/src/CommandLineOptions.php b/src/CommandLineOptions.php new file mode 100644 index 0000000..c105c80 --- /dev/null +++ b/src/CommandLineOptions.php @@ -0,0 +1,51 @@ +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); + } +} diff --git a/update-doc-list b/update-doc-list index 9c16883..6b9fce4 100755 --- a/update-doc-list +++ b/update-doc-list @@ -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']); diff --git a/update-super-project b/update-super-project index 0520da6..409d8c6 100755 --- a/update-super-project +++ b/update-super-project @@ -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']);