本文整理汇总了PHP中getopt函数的典型用法代码示例。如果您正苦于以下问题:PHP getopt函数的具体用法?PHP getopt怎么用?PHP getopt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getopt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getOptions
function getOptions($options)
{
$opt;
$short = '';
$long = array();
$map = array();
foreach ($options as $option) {
$req = isset($option['required']) ? $option['required'] ? ':' : '::' : '';
if (!empty($option['short'])) {
$short .= $option['short'] . $req;
}
if (!empty($option['long'])) {
$long[] = $option['long'] . $req;
}
if (!empty($option['short']) && !empty($option['long'])) {
$map[$option['short']] = $option['long'];
}
}
$arguments = getopt($short, $long);
foreach ($map as $s => $l) {
if (isset($arguments[$s]) && isset($arguments[$l])) {
unset($arguments[$s]);
continue;
}
if (isset($arguments[$s])) {
$arguments[$l] = $arguments[$s];
unset($arguments[$s]);
continue;
}
}
return $arguments;
}
示例2: parseCliArguments
protected function parseCliArguments()
{
// Prepare for getopt(). Note that it supports "require value" for cli args,
// but we use our own format instead (see also php.net/getopt).
$getoptShort = '';
$getoptLong = array();
foreach ($this->flags as $flagKey => $flagInfo) {
switch ($flagInfo['type']) {
case 'value':
$getoptShort .= $flagKey . '::';
break;
case 'boolean':
$getoptShort .= $flagKey;
break;
}
}
foreach ($this->options as $optionName => $optionInfo) {
switch ($optionInfo['type']) {
case 'value':
$getoptLong[] = $optionName . '::';
break;
case 'boolean':
$getoptLong[] = $optionName;
break;
}
}
$parsed = getopt($getoptShort, $getoptLong);
if (!is_array($parsed)) {
$this->error('Parsing command line arguments failed.');
}
$this->parsed = $parsed;
}
示例3: handle
class CLIOptionsHandler
{
/**
* Accept the REPL object and perform any setup necessary from the CLI flags.
*
* @param Boris $boris
*/
public function handle($boris)
{
$args = getopt('hvr:', array('help', 'version', 'require:'));
foreach ($args as $option => $value) {
switch ($option) {
/*
* Sets files to load at startup, may be used multiple times,
* i.e: boris -r test.php,foo/bar.php -r ba/foo.php --require hey.php
*/
case 'r':
case 'require':
$this->_handleRequire($boris, $value);
break;
/*
* Show Usage info
*/
/*
* Show Usage info
*/
示例4: _initOptParams
/**
* Инициализируем переменные с коммандной строки
*
*/
protected function _initOptParams()
{
$arrayInput = getopt(false, array('url:'));
if ($arrayInput && array_key_exists('url', $arrayInput)) {
$_SERVER['REQUEST_URI'] = $arrayInput['url'];
}
}
示例5: get_opts
function get_opts()
{
$short_opts = "u:c:h::";
$long_opts = array("bccwarn:", "bcccrit:", "sirwarn:", "sircrit:", "user:", "pass:");
$options = getopt($short_opts, $long_opts);
return $options;
}
示例6: init
public function init()
{
function addopt($opt)
{
cli::$longopts[] = $opt;
cli::$shortopts = cli::$shortopts . substr($opt, 0, 1);
}
$elems = array("help" => "Display this menu");
foreach (array_merge(data::$elements, $elems) as $name => $element) {
addopt($name);
}
$options = getopt(cli::$shortopts, cli::$longopts);
$realopts = array();
foreach ($options as $name => $option) {
foreach (cli::$longopts as $opt) {
if (substr($name, 0, 1) == substr($opt, 0, 1)) {
$name = $opt;
break;
}
}
$realopts[$name] = $option;
}
cli::$cli_opts = $realopts;
if (in_array("help", array_keys($realopts))) {
foreach (array_merge(data::$elements, $elems) as $name => $element) {
echo $name . "\t\t\t" . $element . PHP_EOL;
}
exit(0);
}
}
示例7: zpa_handle
function zpa_handle()
{
$shortoptions = "w:r:d:li:h:u:p:";
$options = getopt($shortoptions);
$mapi = MAPI_SERVER;
$user = "SYSTEM";
$pass = "";
if (isset($options['h'])) {
$mapi = $options['h'];
}
if (isset($options['u']) && isset($options['p'])) {
$user = $options['u'];
$pass = $options['p'];
}
$zarafaAdmin = zpa_zarafa_admin_setup($mapi, $user, $pass);
if (isset($zarafaAdmin['adminStore']) && isset($options['l'])) {
zpa_get_userlist($zarafaAdmin['adminStore']);
} elseif (isset($zarafaAdmin['adminStore']) && isset($options['d']) && !empty($options['d'])) {
zpa_get_userdetails($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['d']));
} elseif (isset($zarafaAdmin['adminStore']) && isset($options['w']) && !empty($options['w']) && isset($options['i']) && !empty($options['i'])) {
zpa_wipe_device($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['w']), trim($options['i']));
} elseif (isset($zarafaAdmin['adminStore']) && isset($options['r']) && !empty($options['r']) && isset($options['i']) && !empty($options['i'])) {
zpa_remove_device($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['r']), trim($options['i']));
} else {
echo "Usage:\nz-push-admin.sh [actions] [options]\n\nActions: [-l] | [[-d|-w|-r] username]\n\t-l\t\tlist users\n\t-d user\t\tshow user devices\n\t-w user\t\twipe user device, '-i DeviceId' option required\n\t-r user\t\tremove device from list, '-i DeviceId' option required\n\nGlobal options: [-h path] [[-u remoteuser] [-p password]]\n\t-h path\t\tconnect through <path>, e.g. file:///var/run/socket\n\t-u remoteuser\tlogin as remoteuser\n\t-p password\tpassword of the remoteuser\n\n";
}
}
示例8: run
public function run($argc, array $argv)
{
$options = getopt("c::h::", ["clean:", "help::"]);
if (isset($options['h']) || isset($options['help'])) {
self::usage();
return;
}
$cleanOutput = false;
if (isset($options['c']) || isset($options['clean'])) {
$cleanOutput = true;
}
$list = [];
\Phasty\Tman\TaskManager::getInstance()->scanDir(function ($className) use(&$list, $cleanOutput) {
$runTimes = $className::getRunTime();
if (!$runTimes) {
return;
}
$className = \Phasty\Tman\TaskManager::fromClassName(substr($className, strlen($this->cfg["tasksNs"])));
foreach ((array) $runTimes as $args => $runTime) {
if (substr(trim($runTime), 0, 1) === '#' && $cleanOutput) {
continue;
}
$list[] = "{$runTime} " . $this->cfg["tman"] . " run " . "{$className}" . (is_string($args) ? " {$args}" : "");
}
});
if (!empty($list)) {
echo implode(" #tman:" . $this->cfg["tman"] . "\n", $list) . " #tman:" . $this->cfg["tman"] . "\n";
}
}
示例9: handler
/**
* @param array $config
*/
function handler(array $cfg = [])
{
$key = ['listen::', 'indexer::', 'conf::', 'build::', 'status::'];
$opt = getopt('', $key);
$this->cmd = 'listen';
foreach ($key as $v) {
$v = str_replace('::', '', $v);
if (isset($opt[$v])) {
$this->cmd = $v;
$this->args = $opt[$v];
break;
}
}
$this->default_value($cfg, 'redis', 'tcp://127.0.0.1:6379');
$this->default_value($cfg, 'agent_log', __DIR__ . '/agent.log');
$this->default_value($cfg, 'type', 'log');
$this->default_value($cfg, 'input_sync_memory', 5 * 1024 * 1024);
$this->default_value($cfg, 'input_sync_second', 5);
$this->default_value($cfg, 'parser', [$this, 'parser']);
$this->default_value($cfg, 'log_level', 'product');
$this->default_value($cfg, 'elastic', ['http://127.0.0.1:9200']);
$this->default_value($cfg, 'prefix', 'phplogstash');
$this->default_value($cfg, 'shards', 5);
$this->default_value($cfg, 'replicas', 1);
$this->config = $cfg;
$this->redis();
return $this;
}
示例10: getCliParams
/**
* Permet de récupérer les arguments de la console
* Permet surtout de toujours avoir les arguments obligatoire par le système.
*
* @link http://php.net/manual/fr/function.getopt.php
*
* @param string $options : Chaque caractère dans cette chaîne sera utilisé en tant que caractères optionnels et
* devra correspondre aux options passées, commençant par un tiret simple (-).
* Par exemple, une chaîne optionnelle "x" correspondra à l'option -x.
* Seuls a-z, A-Z et 0-9 sont autorisés.
* @param array $longopts : Un tableau d'options. Chaque élément de ce tableau sera utilisé comme option et devra
* correspondre aux options passées, commençant par un tiret double (--).
* Par exemple, un élément longopts "opt" correspondra à l'option --opt.
* Le paramètre options peut contenir les éléments suivants :
* * Caractères individuels (n'accepte pas de valeur)
* * Caractères suivis par un deux-points (le paramètre nécessite une valeur)
* * Caractères suivis par deux deux-points (valeur optionnelle)
* Les valeurs optionnelles sont les premiers arguments après la chaîne.
* Si une valeur est requise, peu importe que la valeur soit suivi d'un espace ou non.
*
* @return array
*/
function getCliParams($options, $longopts = array())
{
$longopts = array_merge($longopts, array('type_site::'));
$opt = getopt('f:' . $options, $longopts);
unset($opt['f']);
return $opt;
}
示例11: init
/** 初始化 */
private function init()
{
$this->is_win = strstr(PHP_OS, 'WIN') ? true : false;
$this->is_cli = PHP_SAPI == 'cli' ? true : false;
$this->is_cgi = 0 === strpos(PHP_SAPI, 'cgi') || false !== strpos(PHP_SAPI, 'fcgi') ? true : false;
if (!$this->is_cli) {
$this->http_host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : '';
$this->request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$pos = strpos($this->request_uri, '?');
$this->request_path = $pos === false ? $this->request_uri : substr($this->request_uri, 0, $pos);
$this->query_string = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
$this->request_method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : '';
$this->is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || !empty($_POST[$this->conf['PARAMS_AJAX_SUBMIT']]) || !empty($_GET[$this->conf['PARAMS_AJAX_SUBMIT']]) ? true : false;
$this->is_get = $this->request_method === 'GET' ? true : false;
$this->is_post = $this->request_method === 'POST' ? true : false;
$this->is_put = $this->request_method === 'PUT' ? true : false;
$this->is_delete = $this->request_method === 'DELETE' ? true : false;
} else {
$opt = getopt('r:', [$this->conf['URL_REQUEST_URI'] . ':']);
$this->request_uri = !empty($opt['r']) ? $opt['r'] : (!empty($opt[$this->conf['URL_REQUEST_URI']]) ? $opt[$this->conf['URL_REQUEST_URI']] : '');
$pos = strpos($this->request_uri, '?');
$this->request_path = $pos === false ? $this->request_uri : substr($this->request_uri, 0, $pos);
$this->query_string = $pos === false ? '' : substr($this->request_uri, $pos + 1);
}
}
示例12: handleArguments
private function handleArguments()
{
$options = getopt('D::H::E::I::A', array('debug::', 'help::', 'exclude::', 'include::', 'all::'));
foreach ($options as $option => $value) {
switch ($option) {
case 'D':
case 'debug':
Utils::log('Debug mode enabled');
$this->debugMode = true;
break;
case 'H':
case 'help':
Utils::help();
exit;
case 'E':
case 'exclude':
$this->insertSpecification($value);
break;
case 'I':
case 'include':
$this->insertSpecification($value, false);
break;
case 'A':
case 'all':
Utils::logf('Possible units are: %s', implode(',', $this->units));
exit;
}
}
$this->validateUnits();
}
示例13: readConfig
/**
* @param $configFilename
* @return FileWatcher
*/
public function readConfig($configFilename)
{
include $configFilename;
$this->_config = $config;
$this->_log('Config file loaded');
if (php_sapi_name() == "cli") {
$longopts = array('password::', 'overallHash::');
$options = getopt('', $longopts);
if (isset($options['password'])) {
$this->_providedPassword = $options['password'];
}
if (isset($options['overallHash'])) {
$this->_providedOverallHash = $options['overallHash'];
}
} else {
// not in cli-mode, try to get password from $_GET or $_POST
if (isset($_POST['password'])) {
$this->_providedPassword = $_POST['password'];
} elseif ($_GET['password']) {
$this->_providedPassword = $_GET['password'];
}
if (isset($_POST['overallHash'])) {
$this->_providedOverallHash = $_POST['overallHash'];
} elseif ($_GET['overallHash']) {
$this->_providedOverallHash = $_GET['overallHash'];
}
}
return $this;
}
示例14: __construct
function __construct($options)
{
$this->reset($options);
pcntl_signal(SIGTERM, array("JAXLHTTPd", "shutdown"));
pcntl_signal(SIGINT, array("JAXLHTTPd", "shutdown"));
$options = getopt("p:b:");
foreach ($options as $opt => $val) {
switch ($opt) {
case 'p':
$this->settings['port'] = $val;
break;
case 'b':
$this->settings['maxq'] = $val;
default:
break;
}
}
$this->httpd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($this->httpd, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($this->httpd, 0, $this->settings['port']);
socket_listen($this->httpd, $this->settings['maxq']);
$this->id = $this->getResourceID($this->httpd);
$this->clients = array("0#" . $this->settings['port'] => $this->httpd);
echo "JAXLHTTPd listening on port " . $this->settings['port'] . PHP_EOL;
}
示例15: run
public function run()
{
$this->buffer = new \Threaded();
$opts = getopt("", ["disable-readline"]);
if (extension_loaded("readline") and $this->stream === "php://stdin" and !isset($opts["disable-readline"])) {
$this->readline = true;
} else {
$this->readline = false;
$this->fp = fopen($this->stream, "r");
stream_set_blocking($this->fp, 1);
//Non-blocking STDIN won't work on Windows
}
$lastLine = microtime(true);
while (true) {
if (($line = $this->readLine()) !== "") {
$this->buffer->synchronized(function (\Threaded $buffer, $line) {
$buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line);
}, $this->buffer, $line);
$lastLine = microtime(true);
} elseif (microtime(true) - $lastLine <= 0.1) {
//Non blocking! Sleep to save CPU
usleep(40000);
}
}
}