home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / ADAUTIL / SCAN_COM.ADA < prev    next >
Encoding:
Text File  |  1990-06-28  |  1.3 KB  |  58 lines

  1. -- This example accepts command lines of the form:
  2. --
  3. --      prog [-a] [-b] [arg] ...
  4. --
  5. -- where "prog" is the name of the program, "-a" and
  6. -- "-b" are legal options, and "arg" is some optional
  7. -- argument.
  8. --
  9. with text_handler;      use text_handler;
  10. with text_io;           use text_io;
  11. with arg;
  12. procedure scan_com is
  13.   this: text(127);
  14.   unrecognized_option: exception;
  15.   illegal_argument: exception;
  16. begin
  17.   for i in 2..arg.count loop
  18.  
  19.     set(this, arg.data(i));
  20.       -- convert string argument to text object.
  21.  
  22.     if not empty(this) then
  23.       if value(this)(1) = '-' then
  24.     if length(this) < 2 then
  25.       raise illegal_argument;
  26.         -- argument was just '-' alone.
  27.     end if;
  28.     case value(this)(2) is
  29.       when 'a' =>
  30.         put_line("-a recognized");
  31.       when 'b' =>
  32.         put_line("-b recognized");
  33.       when others =>
  34.         raise unrecognized_option;
  35.           -- argument wasn't "-a" or "-b".
  36.     end case;
  37.       else
  38.     put_line("argument """ & value(this) &
  39.             """ recognized");
  40.       -- argument didn't start with '-'.
  41.       end if;
  42.     else
  43.       raise illegal_argument;
  44.     -- argument was empty somehow.
  45.     end if;
  46.  
  47.   end loop;
  48.  
  49. exception
  50.   when illegal_argument =>
  51.     put_line("Illegal argument");
  52.  
  53.   when unrecognized_option =>
  54.     put_line("Unrecognized option: '" &
  55.         value(this) & ''');
  56.  
  57. end scan_com;
  58.