aspose file tools*
The moose likes Java in General and the fly likes Command Line Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Command Line Application" Watch "Command Line Application" New topic
Author

Command Line Application

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Suppose we have to make Command line application having following command:

mycommand -source ipadress:port -u username:password -destination ipaddress:port -u username:password

How should i write code of above? like first split source different and destination differently

Regards
Azrael Noor
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
This library may help. http://commons.apache.org/cli/

Can you guarantee that neither username nor password will ever contain a colon? If not, then you'll be in trouble. Why are you trying to cram two parameters into one? Why not keep them separate?


Ping & DNS - my free Android networking tools app
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
OK that is fine i will keep it as
-u username -p password

but in simple we will make code like

if(args[0].equalsIgnoreCase("Command")
&& args[1].equalsIgnoreCase("-ss")
&& args[2].equalsIgnoreCase("-dt")
&& args[4].equalsIgnoreCase("-rfa")
&& args[6].equalsIgnoreCase("-u")
&& args[8].equalsIgnoreCase("-dt")
&& args[9].equalsIgnoreCase("-dq")
&& args[11].equalsIgnoreCase("-rfa")
&& args[13].equalsIgnoreCase("-u")
)
{
ProcessThis(args);
}

i mean as command is around 15 to 20 tokens long(as per requirement specification) but is above way fine?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
Ulf Dittmer wrote:This library may help. http://commons.apache.org/cli/
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
How to handle duplicate arguments in that?

-bkr -n bakar name -h -n hostname
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
What does the documentation say? What happened when you tried?
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
No it was my mistake it was taking - (hyphen) as some unicode character so i retyped and worked fine.

so acc to your why it is better to use apache api?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
Because I do not want to deal with multiple or missing parameter, or have to parse a command lne. Why would I, if a library exists to do that for me?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Azrael Noor wrote:so acc to your why it is better to use apache api?

Same reason you would go to the store and buy a pot for cooking rather than make your own pot. What's more, it's open source so you don't even have to pay for it.


Junilu - [How to Ask Questions] [How to Answer Questions]
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
This API is unable to read Command Line Subcommand

For example if i have Subcommand

DATATRANSFERRING DTD -S <Database1> -D <Database2>

Want to transfer database to database and i put on my options:
CommandLineParser parser = new BasicParser();
Options definedOptions = new Options();
definedOptions.addOption("DTD",false, "TQueue");
definedOptions.addOption("qtt",false, "Source");
definedOptions.addOption("s",false, "Destination");
definedOptions.addOption("d",false, "Connection");

When option is added then if i parse
cmd = parser.parse(definedOptions, args);

and if i do:
if (cmd.hasOption("DTD"))
{
return true;
}


It is not reading DTD subcommand and is just taking single character option like -u or -i

Could anyone please help on this?
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Junilu Lacar wrote:
Azrael Noor wrote:so acc to your why it is better to use apache api?

Same reason you would go to the store and buy a pot for cooking rather than make your own pot. What's more, it's open source so you don't even have to pay for it.


Junilu i mean to say why it is better to use Apache API in context of Using Apache API vs other API's

like in Common CLI we have args4j, TE-Code command line parsing, CLAJR (Command-Line Arguments with Java Reflection), JArgs, JSAP (Java Simple Argument Processor) etc.

why person opts Apache?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
If you were to do a compare of those APIs, it would certainly be interesting to hear about your findings.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Ulf ok i will do that

but please provide me solution of post above that
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
I think this question is too difficult for “beginning”, so I shall move it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command Line Application