Programming AmigaOS in C

Reading command line arguments

In C the normal method is to use the argc (argument count) and argv (argument values) passed from the main() function. In AmigaOS there are more advanced functionality to pass arguments. This uses command line template which allows you to specify keywords, switches, multiple values, numbers or strings and mandatory or optional arguments.

/N = Arguemnt is required and must be supplied
/F = Final. Must be the last arguement in the line.
/K = Keywork. Must be entered with the parameter
/M = Multiple aguments may be supplied.
/N = A numeric valus is expected.
/S = A Switch of a function may be supplied.
/T = A Toggle option but causes bootean value to toggle over between values.
/A = A required keyword must be specified.

You can view the template of any AmigaOS command by entering a question mark (?) after the command name.
In C, you can define your template as follows:

char template[50]; /* create a string variable */
LONG params[4]; /* Number of parameters */ 
strcpy (template,  "DIR,NAMES/S,DATE/S,SIZE/S"); /* define a template as a string, arguments are seperated by commas */
for (c=0; c<4; c++)
 params[c] = 0; /* clear parameter list */

To read the arguments from the command line, use the ReadArgs() function. You need to supply the template string, a parameter array and an optional pointer to RDArgs structure to control ReadArgs, this can be NULL must of the time. The function will return a pointer to a RDArgs structure.

struct RDArgs *rd;
rd = ReadArgs (template, params, NULL);

To check if a parameter has been specified with the command, you can check the value of the appropiate params[] value, and take appropiate actions. E.g.

 if ((params[1]))
   PutStr("NAMES options specified.\n");

Once you have finished with the parameters, you need to use the FreeArgs() function. Supply the RDArgs pointer as a parameter.


Here is a full example listing using ReadArgs(). For AmigaOS 4, prefix AmigaOS functions with IDOS->.

#include <proto/dos.h>
#include <dos/dos.h>
#include <string.h>
#include <stdio.h>
int main(void) 
   struct RDArgs *rd;
   char template[50], out[100];
   LONG params[4];
   int c;
   strcpy(template, "DIR,NAMES/S,DATE/S,SIZE/S");
   for (c=0; c<4; c++)
   params[c] = 0;
   rd = ReadArgs(template, params, NULL);
   if (rd) {
   if ((params[0])) {
   sprintf(out, "DIR is %s\n", (STRPTR)params[0]);
   if ((params[1]))
     PutStr("NAMES options specified.\n");
   if ((params[2]))
     PutStr("DATE option specified.\n");
   if ((params[3]))
     PutStr("SIZE options specified.\n");
     PutStr("Format: ReadArgs DIR NAMES/S DATE/S SIZE/S\n");
Other useful tips