This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command Line Application

 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:This library may help. http://commons.apache.org/cli/
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to handle duplicate arguments in that?

-bkr -n bakar name -h -n hostname
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the documentation say? What happened when you tried?
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7321
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were to do a compare of those APIs, it would certainly be interesting to hear about your findings.
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf ok i will do that

but please provide me solution of post above that
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this question is too difficult for “beginning”, so I shall move it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic