71af2c7fdbe178bd1bfe4977271c8f0aaa3536c8
[m6w6/ext-psi] / tests / getopt / getopt001.phpt
1 --TEST--
2 getopt
3 --INI--
4 psi.directory={PWD}:{PWD}/../../psi.d
5 --SKIPIF--
6 <?php
7 extension_loaded("psi") or die("skip - need ext/psi");
8 PHP_OS === "Linux" or die("skip - only for Linux");
9 function_exists("psi\\getopt") or die("skip - need psi\\getopt()");
10 ?>
11 --ENV--
12 POSIXLY_CORRECT=
13 --FILE--
14 ===TEST===
15 <?php
16
17 $args = [
18 "progname", "huh", "-v", "-x", 1, "-s", "foo", "--", "-gotcha", "--bar", "baz"
19 ];
20
21 $opts = "v::x:s:";
22
23 for ($i = 0; $i<3; ++$i) {
24 psi\opterr(0);
25 psi\optind\set(1);
26
27 while (($opt = chr(psi\getopt($args, $opts)))) {
28 switch ($opt) {
29 case "v":
30 printf("got v\n");
31 break;
32 case "x":
33 case "s":
34 printf("got %s: %s\n", $opt, psi\optarg());
35 break;
36 default:
37 printf("error '%s'\n", $opt);
38 case chr(-1):
39 break 2;
40 }
41 }
42
43 $pos = psi\optind\get();
44 while ($pos < count($args)) {
45 printf("arg: %s\n", $args[$pos++]);
46 }
47 }
48 var_dump($args);
49
50 ?>
51 ===DONE===
52 --EXPECT--
53 ===TEST===
54 got v
55 got x: 1
56 got s: foo
57 arg: huh
58 arg: -gotcha
59 arg: --bar
60 arg: baz
61 got v
62 got x: 1
63 got s: foo
64 arg: huh
65 arg: -gotcha
66 arg: --bar
67 arg: baz
68 got v
69 got x: 1
70 got s: foo
71 arg: huh
72 arg: -gotcha
73 arg: --bar
74 arg: baz
75 array(11) {
76 [0]=>
77 string(8) "progname"
78 [1]=>
79 string(2) "-v"
80 [2]=>
81 string(2) "-x"
82 [3]=>
83 string(1) "1"
84 [4]=>
85 string(2) "-s"
86 [5]=>
87 string(3) "foo"
88 [6]=>
89 string(2) "--"
90 [7]=>
91 string(3) "huh"
92 [8]=>
93 string(7) "-gotcha"
94 [9]=>
95 string(5) "--bar"
96 [10]=>
97 string(3) "baz"
98 }
99 ===DONE===