Parsing Arguments

Go To StackoverFlow.com

1

So my quick question to everyone is, would anyone be able to explain to me the basic reason as to why should I parse a argument in the (String[]args)? I'm not very knowledgeable about this topic and would like to get some quick help so I could understand things better.

Thank you!

2012-04-05 00:24
by John V
What does "parse a argument" mean - Travis Webb 2012-04-05 00:27
It's the way the command line arguments to java.exe are exposed to your code - ulmangt 2012-04-05 00:29


3

You're asking why you would do it? Well, when there's a need (or requirement) to do it ofcourse. If your application should, for example, support arguments like -install, -debug or -foo -bar -quick -user=simpson -pass=bart

Similar to the way ls in linux supports arguments like -R for recursive and dir for "Dos" supports /S for recursive. You want to 'parse' these arguments (e.g. "take a look at them, figure out what they mean") to make your application act accordingly.

2012-04-05 00:30
by RobIII
Hi Rob. Thanks for the explanation! So from what I understand on your post, the string arguments must be parsed so that, let's say in linux for example, invoke a flag along with the application? like say: java helloworld recursive = java helloworld -R? if this was not ran through a parsing method, the recursive "flag" would not work - John V 2012-04-05 17:09
I can't make it any simpler than this: the args string[] is passed into main for a reason. When you (e.g. your code) chooses to ignore the args how is the application going to be influenced by these arguments? It's not like there's some magic thing that knows what a random string like '/FOO' or '-WhoopteeDoo' is supposed to do and makes your application act accordingly. It's like creating a sum(a,b) function with a body of return 7. By ignoring the arguments the code is not going to do what you want it to do. You'll need to return a+b. Same goes for args[] passed into main - RobIII 2012-04-05 21:32


1

This is a very convenient way to work with command line applications in java. For instance, if you wanted to create a batch file to run some Java application (maybe it sets up a mail profile on your computer), you can then use the command line arguments to input even standard OS values like the name of the user, etc.

Do you need to do this all the time? Heck no, but just like everything in programming, it's a tool for us to use when it is appropriate. :)

2012-04-05 00:35
by Crandy


0

A typical program to use command line arguments would, for example, test, if a number is prime:

java mytools.IsPrime 1234567 

or a translation program could help to find a translation:

java mytools.Translate Kindergarten

On Windows, you might rarely use the commandline, but if you pass a list of files by drag and drop to your program, the OS translates this action to a list of names, which are passed to your program, so you can operate on them:

java mytools.PrettyPrint PrettyPrint.java Translate.java IsPrime.java
2012-04-05 00:58
by user unknown
Ads