32.7 Requiring Parameters

Both OEParseCommandLine and OEParseCommandLineLW will check that the user has set all the required parameters. There are two different possible kinds of requirements, single parameter requirements and multi parameter requirements. Single variable requirements are set using the !REQUIRED field in the !PARAMETER record. A required parameter must either be set by the user or have a default value. Multi variable requirements depend upon the setting of several variables and are specified with a text configuration file (separate from the text configuration file passed to OEConfigure). The format of the requirement text configuration file passed to either OEParseCommandLine or OEParseCommandLineLW are described in the API documentation. The following example illustrates both kinds of requirements.

Chapter 32 - Example 9 : cpp file

#include "oeplatform.h"
#include "oesystem.h"

//Defines static const unsigned char* array InterfaceData
#include "ch32-9.itf"

//Defines static const unsigned char* array RequirementData
#include "ch32-9r.req"

using namespace OEPlatform;
using namespace OESystem;

int main(int argc, char** argv)
{
  OEInterface itf;
  OEConfigure(itf,InterfaceData);
  if (OECheckHelp(itf,argc,argv)) return 0;
  OEParseCommandLine(itf,argc,argv,RequirementData);
  OEWriteSettings(itf,oeout,true);
  return 0;
}

Chapter 32 - Example 9 : OEConfigure txt configuration file

!CATEGORY Variables
  !BRIEF Category for variables

  !PARAMETER -x
    !TYPE float
    !DEFAULT 0.0
    !BRIEF First variable
  !END

  !PARAMETER -y
    !TYPE float
    !DEFAULT 0.0
    !BRIEF Second variable
  !END

!END

!CATEGORY operator

  !PARAMETER -op
    !TYPE string
    !BRIEF Operation to perform on x and y
    !REQUIRED yes
    !DETAIL
      The operation performed is

        x <-op> y

      So if -op is 'add' the operation is

        x + y
  !END

!END

Chapter 32 - Example 9 : Requirement txt configuration file

!REQUIREMENT
  !OPTION -y 0.0 !-op divide
  !OPTION !-y 0.0
  !ERROR_MSG
    Cannot divide by zero
    result would be undefined
!END

Running this program gives the following results with a valid command line.

> ch32-9 -x 1 -y 2 -op divide
#Interface settings

#Variables : Category for variables
    -x  1.000000
    -y  2.000000

#operator :
    -op  divide

and the following results with command lines not matching the requirements

> ch32-9 -x 1 -y 1
Fatal: Missing required parameter -op

> ch32-9 -x 1 -y 0 -op divide
Fatal: Missing requirements:
    Cannot divide by zero
    result would be undefined

The first command line above fails because of the "!REQUIRED true" field in the !PARAMETER -op record. The second command line fails because of the multi-parameter requirements.