Ada 95 Quality and Style Guide Chapter 7

Chapter 7: Portability - TOC - 7.1 FUNDAMENTALS

7.1.4 Main Subprogram

guideline

  • Consider using only a parameterless procedure as the main subprogram.
  • Consider using Ada.Command_Line for accessing values from the environment, but recognize that this package's behavior and even its specification are nonportable (see Guideline 7.1.6).
  • Encapsulate and document all uses of package Ada.Command_Line.
  • example

    The following example encapsulates the arguments for a hypothetical "execution mode" argument passed from the environment. It encapsulates both the expected position and the expected values of the argument, as well as provides a default in cases where the environment was unable to provide the information:

    package Environment is
    
       type Execution_Mode is (Unspecified, Interactive, Batch);
    
       function Execution_Argument return Execution_Mode;
    
       ...
    
    end Environment;
    
    ----------------------------------------------------------------------
    
    with Ada.Command_Line;       use Ada.Command_Line;
    with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
    
    package body Environment is
    
       function Execution_Argument return Execution_Mode is
    
          Execution_Argument_Number : constant := 1;
    
          Interactive_Mode_String : constant String := "-i";
          Batch_Mode_String       : constant String := "-b";
    
       begin
          if Argument_Count < Execution_Argument_Number then
             return Unspecified;
          elsif To_Unbounded_String (Argument (Execution_Argument_Number)) =
                   Interactive_Mode_String then
             return Interactive;
          elsif To_Unbounded_String (Argument (Execution_Argument_Number)) =
                   Batch_Mode_String then
             return Batch;
          else
             return Unspecified;
          end if;
       end Execution_Argument;
    
    end Environment;
    
    

    rationale

    The predefined language environment declares the package Ada.Command_Line, providing a standardized way for a program to obtain the values of a command line. Because all Ada compilers must implement the packages in the predefined language environment, you can create a program that is more portable, maintainable, and readable by using this package. You should, however, be aware that even though the language defines the objects and type profiles of this package, it does not force a relationship between the function results and any other entity or operation, and thus, allows the possibility of a nonportable behavior and specification.

    The value returned by the function Ada.Command_Line.Argument_Count is implementation-dependent. Different operating systems follow different conventions regarding the parsing and meaning of command line parameters. To enhance your program's portability, assume the simplest case: that the external execution environment does not support passing arguments to a program.

    Some operating systems are capable of acquiring and interpreting returned integer values near 0 from a function, but many others cannot. Further, many real-time, embedded systems will not be designed to terminate, so a function or a procedure having parameters with modes out or in out will be inappropriate to such applications.

    This leaves procedures with in parameters. Although some operating systems can pass parameters into a program as it starts, others are not. Also, an implementation may not be able to perform type checking on such parameters even if the surrounding environment is capable of providing them.

    notes

    Real-time, embedded applications may not have an "operator" initiating the program to supply the parameters, in which case it would be more appropriate for the program to have been compiled with a package containing the appropriate constant values or for the program to read the necessary values from switch settings or a downloaded auxiliary file. In any case, the variation in surrounding initiating environments is far too great to depend upon the kind of last-minute (program) parameterization implied by (subprogram) parameters to the main subprogram. POSIX 5 provides a standard operating system command line interface that might be a more appropriate alternative to the Ada command line facility depending on the implementation family of an application.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography