Chapter 15. ReadArgs

Table of Contents
Introduction
Methods
 

A Y2K virus might shut down our entire network over the new year. As a precaution, we will shut down our entire network over the new year.

 
-- Anonymous  

Introduction

This is a class that reproduces almost faithfully (and some what enhancing) the *AmigaDOS*'s ReadArgs system call. AmigaDOS ReadArgs is a powerfull commands, mainly used to parse command line arguments from user input. But who uses Amiga knows that ReadArgs is even more than that. It is a flexible way to parse text lines with great power and very customizable.

NOTE: ReadArgs is a very complex command present in the Amiga OS.

Even if I have tried to make it as similar as possible, maybe some bugs are liying somewhere. If you are familiar with the AmigaDOS ReadArgs system call and find any bug in my port, please, fill free to contact me at the e-mail addresses shown below, pointing me what ReadArgs should do and why, and I'll try to fix it as soon as possible.

Overview

ReadArgs parses a text string according to a template that is passed to it. A template consists of a list of fields. Fields in the template are separated by commas. To get the results of ReadArgs, you call the afc_readargs_get_by_name() method passing the field name or, alternatively, afc_readargs_get_by_pos() passing the field index value (one entry per field in the template), starting from 0 for the first field and so on...

Exactly what is put in a given entry by ReadArgs depends on the type of field. The default is an AFC string.

Fields can be followed by modifiers, which specify things such as the type of the field. Modifiers are specified by following the field with a '/' and a single character modifier. Multiple modifiers can be specified by using multiple '/'s.

Please, see afc_readargs_parse() for a full list of modifiers and their implementation.

Usage

The use of ReadArgs is quite simple. You define, inside a template, the way ReadArgs should parse your text string, then you just have to call the afc_readargs_parse() method to have it parsed for you. A result code is returned, if it is "0" then the parsersing successfully succeded, while if it is different, then something went wrong.

Here there are some examples:

Example 1

Suppose you want to create a copy shell instructions. You can create a template like this one:

Example 15-1. ReadArgs Example-1: template

SOURCE/A, DEST/A
and (supposing you want to copy fileA to fileB, the text string could be written in these ways:

  • fileA fileB

  • DEST fileB fileA

  • fileB SOURCE fileA

  • SOURCE fileA DEST fileB

As you can see, ReadArgs is incredibly flexible in parsing strings.

Example 2

Suppose now that you want to get in input a list of files and to copy them into a destination directory, maybe prompting the user for overwrite existing files. A good idea could be create a template in which the user can decide before starting the copy process whether he/she wants to be prompted for overwriting or not. You should declare a template like this:

Example 15-2. ReadArgs Example-2: template

SOURCE/M/A, TO/K/A, OVERWRITE/S, NOOVERWRITE/S

In this way, if the user writes a string like:

Example 15-3. ReadArgs Example-2: user input

fileA fileB fileC fileD TO destdir/ OVERWRITE

ReadArgs will return you a vector containing all the files (A,B,C,D) and you'll know that the overwrite process can take place without asking the user any permission.