Advertisement
MarlonKuqi

Untitled

Nov 13th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. public class Main {
  2.    
  3.     final static Map<String, Integer> PARAMS = new HashMap<>();
  4.    
  5.     final static String indentAll = "indentAll";
  6.     final static String indentIf = "indentIf";
  7.     final static String indentWhile = "indentWhile";
  8.     final static String indentDo = "indentDo";
  9.     final static String indentFor = "indentFor";
  10.     final static String indentForeach = "indentForEach";
  11.     final static int defaultIndent = 2;
  12.    
  13.     @SuppressWarnings("static-access")
  14.     public static void main(String[] args) throws ParseException, FileNotFoundException {
  15.        
  16.         if (args.length < 1) {
  17.             System.err.println("Aborting: no path to EMF resource provided!");
  18.             return;
  19.         }
  20.        
  21.         String outputFile = "";
  22.         Injector injector = new WhileLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
  23.         Main main = injector.getInstance(Main.class);
  24.        
  25.         /* TODO : gerer ici le parsing des options
  26.          * Les valeurs sont à passer via une Map<String, Integer>
  27.          * pour les indentations et via String output pour le fichier de sortie du -o.
  28.          * Definir les clés de la map en "final static indent<Struct>" (indentIf, indentWhile, etc...)
  29.         */
  30.        
  31.         /*Étape 1 : Définition des options. */
  32.         Options options = new Options();
  33.         Option o = OptionBuilder.withArgName("FILE").hasArg().withDescription("Creates an output file with the name given has an argument.").withLongOpt("output").create('o');
  34.         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");
  35.         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") ;
  36.         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");
  37.         Option help = new Option("help", "Gives a detailed list of the options the user can use for the whc command.");
  38.         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");
  39.         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");
  40.         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");
  41.         /* On les ajoute à notre groupe d'options. */
  42.         options.addOption(o);
  43.         options.addOption(all);
  44.         options.addOption(iF);
  45.         options.addOption(whilE);
  46.         options.addOption(help);
  47.         options.addOption(foR);
  48.         options.addOption(foreach);
  49.         options.addOption(dO);
  50.        
  51.         /*Étape 2 : Analyse de la ligne de commande.*/
  52.         try{
  53.             CommandLineParser parser = new GnuParser();
  54.             CommandLine cmd = parser.parse(options, args);
  55.         /*Etape 3: Récupération et traitement des résultat.*/           
  56.             if(cmd.hasOption("help")){
  57.                 HelpFormatter formatter = new HelpFormatter();
  58.                 formatter.printHelp(args[0] , options);
  59.             }      
  60.             if(cmd.hasOption("all")){
  61.                 PARAMS.put(indentAll, Integer.parseInt(cmd.getOptionValue("all", defaultIndent+"")));
  62.             }
  63.             if(cmd.hasOption("if")){
  64.                 PARAMS.put(indentIf,Integer.parseInt(cmd.getOptionValue("if", defaultIndent + "")));
  65.             }
  66.             if(cmd.hasOption("while")){
  67.                 PARAMS.put(indentWhile, Integer.parseInt(cmd.getOptionValue("while", defaultIndent + "")));
  68.             }
  69.             if(cmd.hasOption("do")){
  70.                 PARAMS.put(indentDo, Integer.parseInt(cmd.getOptionValue("do", defaultIndent + "")));
  71.             }
  72.             if(cmd.hasOption("for")){
  73.                 PARAMS.put(indentFor, Integer.parseInt(cmd.getOptionValue("for", defaultIndent + "")));
  74.             }
  75.             if(cmd.hasOption("foreach")){
  76.                 PARAMS.put(indentForeach, Integer.parseInt(cmd.getOptionValue("foreach", defaultIndent + "")));
  77.             }
  78.             if(cmd.hasOption("o")){
  79.                 outputFile = cmd.getOptionValue("o", args[1].replaceFirst(".wh", ".whpp"));    
  80.             }
  81.                        
  82.            
  83.         }catch(MissingOptionException e){
  84.             /*Vérifie si l'option -help est présente */
  85.               boolean h = false;
  86.               try{
  87.                  Options helpOptions = new Options();
  88.                  helpOptions.addOption("help", "help", false, "Gives a detailed list of the options the user can use for the whc command.");
  89.                  CommandLineParser parser = new PosixParser();
  90.                  CommandLine line = parser.parse(helpOptions, args);
  91.                  if(line.hasOption("h")) h = true;
  92.               }
  93.               catch(Exception ex){ }
  94.               if(!h) System.err.println(e.getMessage());
  95.               /* Affichage de l'aide. */
  96.               HelpFormatter formatter = new HelpFormatter();
  97.               formatter.printHelp(args[0] , options );
  98.               System.exit(1);
  99.         }catch(MissingArgumentException e){
  100.               System.err.println(e.getMessage());
  101.               HelpFormatter formatter = new HelpFormatter();
  102.               formatter.printHelp(args[0] , options );
  103.               System.exit(1);
  104.            }catch(ParseException e){
  105.               System.err.println("Error while parsing the command line: "+e.getMessage());
  106.               System.exit(1);
  107.            }catch(Exception e){
  108.                e.printStackTrace();
  109.             };
  110.         main.runGenerator(args[1], outputFile, PARAMS);
  111.         //TODO décomenté et passer en paramètre la map et input et output
  112.         //main.runGenerator(args[1], outputFile, indentations);
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement