PHP Classes

File: test_read_arguments.php

Recommend this page to a friend!
  Packages of Manuel Lemos   Read arguments   test_read_arguments.php   Download  
File: test_read_arguments.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Read arguments
Read and validate shell command line arguments
Author: By
Last change:
Date: 16 years ago
Size: 1,717 bytes
 

 

Contents

Class file image Download
<?php
/*
 * read_arguments.php
 *
 * @(#) $Id: test_read_arguments.php,v 1.1 2010/09/30 07:57:44 mlemos Exp $
 *
 */

    require('read_arguments.php');

    /*
     *  Create an object to read the arguments
     */
    $arguments = new read_arguments_class;

    /*
     *  Define an option to show the command usage help
     */
    $arguments->AddOption(array(
        'Name'=>'help',
        'Type'=>'usage',
        'Description'=>'usage help',
        'Keyword'=>'-h',
        'Aliases'=>array('--help')
    ));

    /*
     *  Define a switch option to turn on a value
     */
    $arguments->AddOption(array(
        'Name'=>'debug',
        'Type'=>'switch',
        'Description'=>'output debug information',
        'Keyword'=>'-d',
        'Aliases'=>array('--debug')
    ));

    /*
     *  Define an optional option that requires a value after the switch keyword
     */
    $arguments->AddOption(array(
        'Name'=>'email',
        'Type'=>'text',
        'Description'=>'change account e-mail address',
        'KeywordRequired'=>true,
        'Keyword'=>'-e',
        'Aliases'=>array('--change-email')
    ));

    /*
     *  Define a mandatory option that requires a value after the switch keyword
     */
    $arguments->AddOption(array(
        'Name'=>'account',
        'Description'=>'nickname or e-mail of the account user',
        'Type'=>'text',
        'Required'=>1
    ));

    if(!$arguments->ProcessArguments($_SERVER['argv'], $values))
    {
        echo 'Error: ', $arguments->error, "\n";
        echo 'Usage: '.$arguments->Usage($_SERVER['argv'][0]);
    }
    elseif(IsSet($values['help']))
    {
        echo 'Usage: '.$arguments->Usage($_SERVER['argv'][0]);
    }
    else
    {
        if(IsSet($values['debug']))
            echo 'Debug output is on', "\n";
        echo 'The account is ', $values['account'], "\n";
        if(IsSet($values['email']))
            echo 'The account e-mail address was changed to ', $values['email'], "\n";
    }
?>