發表新文章 回覆主題  [ 9 篇文章 ] 

討論區首頁 : 資訊專區 : PHP程式設計

發表人 內容
 文章主題 : function()的程式應用
文章發表於 : 2011年 12月 4日, 08:56 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
When you want to get inputs from STDIN, you may use this function if you like using C coding style.

<?php
// up to 8 variables

function scanf($format, &$a0=NULL, &$a1=NULL, &$a2=NULL, &$a3=NULL,
&$a4=NULL, &$a5=NULL, &$a6=NULL, &$a7=NULL)
{
$num_args = func_num_args();
if($num_args > 1) {
$inputs = fscanf(STDIN, $format);
for($i=0; $i<$num_args-1; $i++) {
$arg = 'a'.$i;
$$arg = $inputs[$i];
}
}
}

scanf("%d", $number);

?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 08:58 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
This command line option parser supports any combination of three types of options (switches, flags and arguments) and returns a simple array.

[pfisher ~]$ php test.php --foo --bar=baz
["foo"] => true
["bar"] => "baz"

[pfisher ~]$ php test.php -abc
["a"] => true
["b"] => true
["c"] => true

[pfisher ~]$ php test.php arg1 arg2 arg3
[0] => "arg1"
[1] => "arg2"
[2] => "arg3"

<?php
function parseArgs($argv){
array_shift($argv);
$out = array();
foreach ($argv as $arg){
if (substr($arg,0,2) == '--'){
$eqPos = strpos($arg,'=');
if ($eqPos === false){
$key = substr($arg,2);
$out[$key] = isset($out[$key]) ? $out[$key] : true;
} else {
$key = substr($arg,2,$eqPos-2);
$out[$key] = substr($arg,$eqPos+1);
}
} else if (substr($arg,0,1) == '-'){
if (substr($arg,2,1) == '='){
$key = substr($arg,1,1);
$out[$key] = substr($arg,3);
} else {
$chars = str_split(substr($arg,1));
foreach ($chars as $char){
$key = $char;
$out[$key] = isset($out[$key]) ? $out[$key] : true;
}
}
} else {
$out[] = $arg;
}
}
return $out;
}
?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 08:59 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
Here's my modification of "thomas dot harding at laposte dot net" script (below) to read arguments from $argv of the form --name=VALUE and -flag.

"Input":
./script.php -a arg1 --opt1 arg2 -bcde --opt2=val2 arg3 arg4 arg5 -fg --opt3

"print_r Output":
Array
(
[exec] => ./script.php
[options] => Array
(
[0] => opt1
[1] => Array
(
[0] => opt2
[1] => val2
)
[2] => opt3
)
[flags] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
)
[arguments] => Array
(
[0] => arg1
[1] => arg2
[2] => arg3
[3] => arg4
[4] => arg5
)
)

<?php
function arguments($args ) {
$ret = array(
'exec' => '',
'options' => array(),
'flags' => array(),
'arguments' => array(),
);

$ret['exec'] = array_shift( $args );

while (($arg = array_shift($args)) != NULL) {
// Is it a option? (prefixed with --)
if ( substr($arg, 0, 2) === '--' ) {
$option = substr($arg, 2);

// is it the syntax '--option=argument'?
if (strpos($option,'=') !== FALSE)
array_push( $ret['options'], explode('=', $option, 2) );
else
array_push( $ret['options'], $option );

continue;
}

// Is it a flag or a serial of flags? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' ) {
for ($i = 1; isset($arg[$i]) ; $i++)
$ret['flags'][] = $arg[$i];

continue;
}

// finally, it is not option, nor flag
$ret['arguments'][] = $arg;
continue;
}
return $ret;
}//function arguments
?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:01 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
I find regex and manually breaking up the arguments instead of havingon $_SERVER['argv'] to do it more flexiable this way.

cli_test.php asdf asdf --help --dest=/var/ -asd -h --option mew arf moo -z

Array
(
[input] => Array
(
[0] => asdf
[1] => asdf
)

[commands] => Array
(
[help] => 1
[dest] => /var/
[option] => mew arf moo
)

[flags] => Array
(
[0] => asd
[1] => h
[2] => z
)

)

<?php

function arguments ( $args )
{
array_shift( $args );
$args = join( $args, ' ' );

preg_match_all('/ (--\w+ (?:[= ] [^-]+ [^\s-] )? ) | (-\w+) | (\w+) /x', $args, $match );
$args = array_shift( $match );

/*
Array
(
[0] => asdf
[1] => asdf
[2] => --help
[3] => --dest=/var/
[4] => -asd
[5] => -h
[6] => --option mew arf moo
[7] => -z
)
*/

$ret = array(
'input' => array(),
'commands' => array(),
'flags' => array()
);

foreach ( $args as $arg ) {

// Is it a command? (prefixed with --)
if ( substr( $arg, 0, 2 ) === '--' ) {

$value = preg_split( '/[= ]/', $arg, 2 );
$com = substr( array_shift($value), 2 );
$value = join($value);

$ret['commands'][$com] = !empty($value) ? $value : true;
continue;

}

// Is it a flag? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' ) {
$ret['flags'][] = substr( $arg, 1 );
continue;
}

$ret['input'][] = $arg;
continue;

}

