public function execute(PhutilArgumentParser $args) { $with = $args->getArg('help-with-what'); if (!$with) { $args->printHelpAndExit(); } else { foreach ($with as $thing) { echo phutil_console_format("**%s**\n\n", pht('%s WORKFLOW', strtoupper($thing))); echo $args->renderWorkflowHelp($thing, $show_flags = true); echo "\n"; } exit(PhutilArgumentParser::PARSE_ERROR_CODE); } }
<?php $root = dirname(dirname(dirname(__FILE__))); require_once $root . '/scripts/__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->setTagline('test InteractiveEditor class'); $args->setSynopsis(<<<EOHELP **interactive_editor.php** [__options__] Edit some content via the InteractiveEditor class. This script makes it easier to test changes to InteractiveEditor, which is difficult to unit test. EOHELP ); $args->parseStandardArguments(); $args->parse(array(array('name' => 'fallback', 'param' => 'editor', 'help' => 'Set the fallback editor.'), array('name' => 'line', 'short' => 'l', 'param' => 'number', 'help' => 'Open at line number __number__.'), array('name' => 'name', 'param' => 'filename', 'help' => 'Set edited file name.'))); if ($args->getArg('help')) { $args->printHelpAndExit(); } $editor = new PhutilInteractiveEditor("The wizard quickly\n" . "jinxed the gnomes\n" . "before they vaporized."); $name = $args->getArg('name'); if ($name) { $editor->setName($name); } $line = $args->getArg('line'); if ($line) { $editor->setLineOffset($line); } $fallback = $args->getArg('fallback'); if ($fallback) { $editor->setFallbackEditor($fallback); } $result = $editor->editInteractively();
public function __construct(array $argv) { PhutilServiceProfiler::getInstance()->enableDiscardMode(); $original_argv = $argv; $args = new PhutilArgumentParser($argv); $args->setTagline('daemon overseer'); $args->setSynopsis(<<<EOHELP **launch_daemon.php** [__options__] __daemon__ Launch and oversee an instance of __daemon__. EOHELP ); $args->parseStandardArguments(); $args->parsePartial(array(array('name' => 'trace-memory', 'help' => 'Enable debug memory tracing.'), array('name' => 'log', 'param' => 'file', 'help' => 'Send output to __file__.'), array('name' => 'daemonize', 'help' => 'Run in the background.'), array('name' => 'phd', 'param' => 'dir', 'help' => 'Write PID information to __dir__.'), array('name' => 'verbose', 'help' => 'Enable verbose activity logging.'), array('name' => 'load-phutil-library', 'param' => 'library', 'repeat' => true, 'help' => 'Load __library__.'))); $argv = array(); $more = $args->getUnconsumedArgumentVector(); $this->daemon = array_shift($more); if (!$this->daemon) { $args->printHelpAndExit(); } if ($args->getArg('trace')) { $this->traceMode = true; $argv[] = '--trace'; } if ($args->getArg('trace-memory')) { $this->traceMode = true; $this->traceMemory = true; $argv[] = '--trace-memory'; } if ($args->getArg('load-phutil-library')) { foreach ($args->getArg('load-phutil-library') as $library) { $argv[] = '--load-phutil-library=' . $library; } } $log = $args->getArg('log'); if ($log) { ini_set('error_log', $log); $argv[] = '--log=' . $log; } $verbose = $args->getArg('verbose'); if ($verbose) { $this->verbose = true; $argv[] = '--verbose'; } $this->daemonize = $args->getArg('daemonize'); $this->phddir = $args->getArg('phd'); $this->argv = $argv; $this->moreArgs = coalesce($more, array()); error_log("Bringing daemon '{$this->daemon}' online..."); if (self::$instance) { throw new Exception('You may not instantiate more than one Overseer per process.'); } self::$instance = $this; if ($this->daemonize) { // We need to get rid of these or the daemon will hang when we TERM it // waiting for something to read the buffers. TODO: Learn how unix works. fclose(STDOUT); fclose(STDERR); ob_start(); $pid = pcntl_fork(); if ($pid === -1) { throw new Exception('Unable to fork!'); } else { if ($pid) { exit(0); } } } if ($this->phddir) { $desc = array('name' => $this->daemon, 'argv' => $this->moreArgs, 'pid' => getmypid(), 'start' => time()); Filesystem::writeFile($this->phddir . '/daemon.' . getmypid(), json_encode($desc)); } $this->daemonID = $this->generateDaemonID(); $this->dispatchEvent(self::EVENT_DID_LAUNCH, array('argv' => array_slice($original_argv, 1), 'explicitArgv' => $this->moreArgs)); declare (ticks=1); pcntl_signal(SIGUSR1, array($this, 'didReceiveKeepaliveSignal')); pcntl_signal(SIGUSR2, array($this, 'didReceiveNotifySignal')); pcntl_signal(SIGINT, array($this, 'didReceiveGracefulSignal')); pcntl_signal(SIGTERM, array($this, 'didReceiveTerminalSignal')); }
public function __construct(array $argv) { PhutilServiceProfiler::getInstance()->enableDiscardMode(); $original_argv = $argv; $args = new PhutilArgumentParser($argv); $args->setTagline('daemon overseer'); $args->setSynopsis(<<<EOHELP **launch_daemon.php** [__options__] __daemon__ Launch and oversee an instance of __daemon__. EOHELP ); $args->parsePartial(array(array('name' => 'trace', 'help' => 'Enable debug tracing.'), array('name' => 'trace-memory', 'help' => 'Enable debug memory tracing.'), array('name' => 'log', 'param' => 'file', 'help' => 'Send output to __file__.'), array('name' => 'daemonize', 'help' => 'Run in the background.'), array('name' => 'phd', 'param' => 'dir', 'help' => 'Write PID information to __dir__.'), array('name' => 'conduit-uri', 'param' => 'uri', 'help' => 'Send logs to Conduit on __uri__.'), array('name' => 'verbose', 'help' => 'Enable verbose activity logging.'))); $argv = $args->getUnconsumedArgumentVector(); $this->daemon = array_shift($argv); if (!$this->daemon) { $args->printHelpAndExit(); } if ($args->getArg('trace')) { $this->traceMode = true; array_unshift($argv, '--trace'); } if ($args->getArg('trace-memory')) { $this->traceMode = true; $this->traceMemory = true; array_unshift($argv, '--trace-memory'); } $log = $args->getArg('log'); if ($log) { ini_set('error_log', $log); array_unshift($argv, '--log=' . $log); } $verbose = $args->getArg('verbose'); if ($verbose) { $this->verbose = true; array_unshift($argv, '--verbose'); } $this->daemonize = $args->getArg('daemonize'); $this->phddir = $args->getArg('phd'); $this->conduitURI = $args->getArg('conduit-uri'); $this->argv = $argv; error_log("Bringing daemon '{$this->daemon}' online..."); if (self::$instance) { throw new Exception("You may not instantiate more than one Overseer per process."); } self::$instance = $this; if ($this->daemonize) { // We need to get rid of these or the daemon will hang when we TERM it // waiting for something to read the buffers. TODO: Learn how unix works. fclose(STDOUT); fclose(STDERR); ob_start(); $pid = pcntl_fork(); if ($pid === -1) { throw new Exception("Unable to fork!"); } else { if ($pid) { exit(0); } } } if ($this->phddir) { $desc = array('name' => $this->daemon, 'pid' => getmypid(), 'start' => time()); Filesystem::writeFile($this->phddir . '/daemon.' . getmypid(), json_encode($desc)); } if ($this->conduitURI) { $this->conduit = new ConduitClient($this->conduitURI); $this->daemonLogID = $this->conduit->callMethodSynchronous('daemon.launched', array('daemon' => $this->daemon, 'host' => php_uname('n'), 'pid' => getmypid(), 'argv' => json_encode(array_slice($original_argv, 1)))); } declare (ticks=1); pcntl_signal(SIGUSR1, array($this, 'didReceiveKeepaliveSignal')); pcntl_signal(SIGINT, array($this, 'didReceiveTerminalSignal')); pcntl_signal(SIGTERM, array($this, 'didReceiveTerminalSignal')); }