PHP在命令行中获得多个参数

时间:2023-01-20 03:36:57

On php command line i can read only the first argument. When i add the site name the program not breaking before the password input.

在php命令行上,我只能读取第一个参数。当我添加网站名称时,程序在密码输入之前没有破坏。

echo "Site name: ";
$handle = fopen ("php://stdin","r");
$base_name = trim(fgets($handle));
fclose($handle);

echo "Password:";
$handle = fopen ("php://stdin","r");
$base_password = trim(fgets($handle));
fclose($handle);

How can i read these two variables from stdin?

我怎样才能从stdin读取这两个变量?

2 个解决方案

#1


1  

Your arguments passed at command time will be in the global $argv as well as the super global $_SERVER["argv"] with $argv[0] and $_SERVER["argv"][0] being the command that was called.

在命令时传递的参数将在全局$ argv以及超级全局$ _SERVER [“argv”]中,其中$ argv [0]和$ _SERVER [“argv”] [0]是被调用的命令。

A useful function for parsing out for example the call ./myscript.php --user=root --password=foobar

解析例如调用的有用函数./myscript.php --user = root --password = foobar

function parse_argvs(){
    if( $params = $_SERVER["argv"] ){
        $file = array_shift( $params );
        while( $params ){
            $param = array_shift( $params );
            switch( strspn( $param, "-" ) ){
                case( 1 ):
                    $OPTS[ trim( $param, " -" ) ] = array_shift( $params );
                break;
                case( 2 ):
                    list( $key, $value ) = explode( "=", $param );
                    $OPTS[ trim( $key, " -" ) ] = $value;
                break;
                default:
                    $OPTS[ $param ] = true;
                break;
            }
        }
    }
    return $OPTS ?: array();
}

Called something like

叫做类似的东西

$parsed = parse_argvs();
echo $parsed['user']; //root
echo $parsed['password']; //password

These are actual command line arguments passed at call time. I hope this helps.

这些是在调用时传递的实际命令行参数。我希望这有帮助。

#2


0  

Try this:

$base_name     = readline("Site Name: ");
$base_password = readline("Password: ");

PHP Readline Function

PHP Readline函数

#1


1  

Your arguments passed at command time will be in the global $argv as well as the super global $_SERVER["argv"] with $argv[0] and $_SERVER["argv"][0] being the command that was called.

在命令时传递的参数将在全局$ argv以及超级全局$ _SERVER [“argv”]中,其中$ argv [0]和$ _SERVER [“argv”] [0]是被调用的命令。

A useful function for parsing out for example the call ./myscript.php --user=root --password=foobar

解析例如调用的有用函数./myscript.php --user = root --password = foobar

function parse_argvs(){
    if( $params = $_SERVER["argv"] ){
        $file = array_shift( $params );
        while( $params ){
            $param = array_shift( $params );
            switch( strspn( $param, "-" ) ){
                case( 1 ):
                    $OPTS[ trim( $param, " -" ) ] = array_shift( $params );
                break;
                case( 2 ):
                    list( $key, $value ) = explode( "=", $param );
                    $OPTS[ trim( $key, " -" ) ] = $value;
                break;
                default:
                    $OPTS[ $param ] = true;
                break;
            }
        }
    }
    return $OPTS ?: array();
}

Called something like

叫做类似的东西

$parsed = parse_argvs();
echo $parsed['user']; //root
echo $parsed['password']; //password

These are actual command line arguments passed at call time. I hope this helps.

这些是在调用时传递的实际命令行参数。我希望这有帮助。

#2


0  

Try this:

$base_name     = readline("Site Name: ");
$base_password = readline("Password: ");

PHP Readline Function

PHP Readline函数