Command Line Parsing/Arguments

I would like some help with command line parsing i need to be able to

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2205 Views - Last Post: 08 August 2007 - 08:44 AM Rate Topic: ----- 1 Votes

#1 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Command Line Parsing/Arguments

Post icon  Posted 08 August 2007 - 03:58 AM

I am looking for a tool that will allow me to type in a classpath of a directory i would like to perform a search on for jar files, this will then check a database with all matching jar files and produce a report.

I am really stuck on this as this is a project i have been set and really dont know where to start, i understand that i have to start by using command line parsing for the input of the directory. i would appreciate any help that is avail.

thanks

Mich

Is This A Good Question/Topic? 0
  • +

Replies To: Command Line Parsing/Arguments

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 04:29 AM

Command line parsing is done for you, the main method receives the paramteres as a String[], you just have to process them.
Was This Post Helpful? 0
  • +
  • -

#3 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 05:39 AM

View Post1lacca, on 8 Aug, 2007 - 04:29 AM, said:

Command line parsing is done for you, the main method receives the paramteres as a String[], you just have to process them.



Cheers is there any way you can help me get started with the scenario above? i really need to get this started. and it is stressing me out. :-(
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 05:58 AM

You check the number of arguments, if its 0, tell the user to give you the classpath and exit.
If there is an argument, then you try make a File from it, if it gives an error or it doesn't exist, you tell the user about the problem, and exit.
Then you look for .jar files in that folder examples are here.
Finally you do your database thing, here is a tutorial on it.
I guess, it should be enough to get you started.
Was This Post Helpful? 0
  • +
  • -

#5 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 06:04 AM

View Post1lacca, on 8 Aug, 2007 - 05:58 AM, said:

You check the number of arguments, if its 0, tell the user to give you the classpath and exit.
If there is an argument, then you try make a File from it, if it gives an error or it doesn't exist, you tell the user about the problem, and exit.
Then you look for .jar files in that folder examples are here.
Finally you do your database thing, here is a tutorial on it.
I guess, it should be enough to get you started.



Thanks you are the best x
Was This Post Helpful? 0
  • +
  • -

#6 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 06:21 AM

View Postmichren, on 8 Aug, 2007 - 06:04 AM, said:

View Post1lacca, on 8 Aug, 2007 - 05:58 AM, said:

You check the number of arguments, if its 0, tell the user to give you the classpath and exit.
If there is an argument, then you try make a File from it, if it gives an error or it doesn't exist, you tell the user about the problem, and exit.
Then you look for .jar files in that folder examples are here.
Finally you do your database thing, here is a tutorial on it.
I guess, it should be enough to get you started.



Thanks you are the best x



Can you have a look at this code and let me know if i am on the right lines because i cant get it to compile or work? please :-)

