• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Command Line Application

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:This library may help. http://commons.apache.org/cli/

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to handle duplicate arguments in that?

-bkr -n bakar name -h -n hostname
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the documentation say? What happened when you tried?
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf ok i will do that

but please provide me solution of post above that
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question is too difficult for “beginning”, so I shall move it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic