pike.ida.liu.se
Not logged in

Before we let the program actually do anything with the URL we give to it, we will improve the program to let it take the URL as a command-line argument. That is, we want to start the program with the command

webbrowser.pike http://pike.ida.liu.se/

instead of letting the program ask for the URL. We add some code for this:

#! /usr/local/bin/pike

// The Very Simple World Wide Web Browser

int main(int argc, array(string) argv)
{
  write("Welcome to the Very Simple WWW Browser!\n");
  string url;
  if(argc == 2)
    url = argv[1];
  else
  {
    write("Type the address of the web page:\n");
    url = Stdio.stdin->gets();
  }
  write("URL: " + url + "\n");
  return 0;
} // main

This program can handle a command-line argument, but if you don't give one, it will ask for an URL just as before.

There are some new things to explain here:

  • The line #! /usr/local/bin/pike only works on a Unix system, as described earlier. You can remove it if you are using Windows NT.

  • We have added int argc, array(string) argv as parameters to main. These parameters are like normal variables, except that they will be assigned some values automatically when the program starts.

  • The variable argc has the type int, which means integer. It will contain the number of command-line arguments. The program name is counted as an argument, so if you give one argument to the program, argc will have the value 2

  • The variable argv will contain the arguments. Its type is array(string), which means array of strings. Each command-line argument is put in a string, and together they form a list or array.

  • argc == 2 is a comparison. We check if the value of argc is 2. == is an operator that checks if two things are the same. Some other comparison operators in Pike are < (less than), >= (greater than or equal), and != (not same).

  • argv[1] retrieves an element from position number 1 inside an array. The positions are numbered from 0, so position 1 is actually the second element in the array. This is the URL given as argument to the program. argv[0] contains the name of the program, the string "webbrowser.pike".

  • The if statement lets Pike choose between different actions. It follows the template or pattern

    if( condition )
      something
    else
      something-else

    If the condition (in our case, that argc == 2) is true, Pike will do the something. If the condition is false, it will do the something-else. In our program, there are two statements in the something-else, so we must enclose it in curly brackets, { and }.