CIS-2730 Homework #6: Command Line Switches

Due: Wednesday, March 4, 2015

This is a traditional assignment. Every student will be working on the same thing. Although this assignment uses the Thumper code base you will not be submitted your work via a pull request. Instead you will submit your work in the usual way (via Moodle).

  1. First make sure you have the latest version of the Thumper code base.

            $ git pull upstream master
            $ git push                     # Only necessary if the above pulled new updates.

    Create a local branch in your Thumper repository.

            $ git branch hw06
            $ git checkout hw06

    You do not need to push this branch to your fork of Thumper on GitHub; it can stay entirely local to your system.

  2. Programs often need to process command line arguments. Review the specification of the Ada standard library package Ada.Command_Line.

  3. The Thumper client and server program should accept command line switches to specify the port and host they are using. In particular

            > thumper_server -p 9000
            > thumper_client -h -p 9000

    For both programs the -p switch can be used to specify the port number on which the program listens (in the case of the server) or connects to (in the case of the client). The client can also use the -h switch to specify the IP address of the server host.

    The -p switch is optional. If it is missing the default of 318 is used for the port (this is the "official" port number for the time stamp service). The -h switch is illegal for the server and required for the client. The order in which the switches are used (in the case of the client) is irrelevant. Note that a space is required between the switch name (-p or -h) and its value. Thus the switch name and its value appear as two separate command line arguments to the program.

    Write a package that provides command line switch processing services that can be used with both the client and the server. I suggest a (partial) interface along these lines but you should feel free to adjust this interface if you want.

            with Ada.Strings.Unbounded;
            package Thumper_Switches is
               use Ada.Strings.Unbounded;
               type Endpoint_Type is (Client, Server);
               type Switch_Type is (Host, Port);
               Switch_Error : exception;
               -- Returns with Valid = True if the command line switches are acceptable for the
               -- specified endpoint (Client or Server). It checks
               --   a) The command line is in the form of multiple (name, value) pairs.
               --   b) No names are duplicated.
               --   c) Any required switches are present.
               --   d) No illegal switches are present.
               --   e) No unknown switches are present.
               -- The syntax of switch values need not be checked. When the procedure returns the
               -- Message parameter contains a human readible error message or "No Error" if the
               -- validation was successful.
               -- This procedure must be called successfully before Get_Switch can be used.
               procedure Validate
                 (Endpoint : in      Endpoint_Type;
                  Valid    :     out Boolean;
                  Message  :     out Unbounded_String);
               -- Return the value associated with the given switch. If the switch was not specified
               -- on the command line a suitable default is returned. Switch_Error is raised if
               -- this function is used before Validate is called or if Validate returned a failure
               -- status.
               function Get_Switch(Switch : Switch_Type) return String;
            end Thumper_Switches;

    You may find the description of package Ada.Strings.Unbounded useful.

  4. Modified thumper_client.adb and thumper_server.adb to use this new package to check and obtain switches from the command line and use those switches appropriately.

  5. Be sure to commit your changes to your hw06 branch when you are done. Also be sure to checkout the master branch again before doing further work with Thumper, including pulling updates from the upstream repository. I will post a solution to this assignment in the upstream master that you will eventually pull into your repository and you don't want conflicts!

Submit the files, thumper_switches.adb, thumper_client.adb, and thumper_server.adb in a zip archive to Moodle.

Last Revised: 2015-02-24
© Copyright 2015 by Peter C. Chapin <>