From 787b059948718d182d39c13c7f3f5af96e2ff888 Mon Sep 17 00:00:00 2001 From: Geoff Johnstone Date: Fri, 31 Dec 2010 16:44:17 +0000 Subject: [PATCH] Autodetect output format from filename. --- src/net/ametros/dive/Main.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/net/ametros/dive/Main.java b/src/net/ametros/dive/Main.java index cfddb93..907e9be 100644 --- a/src/net/ametros/dive/Main.java +++ b/src/net/ametros/dive/Main.java @@ -94,9 +94,7 @@ public class Main public static void main (String[] args) throws Exception { final OptionParser op = new OptionParser(); - op.acceptsAll (L("f", "format"), "Output format: html, xhtml, xls or xml"). - withRequiredArg().defaultsTo ("xls"); - + op.acceptsAll (L("f", "format"), "Output format: html, xhtml, xls or xml"); op.acceptsAll (L("o", "output"), "Output filename"). withRequiredArg().ofType (File.class); @@ -106,7 +104,25 @@ public class Main try { options = op.parse (args); - writer = getWriter ((String)options.valueOf ("format")); + + final String format; + if (options.has ("format")) + format = (String)options.valueOf ("format"); + else if (!options.has ("output")) + throw new IllegalArgumentException ("Cannot guess output format " + + "without an output filename"); + else + { + final File file = (File)options.valueOf ("output"); + final String name = file.getName(); + final int dot = name.lastIndexOf ('.'); + if (-1 == dot) + throw new IllegalArgumentException ("Cannot guess output format"); + + format = name.substring (dot + 1); + } + + writer = getWriter (format); } catch (Exception e) { -- 2.11.4.GIT