@@ -12,18 +12,24 @@ if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
1212 );
1313}
1414
15- $ options = getopt ('af:R ' ,array (
15+ $ options = getopt ('af:Nno:RuV ' ,array (
1616 'append:: ' ,
1717 'debug ' ,
1818 'exclude: ' ,
1919 'excmd:: ' ,
2020 'fields:: ' ,
2121 'format:: ' ,
2222 'recurse:: ' ,
23+ 'sort:: ' ,
2324 'version ' ,
2425 'memory:: ' ,
2526));
2627
28+ // option -V is an alternative to --version
29+ if (isset ($ options ['V ' ])) {
30+ $ options ['version ' ] = FALSE ;
31+ }
32+
2733if (!isset ($ options ['debug ' ])) {
2834 error_reporting (0 );
2935}
@@ -43,6 +49,30 @@ array_shift($argv);
4349
4450$ file = array_pop ($ argv );
4551
52+ // option -o is an alternative to -f
53+ if (isset ($ options ['o ' ]) && !isset ($ options ['f ' ])) {
54+ $ options ['f ' ] = $ options ['o ' ];
55+ }
56+
57+ // if both -n and -N options are given, use the last specified one
58+ if (isset ($ options ['n ' ]) && isset ($ options ['N ' ])) {
59+ if (array_search ('n ' , array_keys ($ options )) < array_search ('N ' , array_keys ($ options ))) {
60+ unset($ options ['n ' ]);
61+ } else {
62+ unset($ options ['N ' ]);
63+ }
64+ }
65+
66+ // option -n is equivalent to --excmd=number
67+ if (isset ($ options ['n ' ]) && !isset ($ options ['N ' ])) {
68+ $ options ['excmd ' ] = 'number ' ;
69+ }
70+
71+ // option -N is equivalent to --excmd=pattern
72+ if (isset ($ options ['N ' ]) && !isset ($ options ['n ' ])) {
73+ $ options ['excmd ' ] = 'pattern ' ;
74+ }
75+
4676if (!isset ($ options ['excmd ' ]))
4777 $ options ['excmd ' ] = 'pattern ' ;
4878if (!isset ($ options ['format ' ]))
@@ -55,6 +85,28 @@ if(!isset($options['fields'])) {
5585 $ options ['fields ' ] = str_split ($ options ['fields ' ]);
5686}
5787
88+ // handle -u or --sort options
89+ if (isset ($ options ['sort ' ])) {
90+ // --sort or --sort=[Y,y,YES,Yes,yes]
91+ if ($ options ['sort ' ] === FALSE || yes_or_no ($ options ['sort ' ]) == 'yes ' ) {
92+ $ options ['sort ' ] = 'yes ' ;
93+ // --sort=[N,n,NO,No,no]
94+ } else if (yes_or_no ($ options ['sort ' ]) == 'no ' ) {
95+ $ options ['sort ' ] = 'no ' ;
96+ // --sort=foldcase, case insensitive sorting
97+ } else if ($ options ['sort ' ] == 'foldcase ' ) {
98+ $ options ['sort ' ] = 'foldcase ' ;
99+ } else {
100+ die ('phpctags: Invalid value for "sort" option ' );
101+ }
102+ // option -n is equivalent to --sort=no
103+ } else if (isset ($ options ['u ' ])) {
104+ $ options ['sort ' ] = 'no ' ;
105+ // sort the result by default
106+ } else {
107+ $ options ['sort ' ] = 'yes ' ;
108+ }
109+
58110// if the memory limit option is set and is valid, adjust memory
59111if (isset ($ options ['memory ' ])) {
60112 $ memory_limit = trim ($ options ['memory ' ]);
@@ -86,10 +138,15 @@ try {
86138 die ("phpctags: {$ e ->getMessage ()}" );
87139}
88140
141+ // write to a specified file
89142if (isset ($ options ['f ' ]) && $ options ['f ' ] !== '- ' ) {
90143 $ tagfile = fopen ($ options ['f ' ], isset ($ options ['a ' ]) ? 'a ' : 'w ' );
91- } else {
144+ // write to stdout only when instructed
145+ } else if (isset ($ options ['f ' ]) && $ options ['f ' ] === '- ' ) {
92146 $ tagfile = fopen ('php://stdout ' , 'w ' );
147+ // write to file 'tags' by default
148+ } else {
149+ $ tagfile = fopen ('tags ' , isset ($ options ['a ' ]) ? 'a ' : 'w ' );
93150}
94151fwrite ($ tagfile , $ result );
95152fclose ($ tagfile );
0 commit comments