Close Window

Show Source |    | About   «  3.1. Command Line Basics   ::   Contents   ::   3.3. Using Parameters in Eclipse  »

3.2. Parsing Command Line Parameters In Your Progam

3.2.1. Parameters In Programming

Alright I get the idea of parameters for a function, that's easy enough. But how does a Program itself have parameters? This comes in the Main function that every Java program has. Consider this the "launch" function. Usually the "main" function is named Main and takes an array of strings as its parameter (see below).

public static void main(String[] args)

So when you launch the command from the terminal it passes in an array of all the additional information (and it usually trims white space). So if I run

ls with paramaters

Then args is an array containing:

{"-l", "file.txt"}

*Authors Note: This is true for Java, but is a little different for C or C++. In C, the first parameter that you are given is always the command name so your array in C would be:

{"ls", "-l", "file.txt"}

Java however, removes the command name and only provides parameters.

I have created a main function skeleton for you to use in your projects. It is set up simply and makes use of switch cases. You can download it. Let's see how it works.

public class Main {

    * @param args
   public static void main(String[] args) {
      int count = 0;//Set our counter
      if(args.length < 1){
         //No parameters is this a problem?
      while(count < args.length){//While we have not reached the end of our arguments keep going.
            case "-h"://Display help info
               System.out.println("This is the help message. Proper command syntax:");
               System.out.println("cmdline -v: Displays version information");
               System.out.println("cmdline -h: Displays this help message");
               System.out.println("cmdline -f [file]: Sets file to file provided");
            case "-v"://Display version information
               System.out.println("Cmdline parse sample version 1.0.0");
            case "-f"://This is an example to show you how to handle when you have a parameter that takes info
               String fileName = args[count+1];//We are getting the filename so set it to the string after -f
               System.out.println("Input file is "+fileName);//Print out info
               count = count+2;//Increment counter to next item (skipping filename).
               //Note this provides no bounds checking so if you pass the parameter without file info it may bomb if at the end
               //You may also surround it in a try/catch for safety.
            default://If none of your cases match then this is an unrecognized parameter and we will exit.
               System.out.println("Unrecognized parameter "+args[count]+"\nExiting.");


We have our main function take an array of Strings (named args). If that array is empty then we may or may not want to exit as we have no parameters. It will then progress into a while loop that iterates through all parameters. The syntax of this loop is useful as it does not lock parameters into any fixed order. You can invoke them anyway you wish. The switch case statement allows you to easily write for any parameter and add a case for unrecognized parameters by using the default case for any non matching parameters. In this example I choose to exit after printing the unrecognized string. In the future you may wish to change this to something else. You can learn more about switch statements. It is also important to note that this function will possibly throw an exception or behave in an unexpected way if you use "-f" but do not give a file name. For example you call the program with {"-f", "-v"}, this will set the filename to -v. Or if you call the program with {"-f"}, you will get an exception for trying to access outside the array bounds. You can prevent this by using a try catch, but for simplicity's sake I chose to make example very simple. Once you have your parameters set, you can call the appropriate methods to launch your program. There you go simple command line parsing!

   «  3.1. Command Line Basics   ::   Contents   ::   3.3. Using Parameters in Eclipse  »

Close Window