This repository was archived by the owner on Oct 2, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -730,6 +730,33 @@ public function parse()
730
730
$ this ->_parsed = true ;
731
731
return $ this ;
732
732
}
733
+
734
+ public function checkRequiredArguments (){
735
+
736
+
737
+ foreach ($ this ->_rules as $ name =>$ rule ){
738
+
739
+ if ($ rule ['param ' ] === 'required ' ){
740
+
741
+ $ defined = false ;
742
+
743
+ foreach ($ rule ['alias ' ] as $ alias ){
744
+
745
+ $ defined = $ defined === true ? true : array_key_exists ($ alias , $ this ->_options );
746
+
747
+ }
748
+ if ($ defined === false ){
749
+
750
+ require_once 'Zend/Console/Getopt/Exception.php ' ;
751
+ throw new Zend_Console_Getopt_Exception (
752
+ "Option \"$ alias \" requires a parameter. " ,
753
+ $ this ->getUsageMessage ());
754
+
755
+ }
756
+ }
757
+ }
758
+
759
+ }
733
760
734
761
/**
735
762
* Parse command-line arguments for a single long option.
Original file line number Diff line number Diff line change @@ -268,6 +268,50 @@ public function testGetoptUnSetBeforeParse()
268
268
unset($ opts ->a );
269
269
$ this ->assertFalse (isset ($ opts ->a ));
270
270
}
271
+
272
+ public function testVerifyRequiredArgument (){
273
+ $ opts = new Zend_Console_Getopt (array (
274
+ 'apple|a=s ' =>"First required argument "
275
+ ));
276
+ try {
277
+ $ opts ->parse ();
278
+ $ opts ->checkRequiredArguments ();
279
+ $ this ->fail ('Expected to catch a Zend_Console_Getopt_Exception ' );
280
+ }
281
+ catch (Exception $ e ){
282
+ $ this ->assertTrue ($ e instanceof Zend_Console_Getopt_Exception,
283
+ 'Expected Zend_Console_Getopt_Exception, got ' . get_class ($ e ));
284
+
285
+ $ this ->assertEquals ( 'Option "a" requires a parameter. ' , $ e ->getMessage () );
286
+ }
287
+ }
288
+
289
+ public function testEmptyRequiredOption (){
290
+
291
+ $ opts = new Zend_Console_Getopt (array (
292
+ 'apple|a=s ' =>"First required argument " ,
293
+ 'banana|b=i ' =>"Second required argument "
294
+ ));
295
+
296
+ $ opts ->addArguments (array (
297
+ "-a " ,
298
+ "-b " ,
299
+ "123 "
300
+ ));
301
+
302
+ try {
303
+ $ opts ->parse ();
304
+ $ opts ->checkRequiredArguments ();
305
+ $ this ->fail ('Expected to catch a Zend_Console_Getopt_Exception ' );
306
+
307
+ } catch (Exception $ e ) {
308
+
309
+ $ this ->assertTrue ($ e instanceof Zend_Console_Getopt_Exception,
310
+ 'Expected Zend_Console_Getopt_Exception, got ' . get_class ($ e ));
311
+
312
+ $ this ->assertEquals ( 'Option "a" requires a parameter. ' , $ e ->getMessage () );
313
+ }
314
+ }
271
315
272
316
/**
273
317
* @group ZF-5948
You can’t perform that action at this time.
0 commit comments