Solved

parsing command line options in Java

Posted on 2011-09-09
14
178 Views
Last Modified: 2012-05-12
i want to write an application

java -jar TEST.jar -u username -h host   etc.....

can someone point me to example of parsing this.
0
Comment
Question by:learningunix
  • 6
  • 5
  • 3
14 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36509297
0
 

Author Comment

by:learningunix
ID: 36509376
I don't have

import org.apache.commons.cli.*;

Is ther any way without inastalling new java packages?
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36509435
public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}
http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
0
 

Author Comment

by:learningunix
ID: 36509452
I am looking something similar to waht we have in C++ GetOpt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36509482
If you organise the args well (as you have) then you should be able to use a Map
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 36509499
You can then look up the argument by name e.g.
import java.util.*;
import java.io.*;

public class ArgParser {
    public static void main(String[] args) {
	System.out.println(ArgParser.getOpts(args));
    }

    private static Map<String, String> getOpts(String[] args) {
	Map<String, String> result = new HashMap<String, String>(args.length);
	for(int i = 0;i < args.length;i+=2) {	
	    result.put(args[i], args[i+1]); // trim the leading '-' character if you want from args[i]
	}
	return result;
    }
}

Open in new window

0
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 125 total points
ID: 36509500
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:learningunix
ID: 36510464
I just downloaded jot-simple.
I'll give it a try and see

so when I create my own jar file do I have to give all class file of jotsimple also ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36510527
>>
I just downloaded jot-simple.
I'll give it a try and see
>>

I thought you didn't want to install extra software..?
0
 

Author Comment

by:learningunix
ID: 36510596
I'll try both and see.

Today is my first day of java programming and trying to do too many things at once and probably jumping a lot.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36510802
>>Today is my first day of java programming

In that case keep it simple: use what i posted and you can go for a more reusable and extensible solution later
0
 

Author Comment

by:learningunix
ID: 36510876
I found one more tool
jargs.gnu which is also good one.
0
 

Author Closing Comment

by:learningunix
ID: 36510892
Thanks, I am planning to use jargs.gnu as it is fairly simple.

Thanks once again for the direction.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36511619
:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 32
solarwind tftp server 2 30
bitbucket vs gitbucket 3 30
maven project error 5 19
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now