Command Line Parsing/ArgumentsI would like some help with command line parsing i need to be able to
15 Replies - 1708 Views - Last Post: 08 August 2007 - 08:44 AM
#1
Command Line Parsing/Arguments
Posted 08 August 2007 - 03:58 AM
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
Replies To: Command Line Parsing/Arguments
#2
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 04:29 AM
#3
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 05:39 AM
1lacca, on 8 Aug, 2007 - 04:29 AM, said:
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. :-(
#4
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 05:58 AM
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.
#5
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 06:04 AM
1lacca, on 8 Aug, 2007 - 05:58 AM, said:
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
#6
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 06:21 AM
michren, on 8 Aug, 2007 - 06:04 AM, said:
1lacca, on 8 Aug, 2007 - 05:58 AM, said:
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);
}
}
}
#7
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
#8
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:11 AM
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.
#9
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:21 AM
1lacca, on 8 Aug, 2007 - 07:11 AM, said:
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 ;-)
#10
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:24 AM
This post has been edited by 1lacca: 08 August 2007 - 07:24 AM
#11
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:28 AM
1lacca, on 8 Aug, 2007 - 07:24 AM, said:
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
#12
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:37 AM
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.
#13
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:49 AM
1lacca, on 8 Aug, 2007 - 07:37 AM, said:
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
#14
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 07:59 AM
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.
#15
Re: Command Line Parsing/Arguments
Posted 08 August 2007 - 08:10 AM
1lacca, on 8 Aug, 2007 - 07:59 AM, said:
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?
|
|

New Topic/Question
Reply




MultiQuote




|