Simple Java Command Line Argument Parser Implementation
Here, we will create a Custom CommandLineParser class to parse the arguments passed via the command line in Java.
Let’s first see the sample entry point for the Java program.
public class MyProgram public static void main(String args[]) System.out.println("This is my program."); > >
Ever wondered why do we need to pass an array of strings as arguments in main method of Java?
It’s for the command line arguments. When you run your Java program from the command line:
It takes «Oshan» as an argument. So, if you want your program to access this String , you can access it using the array of String passed in the main method.
// inside main method //Prints "My name is Oshan" System.out.println("My name is ", args[0]);
You can pass the initial configuration you want your Java program to follow via command-line arguments. For example:
## if you want to send file path java MyProgram -filePath file1.txt ## if you want to send multiple file paths java MyProgram -filePath file1.txt file2.txt ## or, if you just want to send flags java MyProgram -noprint
By default, Java takes each string as an argument and cannot differentiate between argument names, values, or flags. So, today, we’ll write a simple program that will parse the arguments passed through the command line. This way we don’t have to hard code the values of the arguments as args[1] or args[2] in our program. Instead, we will be able to see the argument list and choose values using argument names rather than an array of strings. Here is the code:
import java.util.HashMap; import java.util.HashSet; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; public class CommandLineParser List String> args = new ArrayList<>(); HashMapString, ListString>> map = new HashMap<>(); SetString> flags = new HashSet<>(); CommandLineParser(String arguments[]) this.args = Arrays.asList(arguments); map(); > // Return argument names public SetString> getArgumentNames() SetString> argumentNames = new HashSet<>(); argumentNames.addAll(flags); argumentNames.addAll(map.keySet()); return argumentNames; > // Check if flag is given public boolean getFlag(String flagName) if(flags.contains(flagName)) return true; return false; > // Return argument value for particular argument name public String[] getArgumentValue(String argumentName) if(map.containsKey(argumentName)) return map.get(argumentName).toArray(new String[0]); else return null; > // Map the flags and argument names with the values public void map() for(String arg: args) if(arg.startsWith("-")) if (args.indexOf(arg) == (args.size() - 1)) flags.add(arg.replace("-", "")); > else if (args.get(args.indexOf(arg)+1).startsWith("-")) flags.add(arg.replace("-", "")); > else //List of values (can be multiple) ListString> argumentValues = new ArrayList<>(); int i = 1; while(args.indexOf(arg)+i != args.size() && !args.get(args.indexOf(arg)+i).startsWith("-")) argumentValues.add(args.get(args.indexOf(arg)+i)); i++; > map.put(arg.replace("-", ""), argumentValues); > > > > >
Now, you can add the above class to any program you want and easily parse the values.
Let’s try with a simple program:
public class MyProgram public static void main (String args[]) CommandLineParser clp = new CommandLineParser(args); > >
javac -classpath /pathToTheCommandLineParserClass MyProgram.java java MyProgram.java -name Oshan -prints 3 -test
// inside main method String name = clp.getArgumentValue("name"); int prints = Integer.parseInt(clp.getArgumentValue("prints")); boolean isTest = clp.getFlag("test"); if(isTest) System.out.println("This is only a test"); for(int i = 0; i prints; i++) System.out.print(name+" "); >
This is only a test Oshan Oshan Oshan
You can also use the method getArgumentNames to see the passed argument names.
I recently encountered a problem that made me write this custom CommandLineParser . Obviously, you can modify this to fit your requirements or go with the traditional approach. But, making such class removes the ambiguity when multiple arguments are passed via command line.
I hope you find this useful. Thanks for reading.
Java Method Arguments
What does this mean? How do I change a parameter? This short tutorial will help you figure out how parameter passing in Java works and will help you avoid some common mistakes.
First let’s get the terminology straight. The terms «arguments» and «parameters» are used interchangeably; they mean the same thing. We use the term formal parameters to refer to the parameters in the definition of the method. In the example that follows, x and y are the formal parameters.
We use the term actual parameters to refer to the variables we use in the method call. In the following example, length and width are actual parameters.
// Method definition public int mult(int x, int y) < return x * y; >// Where the method mult is used int length = 10; int width = 5; int area = mult(length, width);
Pass-by-value means that when you call a method, a copy of each actual parameter (argument) is passed. You can change that copy inside the method, but this will have no effect on the actual parameter. Unlike many other languages, Java has no mechanism to change the value of an actual parameter. Isn’t this very restrictive? Not really. In Java, we can pass a reference to an object (also called a «handle»)as a parameter. We can then change something inside the object; we just can’t change what object the handle refers to.
Passing Primitive Types
Java has eight primitive data types: six number types, character and boolean. When any variables of these data types are passed as parameters to a method, their values will not change. Consider the following example:
public static void tryPrimitives(int i, double f, char c, boolean test) < i += 10; //This is legal, but the new values c = 'z'; //won't be seen outside tryPrimitives. if(test) test = false; else test = true; >
int ii = 1; double ff = 1.0; char cc = ‘a’; boolean bb = false; tryPrimitives(ii, ff, cc, bb); System.out.println(«ii = » + ii + «, ff = » + ff + «, cc = » + cc + «, bb figure3.gif» HEIGHT=194 WIDTH=426 ALIGN=RIGHT> What’s really happening here? When Java calls a method, it makes a copy of its actual parameters and sends the copies to the method where they become the formal parameters. Then when the method returns, those copies are discarded and the variables in the main code are the same as before.
Passing Object References
What if an argument to a method is an object reference? We can manipulate the object in any way except we cannot make the reference refer to a different object. Suppose we have defined the following class: class Recordpublic static void tryObject(Record r)
In some other code we can create an object of our new class Record , set its fields, and call the method tryObject .
Record Record(); id.num = 2; id.name = "Barney"; tryObject(id); System.out.println(id.name + " " + id.num);
Note that the object’s instances variables are changed in this case. Why? The reference to id is the argument to the method, so the method cannot be used to change that reference; i.e., it can’t make id reference a different Record . But the method can use the reference to perform any allowed operation on the Record that it already references.
Side Note: It is often not good programming style to change the values of instance variables outside the object. Normally, the object would have a method to set the values of its instance variables.
We cannot however make the object parameter refer to a different object by reassigning the reference or calling new on the reference. For example the following method would not work as expected:
public void createRecord(Record r, int n, String name)
We can still encapsulate the initialization of the Record in a method, but we need to return the reference.
public Record createRecord(int n, String name)
Passing Strings
When we write code with objects of class String, it can look as if strings are primitive data types. For example, when we assign a string literal to a variable, it looks no different than, say, assigning an number to an int variable. In particular, we don’t have to use new . In fact, a string is an object, not a primitive. A new is required, and Java does it behind the scenes. It creates a new object of class String and initializes it to contain the string literal we have given it.
String str = "This is a string literal.";
Because str is an object we might think that the string it contains might be changed when we pass str as a parameter to a method. Suppose we have the method tryString :
public static void tryString(String s)
tryString(str); System.out.println(«str This is a string literal.» Why is this? Maybe this picture will help you understand what goes on behind the scenes. It is important to remember what Java does when it assigns a string literal to an object. A different String object is created and the reference variable is set to point to the newly created object. In other words the value of the formal parameter, s, has changed, but this does not affect the actual parameter str. In this example, s pointed to the string object that contains «This is a string literal». After the first statement of the method executes, s points to the new string, but str has not changed. Like other objects, when we pass a string to a method, we can in principle, change things inside the object (although we can’t change which string is referenced, as we just saw). However, this capability is not useful with string because strings are «immutable». They cannot be changed because the String class does not provide any methods to modify its local variables.
Questions
A: Pass-by-value in Java means B: Given the following method: public void swap(int x, int y)int p = 20; int q = 15; swap(p, q);
We have to use a bit of a trick to write a swap routine that works.
Return values
Okay, so how do we change the values of variables inside methods? One way to do it is simply to return the value that we have changed. In the simple example below, we take two integers, a and b , as parameters and return a to the power of b .
public static int power(int a, int b)
int number = 2; int exponent = 4; number = power(number, exponent); System.out.println("New value of number is " + number);
Since methods should be designed to be simple and to do one thing, you will often find that returning a value is enough, and that you don’t need to change the value of any parameters to the method. Note that this does not help us write a swap method because a method can only return one value, and to write swap we need to change the value of two variables.
Question
public int tryPrimitives(int x, int y)
int p = 1; int q = 2; int r = 5; r = tryPrimitives(p, q);
Passing Arrays
Arrays are references. This means that when we pass an arrays as a parameter, we are passing its handle or reference. So, we can change the contents of the array inside the method.
public static void tryArray(char[] b)
char[] a = ; tryArray(a); System.out.println(«a[0] = » + a[0] + «, a[1] = » + a[1] + «, a[2] a[0] = x, a[1] = y, a[2] = z».
Question
D: Given the following method: public void differentArray(float[] x)float[] xx = new float[100]; x[0] = 55.8f; differentArray(xx); System.out.println(«x[0] webtutor.zip» CODE=»Question.class» CODEBASE=»http://www.dgp.utoronto.ca/webtutor/bin/» WIDTH=400 HEIGHT=210>
Summary
That, combined with an understanding of the different between primitives and references and careful tracing will allow you to understand any Java method calls.