Showing posts with label command line arguments. Show all posts
Showing posts with label command line arguments. Show all posts

Friday, 8 April 2011

Using Getopt in java to give command line options in java program

Wouldn't it be nice if we have command line argument processing in Java with options. I mean using flags and all that stuff for my argumens. Example syntax in Student Database Program:-
java myClassName -c Insert -s John -i 7635425 -b CS
-l {Java|C|C++|nothing} -a []
Flags Explanation:-
'c' may have the following options:-Insert, Delete, Update,Search 's' Student name(compulsory)
'i' Student id(compulsory)
'b' Stuent Major(compulsory)
'l' Student Computer Language Proficiency(choice)
'a' Student Achievements(optional)

So this is where Getopt comes handy.
public static void main(String[] args)
{

//string to hold values for the flags -c,-s,-i,-b,-l and -a
String c,s,i,b,l,a;

for(int count=0;args.length;count+)
{
if(!args[count].startsWith("-"))
{
//even numbered arguments should begin with -
System.out.println("improper suntax");
return;
}
//if args[count]==any flag then args[count+1] contains the
//value for that flag
if(args[count].equals("-c")) c=args[++count];
if(args[count].equals("-s")) s=args[++count];
if(args[count].equals("-i")) i=args[++count];
if(args[count].equals("-b")) b=args[++count];
if(args[count].equals("-l")) l=args[++count];
if(args[count].equals("-a")) a=args[++count];
}
//check for compulsory flags
if(c==null | | s==null | | i==null | | b==null | | l==null)
{
System.out.pritnln("Compulsory flags must be given");
return;
}

int id;
try{
id=Interger.parseInt(i);
}catch(NumberFormatException nfe)
{
Sytem.out.println("Id must be numeric");
return;
}
if(c.equalsIgnoreCase("Insert"))
{
.....
}
if(c.equalsIgnoreCase("Delete"))
{
.....
}
//and so on
...
....
}

Thanks

Wednesday, 28 July 2010

Command-Line Parameters in java

Every Java program has a main method with a String[] args parameter. This parameter indicates that the main method receives an array of strings, namely, the arguments specified on the command line.
For example, consider this program:

public class Message
{
public static void main(String[] args)
{
if (args[0].equals("-h"))
System.out.print("Hello,");
else if (args[0].equals("-g"))
System.out.print("Goodbye,");
// print the other command-line arguments
for (int i = 1; i < args.length; i++)
System.out.print(" " + a[i]);
System.out.println("!");
}
}


If the program is called as


java Message -g cruel world

then the args array has the following contents:

args[0]: "-g"

args[1]: "cruel"

args[2]: "world"

The program prints the message

Goodbye, cruel world!

Differing from cpp
In the main method of a Java program, the name of the program is not stored in the args array. For example, when you start up a program as



java Message -h world
from the command line, then args[0] will be "-h" and not "Message" or "java".
While in c++ program name is also stored


Monday, 3 May 2010

Language Fundamentals : The main() Method and command line arguments

The main() method is used as the entry point for a Java application program. All programs must have
a main() method or they cannot be run. The main() method is a method of the class that is executed
to run the program.
Note Importing java.lang The java.lang package is always imported by default and does not need to be imported by an import statement.
For example, if your program's name is MyProgram, then the MyProgram class must be defined in a
file named MyProgram.java. The MyProgram class must have a correctly defined main() method.
A correctly defined main() method has the following form:
public static void main(String[] args) {
// Statements go here
}
The main() method must be declared as public, static, and void.
The void keyword must appear immediately before main().
The public and static keywords may be interchanged.
The main() method has one argument—an array of String arguments. This argument may be defined as
String[] args or String []args or String args[]. The args argument may use any valid
identifier. For example, you can use arg, myArgs, or parms. However, args is standard, and you
should probably stick with it. As a convention, when I refer to args, I'm referring to the argument to a
program's main() method.
The args array is used to access a program's command-line arguments. These arguments are passed
to a program when it is invoked. They are passed as part of the command that is used to invoke the
program.

Note Applets Applets are not required to have a main() method.

For example, to run the MyProgram program, you would enter
java MyProgram
Suppose that you wanted to pass the arguments 2 and 3 to MyProgram. You would invoke it as follows:
java MyProgram 2 3
The String object "2" would be accessed as args[0], and the String object "3" would be accessed as args[1]. If you are a C or C++ programmer—pay attention. Java accesses commandline
arguments using different indices than do C and C++ programs.
The ArgsTest program of Listing 2.1 shows how command-line arguments are accessed using the
args array. When you run the program using the following command line
java ArgsTest this is a test
it displays the following results
args[0] = this
args[1] = is
args[2] = a
args[3] = test
Listing 2.1: The Argstest Program
class ArgsTest {
public static void main(String[] args) {
for(int i=0;i
System.out.println("args["+i+"] = "+args[i]);
}
}
}