add command line options - fixes #303

Fri, 06 Oct 2023 19:35:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 06 Oct 2023 19:35:07 +0200
changeset 81
ea79a383ad75
parent 80
e55f318a8b8b
child 82
85f5188acc21

add command line options - fixes #303

src/main/java/de/unixwork/uwproj/Main.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/unixwork/uwproj/Main.java	Mon Oct 02 18:07:37 2023 +0200
+++ b/src/main/java/de/unixwork/uwproj/Main.java	Fri Oct 06 19:35:07 2023 +0200
@@ -8,6 +8,10 @@
 
 public class Main {
 
+    public final static String IN_FILE_DEFAULT = "make/project.xml";
+    public final static String TPL_FILE_DEFAULT = "make/configure.vm";
+    public final static String OUT_FILE_DEFAULT = "configure";
+
     static Project loadProjectFile(String fileName) {
         try {
             return new Project(new File(fileName));
@@ -29,10 +33,61 @@
         new VelocityEngine().getTemplate(tplFileName).merge(context, out);
     }
 
+    private static void printUsage() {
+        System.err.println("Usage: uwproj [OPTIONS]");
+        System.err.println();
+        System.err.println("OPTIONS:");
+        System.err.println("  -h --help              Print help text and exit");
+        System.err.println("  -o --output <file>     Path to the output file");
+        System.err.printf("                         (default: %s)\n", OUT_FILE_DEFAULT);
+        System.err.println("  -p --project <file>    Path to the project.xml file");
+        System.err.printf("                         (default: %s)\n", IN_FILE_DEFAULT);
+        System.err.println("  -t --template <file>   Path to the velocity template");
+        System.err.printf("                         (default: %s)\n", TPL_FILE_DEFAULT);
+    }
+
+    private static void checkOneMoreArg(int i, int length) {
+        if (i+1 >= length) {
+            printUsage();
+            System.exit(1);
+        }
+    }
+
     public static void main(String[] args) {
-        final var inFileName = "make/project.xml";
-        final var tplFileName = "make/configure.vm";
-        final var outFileName = "configure";
+        var inFileName = IN_FILE_DEFAULT;
+        var tplFileName = TPL_FILE_DEFAULT;
+        var outFileName = OUT_FILE_DEFAULT;
+
+        for (int i = 0 ; i < args.length ; i++) {
+            switch (args[i]) {
+                case "-o":
+                case "--output":
+                    checkOneMoreArg(i, args.length);
+                    outFileName = args[++i];
+                    break;
+                case "-p":
+                case "--project":
+                    checkOneMoreArg(i, args.length);
+                    inFileName = args[++i];
+                    break;
+
+                case "-t":
+                case "--template":
+                    checkOneMoreArg(i, args.length);
+                    tplFileName = args[++i];
+                    break;
+                case "--help":
+                case "-h":
+                    printUsage();
+                    System.exit(0);
+                default:
+                    printUsage();
+                    System.exit(1);
+            }
+        }
+
+        System.out.println("In:  " + inFileName);
+        System.out.println("Tpl: " + tplFileName);
 
         try (var out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileName)))) {
             writeConfigureScript(out, tplFileName, loadProjectFile(inFileName));

mercurial