/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }
@Test public void testNumberPattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("n%d%x%"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-n", "1", "-d", "2.1", "-x", "3,5" }); assertEquals("n object class", Long.class, line.getOptionObject("n").getClass()); assertEquals("n value", new Long(1), line.getOptionObject("n")); assertEquals("d object class", Double.class, line.getOptionObject("d").getClass()); assertEquals("d value", new Double(2.1), line.getOptionObject("d")); assertNull("x object", line.getOptionObject("x")); }
@Test public void testObjectPattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("o@i@n@"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-o", "java.lang.String", "-i", "java.util.Calendar", "-n", "System.DateTime" }); assertEquals("o value", "", line.getOptionObject("o")); assertNull("i value", line.getOptionObject("i")); assertNull("n value", line.getOptionObject("n")); }
@Test public void testURLPattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("u/v/"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-u", "http://commons.apache.org", "-v", "foo://commons.apache.org" }); assertEquals("u value", new URL("http://commons.apache.org"), line.getOptionObject("u")); assertNull("v value", line.getOptionObject("v")); }
@Test public void testClassPattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("c+d+"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-c", "java.util.Calendar", "-d", "System.DateTime" }); assertEquals("c value", Calendar.class, line.getOptionObject("c")); assertNull("d value", line.getOptionObject("d")); }
@Test public void testExistingFilePattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("f<"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-f", "test.properties" }); assertEquals("f value", new File("test.properties"), line.getOptionObject("f")); // todo test if an error is returned if the file doesn't exists (when it's implemented) }
@Test public void testUntypedPattern() throws Exception { Options options = PatternOptionBuilder.parsePattern("abc"); CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, new String[] { "-abc" }); assertTrue(line.hasOption('a')); assertNull("value a", line.getOptionObject('a')); assertTrue(line.hasOption('b')); assertNull("value b", line.getOptionObject('b')); assertTrue(line.hasOption('c')); assertNull("value c", line.getOptionObject('c')); }
assertEquals("string flag a", "foo", line.getOptionObject("a")); assertEquals("object flag b", new Vector<Object>(), line.getOptionObject("b")); assertTrue("boolean true flag c", line.hasOption("c")); assertFalse("boolean false flag d", line.hasOption("d")); assertEquals("file flag e", new File("build.xml"), line.getOptionObject("e")); assertEquals("class flag f", Calendar.class, line.getOptionObject("f")); assertEquals("number flag n", new Double(4.5), line.getOptionObject("n")); assertEquals("url flag t", new URL("http://commons.apache.org"), line.getOptionObject("t")); assertEquals("string flag a", "foo", line.getOptionObject('a')); assertEquals("object flag b", new Vector<Object>(), line.getOptionObject('b')); assertTrue("boolean true flag c", line.hasOption('c')); assertFalse("boolean false flag d", line.hasOption('d')); assertEquals("file flag e", new File("build.xml"), line.getOptionObject('e')); assertEquals("class flag f", Calendar.class, line.getOptionObject('f')); assertEquals("number flag n", new Double(4.5), line.getOptionObject('n')); assertEquals("url flag t", new URL("http://commons.apache.org"), line.getOptionObject('t')); assertEquals("files flag m", new File[0], line.getOptionObject('m')); fail("Multiple files are not supported yet, should have failed"); } catch(UnsupportedOperationException uoe) { assertEquals("date flag z", new Date(1023400137276L), line.getOptionObject('z')); fail("Date is not supported yet, should have failed"); } catch(UnsupportedOperationException uoe) {
/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }
/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }
/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }
/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }
/** * Return the <code>Object</code> type of this <code>Option</code>. * * @param opt the name of the option * @return the type of opt */ public Object getOptionObject(char opt) { return getOptionObject(String.valueOf(opt)); }