# HG changeset patch # User Mike Becker # Date 1696613707 -7200 # Node ID ea79a383ad7554aa9266b20d225165bc0a40722b # Parent e55f318a8b8b49ce3509771922187c692df62edb add command line options - fixes #303 diff -r e55f318a8b8b -r ea79a383ad75 src/main/java/de/unixwork/uwproj/Main.java --- 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 Path to the output file"); + System.err.printf(" (default: %s)\n", OUT_FILE_DEFAULT); + System.err.println(" -p --project Path to the project.xml file"); + System.err.printf(" (default: %s)\n", IN_FILE_DEFAULT); + System.err.println(" -t --template 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));