public class Main {

public static void main (String[] args) {
//System.out.println ();
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new Stipublic class Main {

public static void main (String[] args) {
//System.out.println ();
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new StickFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}
ckFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}
Was This Post Helpful? 0
  • +
  • -

#7 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 06:52 AM

public class Main {

public static void main (String[] args) {
//System.out.println (); 
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new Stipublic class Main {

public static void main (String[] args) {
//System.out.println ();
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new StickFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}
ckFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}

[qpublic class Main {

public static void main (String[] args) {
//System.out.println ();
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new Stipublic class Main {

public static void main (String[] args) {
//System.out.println ();
//System.out.println (" "+ args [0]);
//System.out.println ("Please Type in Main " +args [1]);
//}
//}
//do we have the correct number of arguments?
if (args.length != 1) {
System.out.println("Please enter Main");
System.exit(1);
}


//has the user specified male
if (args[0].equalsIgnoreCase("main")) {

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter)



} else if (args[0].equalsIgnoreCase("")) { //has the user specified female

// StickFigureDrawApp artist = new StickFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}
ckFigureDrawApp();
//artist.drawStickFigureFemale();

}
else { // if not show error message prompting them to do so [male|female]
System.out.println("Please enter main.java");
System.exit(1);
}

}

}


This post has been edited by 1lacca: 08 August 2007 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:11 AM

Please use code tags around source codes, because it preserves indentation, and it makes it easier to read (I've added code tags in your last post).
So first thing:
if (args[0].equalsIgnoreCase("main")) won't work as you expect it to, because in java the first (at the 0th index) argument is the first argument, not the program's name - unlike c++.

File dir = new File("directoryName"); this is where you want to use the user provided argument.
After that I see that you've copied the examples on the link I've provided without having a look at them. This won't work. Study the code, and customize it, so it fits your needs. We won't provide here complete solutions, the main goal of DIC is to help you LEARN! We only provide guidance, where you can find the knowledge, but not the solution.
Was This Post Helpful? 0
  • +
  • -

#9 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:21 AM

View Post1lacca, on 8 Aug, 2007 - 07:11 AM, said:

Please use code tags around source codes, because it preserves indentation, and it makes it easier to read (I've added code tags in your last post).
So first thing:
if (args[0].equalsIgnoreCase("main")) won't work as you expect it to, because in java the first (at the 0th index) argument is the first argument, not the program's name - unlike c++.

File dir = new File("directoryName"); this is where you want to use the user provided argument.
After that I see that you've copied the examples on the link I've provided without having a look at them. This won't work. Study the code, and customize it, so it fits your needs. We won't provide here complete solutions, the main goal of DIC is to help you LEARN! We only provide guidance, where you can find the knowledge, but not the solution.


Sorry im new to all of this, this is way over my head! cheers for your help ;-)
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:24 AM

Also, I suppose that you've also pasted things from the attribute parsing - male or female. Again: have a look at the code, check your notes / books, try to find out what is happening, and modify it.

This post has been edited by 1lacca: 08 August 2007 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#11 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:28 AM

View Post1lacca, on 8 Aug, 2007 - 07:24 AM, said:

Also, I suppose that you've also pasted things from the attribute parsing - male or female. Again: have a look at the code, check your notes / books, try to find out what is happening, and modify it.



Iv worked out that i just have the problem of structuring it, its not the customisation, i just dont know where to put things and i dont have any tutors or anything on hand to guide me (self taught). a bit like building a house i have the materials i just dont know how to structure it. if you get what i mean. x
Was This Post Helpful? 0
  • +
  • -

#12 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:37 AM

I see.
Then first, only do the argument parsing.
Once it is ready and works as expected, add the file processing part.
Once it is done and works flawlessly add the database access.
It is always easier to concentrate on only one thing, that hopefully only needs just a little new knowledge, than doing the whole thing at once, overwhelmed by the problems, and getting lost in it hopelessly.
Also, I can't stress it enough times: try to run the examples first, understand how they work, experiment with them, and only than insert them into your code. If they don't work as posted on a page, then try to complete them, so that they are actually functional.
Was This Post Helpful? 0
  • +
  • -

#13 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:49 AM

View Post1lacca, on 8 Aug, 2007 - 07:37 AM, said:

I see.
Then first, only do the argument parsing.
Once it is ready and works as expected, add the file processing part.
Once it is done and works flawlessly add the database access.
It is always easier to concentrate on only one thing, that hopefully only needs just a little new knowledge, than doing the whole thing at once, overwhelmed by the problems, and getting lost in it hopelessly.
Also, I can't stress it enough times: try to run the examples first, understand how they work, experiment with them, and only than insert them into your code. If they don't work as posted on a page, then try to complete them, so that they are actually functional.



Is there any tutorial apart from the java one that specifically concentrates on the parsing of class paths and if there is no argument passed it defaults directly to the current working directory, i have been here all day and have made no progression it is so disheartening, im so glad of your advice. x
Was This Post Helpful? 0
  • +
  • -

#14 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 07:59 AM

When you are talking about classpath parsing, do you mean a simple directory, or more like ; separated list like the one the java -cp (classpath) expects?
In the first case the File class will do the parsing for you, and throws an exception if it is a malformed path. In the second just use a Stringtokenizer, and do just like before for each tokens.
Defaulting to the working directory must be done by you, so that if no argument is provided, you set the classpath to the working directory. Getting the working directory.
Was This Post Helpful? 0
  • +
  • -

#15 michren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 07

Re: Command Line Parsing/Arguments

Posted 08 August 2007 - 08:10 AM

View Post1lacca, on 8 Aug, 2007 - 07:59 AM, said:

When you are talking about classpath parsing, do you mean a simple directory, or more like ; separated list like the one the java -cp (classpath) expects?
In the first case the File class will do the parsing for you, and throws an exception if it is a malformed path. In the second just use a Stringtokenizer, and do just like before for each tokens.
Defaulting to the working directory must be done by you, so that if no argument is provided, you set the classpath to the working directory. Getting the working directory.



My aim is to get a java tool that will search through a desired directory for jar files, so on the command prompt i would like to enter in the class path of that directory i want to search for the jar files, it then looks in the database (that i will create) with all existing jar files and produce all information about every jar file in that directory. i am literally soo confused with how to go about it and i dont have long to get this cracked as i have wasted a day already. I am a novice to all this but i have been set the task so i have to do it really, any suggestions on how to go about this? is it command line parsing that i have to work with in order to read in the class path then to analyse the file?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2