Say, what is thy name?

4 min read, 961 words

Book III

Say, what is thy name?

"The secret of getting things done is to act!" Dante Alighieri

We shall now be improving upon our greet program, for the version we created is rather unwieldy; indeed, we are required to edit the source code and thereafter recompile the entirety of the program whenever we wish to alter the name of the person being greeted — truly, a most ineffective way of handling this matter. Instead, it would be much more effective to supply the name of the person whom we wish to greet when running the compiled program and one possible way of doing this is using command line arguments.

Command line arguments are commonly seen with programs one is meant to execute from the command line, for they add a convenient way of allowing user input; for example, a command line argument of the Linux tar command would be car — yielding tar caf — wherewith one may create a new tar archive.

Let us hence glance at one possible implementation of a greet program one can supply with a name using a command line argument: —

# greet.b

implement GreetImproved;

include "arg.m";
include "sys.m";
include "draw.m";
sys: Sys;
arg: Arg;

GreetImproved: module {
	init: fn(ctxt: ref Draw->Context, args: list of string);
};

greet: fn(name: string): string;

greet(name: string): string {
	return "Hello, " + name + "\n";
}

init (ctxt: ref Draw->Context, args: list of string) {
	sys = load Sys Sys->PATH;
	arg = load Arg Arg->PATH;
	
	arg->init(args);
	input := arg->arg();
	
	sys->print("%s", greet(input));
}

As you may have noticed, we are calling upon the help of a library we had not used previously, namely arg.m; this library possesses the code which allows us to easily handle command line arguments. On line 8, just as we were required to do with sys.m, we see the creation of a new variable titled arg of the Arg type, which shall shortly be filled with an instance of the Arg library.

The greet function itself has not been altered and one can copy it verbatim from our previously created greet program.

Lines 22 through 25 contain all the declarations and references we are required to use in order for us to be able to properly employ the arg.m library, beginning with storing an Arg instance within the priorly made arg variable on line 22.

Thereafter, on line 24, we execute the init function within the Arg module, to which we supply the args variable, which we have defined within the init function of our current greet.b program. Let us briefly view the Arg module’s code: —

Arg : module
{
	PATH: con "/dis/lib/arg.dis";
	
	init: fn(argv: list of string);
	setusage: fn(usage: string);
	usage: fn();
	opt: fn(): int;
	arg: fn(): string;
	earg: fn(): string;
	
	progname: fn(): string;
	argv: fn(): list of string;
};

The init function herein is rather similar to the one we are using within our greet program, with the sole differences being the omission of the ctext variable and the renaming of args to argv.

Additionally, let us decipher the declaration of the args variable within the init function, for it creates a variable which may store list of string; as we have discussed antecedently, a string’s function, in essence, is to store text. Thus if we wished to store a larger number of strings within a list, we can do so by utilising a list of string data type, which allows one to save strings in a numbered list, wherefrom we can retrieve particular entries. The entries stored within the args variable is the data we enter after typing our program’s name.

Thus, taking our current greet program as an example, if we typed greet Hex into the shell, the first argument will be the program itself — namely greet —, whereas the second argument will be Hex. The arg function will thus return the second argument of our program and ignore the first argument, or those following the second one. If you wished to retrieve the first argument, the function progname should be used instead.

On line 25, the declaration of the input variable can be found with a rather strange looking sign, namely :=; this is used whenever you wish to write both the declaration of a variable and its functions on one line; in essence, it is the amalgamation of : — used to declare a variable — and = — which one uses to confer a particular value to the variable.

The value we herein assign to it is arg->arg(), id est, the output of the arg function within the Arg module — which, as I hope you recall, outputs the first argument provided following the name of the program itself.

Then, lastly, line 27 concludes the program by printing the output of the greet function, whom we provide with the user input stored within the input variable.

However, a large portion of names tend to consist of more than but one word and as our improved greet function only returns the value of merely one of the appended command line arguments, it may not work with scores of different names. Thus, if we wished to work with all affixed arguments, we are required to work with so-called loops.