added working arguments parser with doc
This commit is contained in:
		
							parent
							
								
									bc5b41122c
								
							
						
					
					
						commit
						9541ab5718
					
				
							
								
								
									
										47
									
								
								src/main/java/fr/packageviewer/ArgParse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/fr/packageviewer/ArgParse.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,47 @@ | ||||
| package fr.packageviewer; | ||||
| 
 | ||||
| import com.beust.jcommander.JCommander; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * Class to parse the command line arguments given by the user | ||||
|  * @author Capelier-Marla | ||||
|  */ | ||||
| public class ArgParse { | ||||
| 
 | ||||
|     /* distribution the user want */ | ||||
| 	private static String distribution; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Get the command line argument given by the user, parse it with the parser and store it in the corresponding variable | ||||
| 	 * @author Capelier-Marla | ||||
| 	 * @param args the command line arguments given by the user | ||||
| 	 * @return void | ||||
| 	 */ | ||||
| 	static void parseArguments(String[] args) { | ||||
| 		// create JCommander and CommandLineParams objects | ||||
| 		JCommander jCommander = new JCommander(); | ||||
| 		CommandLineParams params = new CommandLineParams(); | ||||
| 		// add argument required by the params to the JCommander object | ||||
| 		jCommander.addObject(params); | ||||
|         try { | ||||
|             // parse the argument from list of String | ||||
|             jCommander.parse(args); | ||||
|             // store the argument parsed in the variable | ||||
|             distribution = params.distribution; | ||||
|         } catch (Exception e) { | ||||
|             // if the parsing failed, print the error message and exit the program | ||||
|             System.out.println("You forgot the distribution name."); | ||||
|             System.exit(0); | ||||
|         } | ||||
| 	} | ||||
| 
 | ||||
|     /** | ||||
|      * Get the distribution name. If the user didn't give any or we didn't parsed, return null | ||||
|      * @author Capelier-Marla | ||||
|      * @return String: the distribution name | ||||
|      */ | ||||
|     public static String getDistribution() { | ||||
|         return distribution; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										26
									
								
								src/main/java/fr/packageviewer/CommandLineParams.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/main/java/fr/packageviewer/CommandLineParams.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| package fr.packageviewer; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import com.beust.jcommander.Parameter; | ||||
| 
 | ||||
| /** | ||||
|  * Class to store and get the command line arguments given by the user | ||||
|  * @author Capelier-Marla | ||||
|  */ | ||||
| public class CommandLineParams { | ||||
|     /** | ||||
|      * List of parameters given by the user | ||||
|      */ | ||||
|     @Parameter | ||||
|     public List<String> parameters = new ArrayList<>(); | ||||
| 
 | ||||
|     /** | ||||
|      * Distribution the user want to search packages in | ||||
|      */ | ||||
|     @Parameter(names = {"--distro", "-d"}, | ||||
|                description = "Linux distribution to search in", | ||||
|                required = false) | ||||
|     public String distribution; | ||||
| } | ||||
| @ -1,7 +1,33 @@ | ||||
| package fr.packageviewer; | ||||
| 
 | ||||
| import com.beust.jcommander.JCommander; | ||||
| 
 | ||||
| public class Main { | ||||
| 	public static void main(String[] args) { | ||||
| 		System.out.println("Hello world!"); | ||||
| 
 | ||||
| 	/* distribution the user want */ | ||||
| 	private static String distribution; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Get the command line argument given by the user, parse it with the parser and store it in the corresponding variable | ||||
| 	 * @author Capelier-Marla | ||||
| 	 * @param args the command line arguments given by the user | ||||
| 	 * @return void | ||||
| 	 */ | ||||
| 	static void parseArguments(String[] args) { | ||||
| 		// create JCommander and CommandLineParams objects | ||||
| 		JCommander jCommander = new JCommander(); | ||||
| 		CommandLineParams params = new CommandLineParams(); | ||||
| 		// add argument required by the params to the JCommander object | ||||
| 		jCommander.addObject(params); | ||||
| 		// parse the argument from list of String | ||||
| 		jCommander.parse(args); | ||||
| 		// store the argument parsed in the variable | ||||
| 		distribution = params.distribution; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 	public static void main(String[] args) { | ||||
| 		// send the command line arguments to the parser | ||||
| 		parseArguments(args); | ||||
| 		System.out.println(distribution); | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user