Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- final static Map<String, Integer> PARAMS = new HashMap<>();
- final static String indentAll = "indentAll";
- final static String indentIf = "indentIf";
- final static String indentWhile = "indentWhile";
- final static String indentDo = "indentDo";
- final static String indentFor = "indentFor";
- final static String indentForeach = "indentForEach";
- final static int defaultIndent = 2;
- @SuppressWarnings("static-access")
- public static void main(String[] args) throws ParseException, FileNotFoundException {
- if (args.length < 1) {
- System.err.println("Aborting: no path to EMF resource provided!");
- return;
- }
- String outputFile = "";
- Injector injector = new WhileLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
- Main main = injector.getInstance(Main.class);
- /* TODO : gerer ici le parsing des options
- * Les valeurs sont à passer via une Map<String, Integer>
- * pour les indentations et via String output pour le fichier de sortie du -o.
- * Definir les clés de la map en "final static indent<Struct>" (indentIf, indentWhile, etc...)
- */
- /*Étape 1 : Définition des options. */
- Options options = new Options();
- Option o = OptionBuilder.withArgName("FILE").hasArg().withDescription("Creates an output file with the name given has an argument.").withLongOpt("output").create('o');
- Option all = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the general indentation (expressions, not conditional blocks).").withLongOpt("allindent").create("all");
- Option iF = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the indentation of the if blocks.").withLongOpt("ifindent").create("if") ;
- Option whilE = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the indentation of the while blocks.").withLongOpt("whileindent").create("while");
- Option help = new Option("help", "Gives a detailed list of the options the user can use for the whc command.");
- Option dO = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the indentation of the while blocks.").withLongOpt("doindent").create("do");
- Option foR = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the indentation of the for blocks.").withLongOpt("doindent").create("for");
- Option foreach = OptionBuilder.withArgName("INT").hasArg().withType(Integer.class).withDescription("Number of spaces chosen for the indentation of the foreach blocks.").withLongOpt("doindent").create("foreach");
- /* On les ajoute à notre groupe d'options. */
- options.addOption(o);
- options.addOption(all);
- options.addOption(iF);
- options.addOption(whilE);
- options.addOption(help);
- options.addOption(foR);
- options.addOption(foreach);
- options.addOption(dO);
- /*Étape 2 : Analyse de la ligne de commande.*/
- try{
- CommandLineParser parser = new GnuParser();
- CommandLine cmd = parser.parse(options, args);
- /*Etape 3: Récupération et traitement des résultat.*/
- if(cmd.hasOption("help")){
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp(args[0] , options);
- }
- if(cmd.hasOption("all")){
- PARAMS.put(indentAll, Integer.parseInt(cmd.getOptionValue("all", defaultIndent+"")));
- }
- if(cmd.hasOption("if")){
- PARAMS.put(indentIf,Integer.parseInt(cmd.getOptionValue("if", defaultIndent + "")));
- }
- if(cmd.hasOption("while")){
- PARAMS.put(indentWhile, Integer.parseInt(cmd.getOptionValue("while", defaultIndent + "")));
- }
- if(cmd.hasOption("do")){
- PARAMS.put(indentDo, Integer.parseInt(cmd.getOptionValue("do", defaultIndent + "")));
- }
- if(cmd.hasOption("for")){
- PARAMS.put(indentFor, Integer.parseInt(cmd.getOptionValue("for", defaultIndent + "")));
- }
- if(cmd.hasOption("foreach")){
- PARAMS.put(indentForeach, Integer.parseInt(cmd.getOptionValue("foreach", defaultIndent + "")));
- }
- if(cmd.hasOption("o")){
- outputFile = cmd.getOptionValue("o", args[1].replaceFirst(".wh", ".whpp"));
- }
- }catch(MissingOptionException e){
- /*Vérifie si l'option -help est présente */
- boolean h = false;
- try{
- Options helpOptions = new Options();
- helpOptions.addOption("help", "help", false, "Gives a detailed list of the options the user can use for the whc command.");
- CommandLineParser parser = new PosixParser();
- CommandLine line = parser.parse(helpOptions, args);
- if(line.hasOption("h")) h = true;
- }
- catch(Exception ex){ }
- if(!h) System.err.println(e.getMessage());
- /* Affichage de l'aide. */
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp(args[0] , options );
- System.exit(1);
- }catch(MissingArgumentException e){
- System.err.println(e.getMessage());
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp(args[0] , options );
- System.exit(1);
- }catch(ParseException e){
- System.err.println("Error while parsing the command line: "+e.getMessage());
- System.exit(1);
- }catch(Exception e){
- e.printStackTrace();
- };
- main.runGenerator(args[1], outputFile, PARAMS);
- //TODO décomenté et passer en paramètre la map et input et output
- //main.runGenerator(args[1], outputFile, indentations);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement