From b0b7909bdd454e9614f4ffe34f384b0da0ce4585 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 20 Mar 2014 20:55:57 +0100 Subject: converted some more CLI tools, minor CLI class updates --- bin/gittool.php | 10 +--- bin/indexer.php | 176 ++++++++++++++++++++++++++++---------------------------- bin/render.php | 92 ++++++++++++++--------------- inc/cli.php | 25 +++++--- 4 files changed, 152 insertions(+), 151 deletions(-) diff --git a/bin/gittool.php b/bin/gittool.php index cb4ef3484..fca76768d 100755 --- a/bin/gittool.php +++ b/bin/gittool.php @@ -1,13 +1,9 @@ #!/usr/bin/php run(); \ No newline at end of file +// Main +$cli = new GitToolCLI(); +$cli->run(); \ No newline at end of file diff --git a/bin/indexer.php b/bin/indexer.php index 6f6b5d9fa..76269f95a 100755 --- a/bin/indexer.php +++ b/bin/indexer.php @@ -1,98 +1,100 @@ #!/usr/bin/php isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$CLEAR = false; -$QUIET = false; -$INDEXER = null; -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'c': - case 'clear': - $CLEAR = true; - break; - case 'q': - case 'quiet': - $QUIET = true; - break; +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/'); +define('NOSESSION', 1); +require_once(DOKU_INC . 'inc/init.php'); + +class IndexerCLI extends DokuCLI { + + private $quiet = false; + private $clear = false; + + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . + 'given the index is cleared first.' + ); + + $options->registerOption( + 'clear', + 'clear the index before updating', + 'c' + ); + $options->registerOption( + 'quiet', + 'don\'t produce any output', + 'q' + ); } -} - -#------------------------------------------------------------------------------ -# Action - -if($CLEAR) _clearindex(); -_update(); - - - -#------------------------------------------------------------------------------ - -function _usage() { - print "Usage: indexer.php - - Updates the searchindex by indexing all new or changed pages - when the -c option is given the index is cleared first. - OPTIONS - -h, --help show this help and exit - -c, --clear clear the index before updating - -q, --quiet don't produce any output -"; -} - -function _update(){ - global $conf; - $data = array(); - _quietecho("Searching pages... "); - search($data,$conf['datadir'],'search_allpages',array('skipacl' => true)); - _quietecho(count($data)." pages found.\n"); - - foreach($data as $val){ - _index($val['id']); + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param DokuCLI_Options $options + * @return void + */ + protected function main(DokuCLI_Options $options) { + $this->clear = $options->getOpt('clear'); + $this->quiet = $options->getOpt('quiet'); + + if($this->clear) $this->clearindex(); + + $this->update(); } -} -function _index($id){ - global $CLEAR; - global $QUIET; + /** + * Update the index + */ + function update() { + global $conf; + $data = array(); + $this->quietecho("Searching pages... "); + search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); + $this->quietecho(count($data) . " pages found.\n"); + + foreach($data as $val) { + $this->index($val['id']); + } + } - _quietecho("$id... "); - idx_addPage($id, !$QUIET, $CLEAR); - _quietecho("done.\n"); -} + /** + * Index the given page + * + * @param string $id + */ + function index($id) { + $this->quietecho("$id... "); + idx_addPage($id, !$this->quiet, $this->clear); + $this->quietecho("done.\n"); + } -/** - * Clear all index files - */ -function _clearindex(){ - _quietecho("Clearing index... "); - idx_get_indexer()->clear(); - _quietecho("done.\n"); -} + /** + * Clear all index files + */ + function clearindex() { + $this->quietecho("Clearing index... "); + idx_get_indexer()->clear(); + $this->quietecho("done.\n"); + } -function _quietecho($msg) { - global $QUIET; - if(!$QUIET) echo $msg; + /** + * Print message if not supressed + * + * @param string $msg + */ + function quietecho($msg) { + if(!$this->quiet) echo $msg; + } } -//Setup VIM: ex: et ts=2 : +// Main +$cli = new IndexerCLI(); +$cli->run(); \ No newline at end of file diff --git a/bin/render.php b/bin/render.php index d30ef2958..01fd16cdd 100755 --- a/bin/render.php +++ b/bin/render.php @@ -1,5 +1,10 @@ #!/usr/bin/php */ -if ('cli' != php_sapi_name()) die(); - -ini_set('memory_limit','128M'); -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); -define('NOSESSION',1); -require_once(DOKU_INC.'inc/init.php'); -require_once(DOKU_INC.'inc/common.php'); -require_once(DOKU_INC.'inc/parserutils.php'); -require_once(DOKU_INC.'inc/cliopts.php'); +class RenderCLI extends DokuCLI { -// handle options -$short_opts = 'hr:'; -$long_opts = array('help','renderer:'); -$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); -if ( $OPTS->isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$RENDERER = 'xhtml'; -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'r': - case 'renderer': - $RENDERER = $val; + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'A simple commandline tool to render some DokuWiki syntax with a given renderer.' . + "\n\n" . + 'This may not work for plugins that expect a certain environment to be ' . + 'set up before rendering, but should work for most or even all standard ' . + 'DokuWiki markup' + ); + $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode'); } -} - -// do the action -$source = stream_get_contents(STDIN); -$info = array(); -$result = p_render($RENDERER,p_get_instructions($source),$info); -if(is_null($result)) die("No such renderer $RENDERER\n"); -echo $result; + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param DokuCLI_Options $options + * @throws DokuCLI_Exception + * @return void + */ + protected function main(DokuCLI_Options $options) { + $renderer = $options->getOpt('renderer', 'xhtml'); -/** - * Print usage info - */ -function _usage(){ - print "Usage: render.php - - Reads DokuWiki syntax from STDIN and renders it with the given renderer - to STDOUT - - OPTIONS - -h, --help show this help and exit - -r, --renderer the render mode (default: xhtml) -"; + // do the action + $source = stream_get_contents(STDIN); + $info = array(); + $result = p_render($renderer, p_get_instructions($source), $info); + if(is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer"); + echo $result; + } } + +// Main +$cli = new RenderCLI(); +$cli->run(); \ No newline at end of file diff --git a/inc/cli.php b/inc/cli.php index 84aad3411..c51d9b7d2 100644 --- a/inc/cli.php +++ b/inc/cli.php @@ -323,7 +323,7 @@ class DokuCLI_Options { * @param string $long multi character option (specified with --) * @param string $help help text for this option * @param string|null $short one character option (specified with -) - * @param bool $needsarg does this option require an argument? + * @param bool|string $needsarg does this option require an argument? give it a name here * @param string $command what command does this option apply to * @throws DokuCLI_Exception */ @@ -465,12 +465,13 @@ class DokuCLI_Options { * * Can only be used after parseOptions() has been run * - * @param $option + * @param string $option + * @param mixed $default what to return if the option was not set * @return mixed */ - public function getOpt($option) { + public function getOpt($option, $default = false) { if(isset($this->options[$option])) return $this->options[$option]; - return false; + return $default; } /** @@ -501,6 +502,8 @@ class DokuCLI_Options { $text .= "\n$command"; } + if($hasopts) $text .= ' '; + foreach($this->setup[$command]['args'] as $arg) { if($arg['required']) { $text .= ' <' . $arg['name'] . '>'; @@ -519,17 +522,23 @@ class DokuCLI_Options { } if($hasopts) { - $text .= "\n"; + $text .= "\n OPTIONS\n\n"; foreach($this->setup[$command]['opts'] as $long => $opt) { - $name = "--$long"; - if($opt['short']) $name = '-' . $opt['short'] . ' ' . $name; - if($opt['needsarg']) $name .= ' '; + $name = ''; + if($opt['short']) { + $name .= '-' . $opt['short']; + if($opt['needsarg']) $name .= ' <'.$opt['needsarg'].'>'; + $name .= ', '; + } + $name .= "--$long"; + if($opt['needsarg']) $name .= ' <'.$opt['needsarg'].'>'; $text .= $this->tableFormat( array(2, 20, 52), array('', $name, $opt['help']) ); + $text .= "\n"; } } -- cgit v1.2.3