The code for this command line is very shortonly about 100 lines. This is because it's pretty bare bones. Here are some ideas for making this code more useful.
This command line syntax lets you use a sequence of words, such as "g i", to implement a specific command. Expressing this as "g -i" would be more Unix-like, so this might be a useful modification. You can implement this in a couple of ways:
- Strip the dashes from the options and call the g_i() function.
- Have a single function, g(), that is responsible for interpreting the options.
One solution to this problem is to use a prefix for handler names: bcl_g(), bcl_g_i(), and so on. This could be made mandatory, or it could be optional. If it's optional, the code would have to check for the existence of bcl_g(). If it didn't exist, the code could then check for g().
Commands in Separate Files
The parser is very simple, separating the command into words by breaking it at whitespace. But consider the following command:
% al "hello world!"
This doesn't work right. It should display an alert box with the words "hello world!", but in fact it displays only the world "hello" (see Figure 4). The parser breaks the command up into the following words:
|Figure 4. Parser Breaks Up the "hello world!" Command|
Although both arguments are passed to al(), the al() function recognizes only the first argument, so that's what it displays.
This problem can be solved using a more sophisticated parserone that recognizes quotation marks. But this in turn requires a syntax for escaping quotation marks. A full command line parser has many elementsfeel free to add what you need.
A Scripting Language for a Scripting Language
Command lines are useful for a number of reasons. It's much easier to run new code by calling it from a handler than it is to create a GUI to call it. A command line is a great way to try out new code before you go to the trouble of implementing a real interface. Command lines are also useful for impatient people, who don't like to use the mouse any more than necessary.
The browser command line is, in a sense, a scripting language for a scripting language.