return $ret;
}

print_r( arguments( $argv ) );

?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:01 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
<?php
function arguments($argv) {
$ARG = array();
foreach ($argv as $arg) {
if (strpos($arg, '--') === 0) {
$compspec = explode('=', $arg);
$key = str_replace('--', '', array_shift($compspec));
$value = join('=', $compspec);
$ARG[$key] = $value;
} elseif (strpos($arg, '-') === 0) {
$key = str_replace('-', '', $arg);
if (!isset($ARG[$key])) $ARG[$key] = true;
}
}
return $ARG;
}
?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:02 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
Here's <losbrutos at free dot fr> function modified to support unix like param syntax like <B Crawford> mentions:

<?php
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches)) {
$key = $matches[1];
switch ($matches[2]) {
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}

/* make unix like -afd == -a -f -d */
if(preg_match("/^-([a-zA-Z0-9]+)/", $matches[0], $match)) {
$string = $match[1];
for($i=0; strlen($string) > $i; $i++) {
$_ARG[$string[$i]] = true;
}
} else {
$_ARG[$key] = $arg;
}
} else {
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
?>

Sample:

eromero@ditto ~/workspace/snipplets $ foxogg2mp3.php asdf asdf --help --dest=/var/ -asd -h
Array
(
[input] => Array
(
[0] => /usr/local/bin/foxogg2mp3.php
[1] => asdf
[2] => asdf
)

[help] => 1
[dest] => /var/
[a] => 1
[s] => 1
[d] => 1
[h] => 1
)


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:03 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
I was looking for a way to interactively get a single character response from user. Using STDIN with fread, fgets and such will only work after pressing enter. So I came up with this instead:

#!/usr/bin/php -q
<?php
function inKey($vals) {
$inKey = "";
While(!in_array($inKey,$vals)) {
$inKey = trim(`read -s -n1 valu;echo \$valu`);
}
return $inKey;
}
function echoAT($Row,$Col,$prompt="") {
// Display prompt at specific screen coords
echo "\033[".$Row.";".$Col."H".$prompt;
}
// Display prompt at position 10,10
echoAT(10,10,"Opt : ");

// Define acceptable responses
$options = array("1","2","3","4","X");

// Get user response
$key = inKey($options);

// Display user response & exit
echoAT(12,10,"Pressed : $key\n");
?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:03 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
I have not seen in this thread any code snippets that support the full *nix style argument parsing. Consider this:

<?php
print_r(getArgs($_SERVER['argv']));

function getArgs($args) {
$out = array();
$last_arg = null;
for($i = 1, $il = sizeof($args); $i < $il; $i++) {
if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ) {
$parts = explode("=", $match[1]);
$key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
if(isset($parts[1])) {
$out[$key] = $parts[1];
}
else {
$out[$key] = true;
}
$last_arg = $key;
}
else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ) {
for( $j = 0, $jl = strlen($match[1]); $j < $jl; $j++ ) {
$key = $match[1]{$j};
$out[$key] = true;
}
$last_arg = $key;
}
else if($last_arg !== null) {
$out[$last_arg] = $args[$i];
}
}
return $out;
}

/*
php file.php --foo=bar -abc -AB 'hello world' --baz

produces:

Array
(
[foo] => bar
[a] => true
[b] => true
[c] => true
[A] => true
[B] => hello world
[baz] => true
)

*/
?>


Back to top
 個人資料  
 
 文章主題 : Re: function()的程式應用
文章發表於 : 2011年 12月 4日, 09:04 
離線
系統管理員

註冊時間: 2009年 1月 14日, 06:05
文章: 1419
an another "another variant" :

<?php
function arguments($argv)
{
$_ARG = array();
foreach ($argv as $arg)
{
if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches))
{
$key = $matches[1];
switch ($matches[2])
{
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}
$_ARG[$key] = $arg;
}
else
{
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
?>

$php myscript.php arg1 -arg2=val2 --arg3=arg3 -arg4 --arg5 -arg6=false

Array
(
[input] => Array
(
[0] => myscript.php
[1] => arg1
)

[arg2] => val2
[arg3] => arg3
[arg4] => true
[arg5] => true
[arg5] => false
)


Back to top
 個人資料  
 
顯示文章 :  排序  
發表新文章 回覆主題  [ 9 篇文章 ] 

討論區首頁 : 資訊專區 : PHP程式設計


誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 2 位訪客


不能 在這個版面發表主題
不能 在這個版面回覆主題
不能 在這個版面編輯文章
不能 在這個版面刪除文章
不能 在這個版面上傳附加檔案

搜尋:
前往 :  
cron
Style by Midnight Phoenix & N.Design Studio
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
正體中文語系由 竹貓星球 維護製作