1515 * @package Argv
1616 */
1717namespace Horde \Argv ;
18+ use Iterator ;
19+ use InvalidArgumentException ;
20+ use RuntimeException ;
21+
1822
1923/**
2024 * Defines the Option class and some standard value-checking functions.
@@ -131,8 +135,8 @@ public function checkChoice($opt, $value)
131135 $ choices [] = (string )$ choice ;
132136 }
133137 $ choices = "' " . implode ("', ' " , $ choices ) . "' " ;
134- throw new Horde_Argv_OptionValueException (sprintf (
135- Horde_Argv_Translation ::t ("option %s: invalid choice: '%s' (choose from %s) " ),
138+ throw new OptionValueException (sprintf (
139+ Translation ::t ("option %s: invalid choice: '%s' (choose from %s) " ),
136140 $ opt , $ value , $ choices ));
137141 }
138142 }
@@ -155,6 +159,7 @@ public function checkChoice($opt, $value)
155159 'help ' ,
156160 'metavar ' ,
157161 );
162+
158163
159164 /**
160165 * The set of actions allowed by option parsers. Explicitly listed here so
@@ -267,14 +272,29 @@ public function checkChoice($opt, $value)
267272
268273 public $ shortOpts = array ();
269274 public $ longOpts = array ();
275+ // These should probably be made readonly once we are sure we *really* usually set them through a constructor
276+ public $ action ;
277+ public $ type ;
270278 public $ dest ;
271279 public $ default ;
280+ public $ nargs ;
281+ public $ const ;
282+ public $ choices ;
283+ public $ callback ;
284+ public $ callbackArgs ;
285+ public $ help ;
286+ // TODO: Where is this even used?
287+ public $ metavar ;
288+ // Used in OptionContainer->addOption
289+ public $ container ;
272290
273291 /**
274292 * Constructor.
275293 */
276294 public function __construct ()
277295 {
296+ // TODO: Refactor this to use optional constructor properties
297+
278298 // The last argument to this function is an $attrs hash, if it
279299 // is present and an array. All other arguments are $opts.
280300 $ opts = func_get_args ();
@@ -323,17 +343,17 @@ protected function _setOptStrings($opts)
323343 $ opt = (string )$ opt ;
324344
325345 if (strlen ($ opt ) < 2 ) {
326- throw new Horde_Argv_OptionException (sprintf ("invalid option string '%s': must be at least two characters long " , $ opt ), $ this );
346+ throw new OptionException (sprintf ("invalid option string '%s': must be at least two characters long " , $ opt ), $ this );
327347 } elseif (strlen ($ opt ) == 2 ) {
328348 if (!($ opt [0 ] == '- ' && $ opt [1 ] != '- ' )) {
329- throw new Horde_Argv_OptionException (sprintf (
349+ throw new OptionException (sprintf (
330350 "invalid short option string '%s': " .
331351 "must be of the form -x, (x any non-dash char) " , $ opt ), $ this );
332352 }
333353 $ this ->shortOpts [] = $ opt ;
334354 } else {
335355 if (!(substr ($ opt , 0 , 2 ) == '-- ' && $ opt [2 ] != '- ' )) {
336- throw new Horde_Argv_OptionException (sprintf (
356+ throw new OptionException (sprintf (
337357 "invalid long option string '%s': " .
338358 "must start with --, followed by non-dash " , $ opt ), $ this );
339359 }
@@ -360,7 +380,7 @@ protected function _setAttrs($attrs)
360380 if ($ attrs ) {
361381 $ attrs = array_keys ($ attrs );
362382 sort ($ attrs );
363- throw new Horde_Argv_OptionException (sprintf (
383+ throw new OptionException (sprintf (
364384 'invalid keyword arguments: %s ' , implode (', ' , $ attrs )), $ this );
365385 }
366386 }
@@ -373,7 +393,7 @@ public function _checkAction()
373393 if (is_null ($ this ->action )) {
374394 $ this ->action = 'store ' ;
375395 } elseif (!in_array ($ this ->action , $ this ->ACTIONS )) {
376- throw new Horde_Argv_OptionException (sprintf ("invalid action: '%s' " , $ this ->action ), $ this );
396+ throw new OptionException (sprintf ("invalid action: '%s' " , $ this ->action ), $ this );
377397 }
378398 }
379399
@@ -395,11 +415,11 @@ public function _checkType()
395415 }
396416
397417 if (!in_array ($ this ->type , $ this ->TYPES )) {
398- throw new Horde_Argv_OptionException (sprintf ("invalid option type: '%s' " , $ this ->type ), $ this );
418+ throw new OptionException (sprintf ("invalid option type: '%s' " , $ this ->type ), $ this );
399419 }
400420
401421 if (!in_array ($ this ->action , $ this ->TYPED_ACTIONS )) {
402- throw new Horde_Argv_OptionException (sprintf (
422+ throw new OptionException (sprintf (
403423 "must not supply a type for action '%s' " , $ this ->action ), $ this );
404424 }
405425 }
@@ -409,15 +429,15 @@ public function _checkChoice()
409429 {
410430 if ($ this ->type == 'choice ' ) {
411431 if (is_null ($ this ->choices )) {
412- throw new Horde_Argv_OptionException (
432+ throw new OptionException (
413433 "must supply a list of choices for type 'choice' " , $ this );
414434 } elseif (!(is_array ($ this ->choices ) || $ this ->choices instanceof Iterator)) {
415- throw new Horde_Argv_OptionException (sprintf (
435+ throw new OptionException (sprintf (
416436 "choices must be a list of strings ('%s' supplied) " ,
417437 gettype ($ this ->choices )), $ this );
418438 }
419439 } elseif (!is_null ($ this ->choices )) {
420- throw new Horde_Argv_OptionException (sprintf (
440+ throw new OptionException (sprintf (
421441 "must not supply choices for type '%s' " , $ this ->type ), $ this );
422442 }
423443 }
@@ -443,7 +463,7 @@ public function _checkDest()
443463 public function _checkConst ()
444464 {
445465 if (!in_array ($ this ->action , $ this ->CONST_ACTIONS ) && !is_null ($ this ->const )) {
446- throw new Horde_Argv_OptionException (sprintf (
466+ throw new OptionException (sprintf (
447467 "'const' must not be supplied for action '%s' " , $ this ->action ),
448468 $ this );
449469 }
@@ -456,38 +476,46 @@ public function _checkNargs()
456476 $ this ->nargs = 1 ;
457477 }
458478 } elseif (!is_null ($ this ->nargs )) {
459- throw new Horde_Argv_OptionException (sprintf (
479+ throw new OptionException (sprintf (
460480 "'nargs' must not be supplied for action '%s' " , $ this ->action ),
461481 $ this );
462482 }
463483 }
464484
465485 public function _checkCallback ()
466486 {
487+ // if action is callback, callback must exist and be valid. If not, callback must be null or exist and be valid
467488 if ($ this ->action == 'callback ' ) {
489+ // Callback must be a callable or an array with object as first item and method name as second item OR a string in format CLASS#method
468490 if (!is_callable ($ this ->callback )) {
469- $ callback_name = is_array ($ this ->callback ) ?
470- is_object ($ this ->callback [0 ]) ? get_class ($ this ->callback [0 ] . '# ' . $ this ->callback [1 ]) : implode ('# ' , $ this ->callback ) :
471- $ this ->callback ;
472- throw new Horde_Argv_OptionException (sprintf (
473- "callback not callable: '%s' " , $ callback_name ), $ this );
491+ $ callback_name = '' ;
492+ if (is_array ($ this ->callback )) {
493+ if (is_object ($ this ->callback [0 ])) {
494+ $ callback_name = get_class ($ this ->callback [0 ]) . '# ' . $ this ->callback [1 ];
495+ } else {
496+ $ callback_name = implode ('# ' , $ this ->callback );
497+ }
498+ } else {
499+ throw new OptionException (sprintf (
500+ "callback not callable: '%s' " , $ callback_name ), $ this );
501+ }
474502 }
475503 if (!is_null ($ this ->callbackArgs ) && !is_array ($ this ->callbackArgs )) {
476- throw new Horde_Argv_OptionException (sprintf (
504+ throw new OptionException (sprintf (
477505 "callbackArgs, if supplied, must be an array: not '%s' " ,
478506 $ this ->callbackArgs ), $ this );
479507 }
480508 } else {
481509 if (!is_null ($ this ->callback )) {
482510 $ callback_name = is_array ($ this ->callback ) ?
483- is_object ($ this ->callback [0 ]) ? get_class ($ this ->callback [0 ] . '# ' . $ this ->callback [1 ]) : implode ('# ' , $ this ->callback ) :
511+ is_object ($ this ->callback [0 ]) ? get_class ($ this ->callback [0 ]) . '# ' . $ this ->callback [1 ] : implode ('# ' , $ this ->callback ) :
484512 $ this ->callback ;
485- throw new Horde_Argv_OptionException (sprintf (
513+ throw new OptionException (sprintf (
486514 "callback supplied ('%s') for non-callback option " ,
487515 $ callback_name ), $ this );
488516 }
489517 if (!is_null ($ this ->callbackArgs )) {
490- throw new Horde_Argv_OptionException (
518+ throw new OptionException (
491519 'callbackArgs supplied for non-callback option ' , $ this );
492520 }
493521 }
0 commit comments