Console_Getargs
[ class tree: Console_Getargs ] [ index: Console_Getargs ] [ all elements ]

Source for file example.php

Documentation is available at example.php

  1. <?php
  2. /**
  3.  * Example for Console_Getargs class
  4.  * 
  5.  * $Id: example.php 170645 2004-10-14 20:44:41Z scottmattocks $
  6.  */
  7.  
  8. require_once 'Console/Getargs.php';
  9.  
  10. $config = array(
  11.             // Option takes 2 values
  12.             'files|images' => array('short' => 'f|i',
  13.                     'min' => 2,
  14.                     'max' => 2,
  15.                     'desc' => 'Set the source and destination image files.'),
  16.             // Option takes 1 value
  17.             'width' => array('short' => 'w',
  18.                  'min' => 1,
  19.                  'max' => 1,
  20.                  'desc' => 'Set the new width of the image.'),
  21.             // Option is a switch
  22.             'debug' => array('short' => 'd',
  23.                  'max' => 0,
  24.                  'desc' => 'Switch to debug mode.'),
  25.             // Option takes from 1 to 3 values, using the default value(s) if the arg is not present
  26.             'formats' => array('min' => 1,
  27.                    'max' => 3,
  28.                    'desc' => 'Set the image destination format.',
  29.                    'default' => array('jpegbig''jpegsmall')),
  30.             // Option takes from 1 to an unlimited number of values
  31.             'filters' => array('short' => 'fi',
  32.                    'min' => 1,
  33.                    'max' => -1,
  34.                    'desc' => 'Set the filters to be applied to the image upon conversion. The filters will be used in the order they are set.'),
  35.             // Option accept 1 value or nothing. If nothing, then the default value is used
  36.             'verbose' => array('short' => 'v',
  37.                    'min' => 0,
  38.                    'max' => 1,
  39.                    'desc' => 'Set the verbose level.',
  40.                    'default' => 3),
  41.             // Parameters. Anything leftover at the end of the command line is added.
  42.             CONSOLE_GETARGS_PARAMS => array('min' => 1,
  43.                         'max' => 2,
  44.                         'desc' =>
  45.                         'Set the application parameters.',
  46.                         'default' => 'DEFAULT')
  47.             );
  48.  
  49. $args =Console_Getargs::factory($config);
  50.  
  51. // Use the following two lines to test passing an array
  52. // other than $_SERVER['argv']
  53. //$test =  array('-dvw', 500, 'foo1', 'foo2');
  54. //$args =& Console_Getargs::factory($config, $test);
  55.  
  56. if (PEAR::isError($args)) {
  57.     $header "Console_Getargs Example\n".
  58.               'Usage: '.basename($_SERVER['SCRIPT_NAME'])." [options]\n\n";
  59.     if ($args->getCode(=== CONSOLE_GETARGS_ERROR_USER{
  60.         echo Console_Getargs::getHelp($config$header$args->getMessage())."\n";
  61.     else if ($args->getCode(=== CONSOLE_GETARGS_HELP{
  62.         echo Console_Getargs::getHelp($config$header)."\n";
  63.         // To see the automatic header uncomment this line
  64.         //echo Console_Getargs::getHelp($config)."\n";
  65.     }
  66.     exit;
  67. }
  68.  
  69. echo 'Verbose: '.$args->getValue('verbose')."\n";
  70. echo 'Formats: '.(is_array($args->getValue('formats')) implode(', '$args->getValue('formats'))."\n" $args->getValue('formats')."\n");
  71. echo 'Files: '.($args->isDefined('files'implode(', '$args->getValue('files'))."\n" "undefined\n");
  72. if ($args->isDefined('fi')) {
  73.     echo 'Filters: '.(is_array($args->getValue('fi')) implode(', '$args->getValue('fi'))."\n" $args->getValue('fi')."\n");
  74. else {
  75.     echo "Filters: undefined\n";
  76. }
  77. echo 'Width: '.$args->getValue('w')."\n";
  78. echo 'Debug: '.($args->isDefined('d'"YES\n" "NO\n");
  79. echo 'Parameters: '.($args->isDefined('parameters'is_array($args->getValue('parameters')) implode(', '$args->getValue('parameters')) $args->getValue('parameters'"undefined""\n";
  80.  
  81. // Test with:
  82. // ----------
  83. // Get the help message
  84. // php -q example.php -h
  85. //
  86. // Pass two files
  87. // php -q example.php -v -f src.tiff dest.tiff
  88. //
  89. // Set verbose level 5, pass two files, and debug
  90. // php -q example.php -v5 -f src.tiff dest.tiff -d
  91. //
  92. // Set verbose level 1, pass two files, debug, and set width to 100
  93. // php -q example.php -v 1 -f src.tiff dest.tiff -d --width=100
  94. //
  95. // Set verbose (defaults to 3), pass two files, and pass two filters
  96. // php -q example.php -v -f src.tiff dest.tiff -fi sharp blur
  97. //
  98. // Set three formats
  99. // php -q example.php --format gif jpeg png
  100. //
  101. // Debug, set verbose to default level and width to 100
  102. // php -q example.php -dvw 100
  103. //
  104. // Pass two application parameters
  105. // php -q example.php foo1 foo2
  106. //
  107. // Debug, set verbose level 5, pass two files, and pass two application parameters
  108. // php -q examples/example.php -dv5 -f src.tiff dest.tiff foo1 foo2 
  109.  
  110. ?>

Documentation generated on Mon, 11 Mar 2019 15:40:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.