Merge pull request #14 from ThomasRubini/main_app
Added a parser to get the distribution the user want
This commit is contained in:
commit
3f76bb8bc5
@ -40,10 +40,11 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
bundle 'org.json:json:20220924'
|
||||
bundle 'com.beust:jcommander:1.78'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
}
|
||||
|
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,10 @@
|
||||
package fr.packageviewer;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
// send the command line arguments to the parser
|
||||
ArgParse.parseArguments(args);
|
||||
System.out.println(ArgParse.getDistribution());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user