-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphp_cli_param_usage.php
63 lines (49 loc) · 1.39 KB
/
php_cli_param_usage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
// Program : Passing Arguments into PHP from (Windows) CLI and Parsing them
// Modes : CLI Arguments, Argument=Value Pairs, long option based getopt() parsing
// Author : Ap.Muthu <[email protected]>
// Version : 1.0
// Release Date : 2020-02-16
//
// Alt Library : https://github.com/getopt-php/getopt-php
// Alt Docs : http://getopt-php.github.io/getopt-php/
*/
// Normal CLI arguments
// ..\php.exe hello.php MyArg1 MyArd2
echo "Hello World\n";
echo print_r($argv, true);
// Parameters parsed in script
// ..\php.exe -f "hello.php" a=fff b=45
if($argc>1)
parse_str(implode('&',array_slice($argv, 1)), $_GET);
echo print_r($_GET, true);
// Using getopt() to parse them - platform agnostic
// ..\php.exe hello.php --a="fff" --b="45"
$defaultValues = array("a" => "", "b" => "");
// The variable followed by single colon is mandatory value, double colon makes it optional, else mere presence is discerned
$givenArguments = getopt("", array("a:","b::"));
$options = array_merge($defaultValues, $givenArguments);
$a = $options['a'];
$b = $options['b'];
echo '$a = ' . $a . "\n";
echo '$b = ' . $b;
/* Outputs
// ..\php.exe hello.php MyArg1 MyArd2
Hello World
Array
(
[0] => hello.php
[1] => MyArg1
[2] => MyArd2
)
// ..\php.exe -f "hello.php" a=fff b=45
Array
(
[a] => fff
[b] => 45
)
// ..\php.exe hello.php --a="fff" --b="45"
$a = fff
$b = 45
*/