|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
[cli] 2.x command lineI must be missing something very simple. . .
The 2.x appears to have no way of not processing items on the command line: it seems that in order to parse a command line you have to specify every aspect of the line. This means that a command line such as: foo -q blah where blah is a variable cannot be described. With 1.x this is easy you just specify information about the -q parse the line and then call getArgList to get a list of all the items not recognized. This works well, but seems to have no equivalent in 2.x. -- Russel. ==================================================== Dr Russel Winder Partner Concertant LLP t: +44 20 7585 2200, +44 20 7193 9203 41 Buckmaster Road, f: +44 8700 516 084 London SW11 1EN, UK. m: +44 7770 465 077 |
|
|
Re: [cli] 2.x command lineHi Russel,
I've been looking at that just now, and it doesn't work as expected. I'm assuming you mean 'blah' here is a separate argument to 'foo', not as an argument to '-q', that 'foo blah' is valid. I've also been trying to attach an argument to an option so for instance -f filename would work, without any luck. I think either I'm missing something, or there's a bug. Please find enclosed a JUnit file which seems to highlight a problem. The final test in 'testWithOneArgument' fails. ======================================= package com.wategan.cli.demo; import junit.framework.TestCase; import org.apache.commons.cli2.*; import org.apache.commons.cli2.builder.ArgumentBuilder; import org.apache.commons.cli2.builder.DefaultOptionBuilder; import org.apache.commons.cli2.builder.GroupBuilder; import org.apache.commons.cli2.commandline.Parser; import org.apache.commons.cli2.option.DefaultOption; public class CLIDemoWithOneOptionWithAnArgumentTest extends TestCase { public void testOptionAndArgument() throws Exception { CommandLine cmd = parseArgs(new String[]{"-l", "hell"}); assertNotNull(cmd); Option longOption = cmd.getOption("-l"); Object widthOption = cmd.getValue("width"); assertNotNull(longOption); assertNotNull(widthOption); assertEquals(2, cmd.getOptions().size()); assertEquals("hell", widthOption.toString().trim()); assertEquals("[-l (--length)]", longOption.toString().trim()); } public void testDemoWithOneOption() throws Exception { CommandLine cmd = parseArgs(new String[]{"-l"}); assertNotNull(cmd); assertEquals(1, cmd.getOptions().size()); Option longOption = cmd.getOption("-l"); assertNotNull(longOption); assertEquals("[-l (--length)]", longOption.toString().trim()); } public void testWithOneArgument() throws Exception { CommandLine cmd = parseArgs(new String[]{"hell"}); assertNotNull(cmd); assertEquals(1, cmd.getOptions().size()); Option widthOption = cmd.getOption("width"); assertNotNull(widthOption); assertEquals("hell", widthOption.toString().trim()); } private CommandLine parseArgs(String[] args) { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); GroupBuilder groupBuilder = new GroupBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Argument widthOption = argumentBuilder .withMinimum(0) .withMaximum(1) .withName("width") .create(); DefaultOption longOption = optionBuilder .withShortName("l") .withLongName("length") .create(); Group group = groupBuilder.withOption(longOption).withOption(widthOption).create(); Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = null; try { cmdLine = parser.parse(args); } catch (OptionException e) { System.out.println(e.getMessage()); } finally { } return cmdLine; } } ======================================= 2008/6/3 Russel Winder <russel.winder@...>: > I must be missing something very simple. . . > > The 2.x appears to have no way of not processing items on the command > line: it seems that in order to parse a command line you have to > specify every aspect of the line. This means that a command line such > as: > > foo -q blah > > where blah is a variable cannot be described. With 1.x this is easy you > just specify information about the -q parse the line and then call > getArgList to get a list of all the items not recognized. This works > well, but seems to have no equivalent in 2.x. > > -- > Russel. > ==================================================== > Dr Russel Winder Partner > > Concertant LLP t: +44 20 7585 2200, +44 20 7193 9203 > 41 Buckmaster Road, f: +44 8700 516 084 > London SW11 1EN, UK. m: +44 7770 465 077 > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: [cli] 2.x command lineHi Again,
Here is JUnit file that demonstrates how to use the '-f filename' style functionality. I'm putting these in http://wiki.apache.org/commons/CLI2 ====================================================== package com.wategan.cli.demo; import junit.framework.TestCase; import org.apache.commons.cli2.*; import org.apache.commons.cli2.builder.ArgumentBuilder; import org.apache.commons.cli2.builder.DefaultOptionBuilder; import org.apache.commons.cli2.builder.GroupBuilder; import org.apache.commons.cli2.commandline.Parser; import org.apache.commons.cli2.option.DefaultOption; public class CLIDemoWithOneOptionAndAnArgumentTest extends TestCase { public void testOptionAndArgument() throws Exception { CommandLine cmd = parseArgs(new String[]{"-l", "hell"}); assertNotNull(cmd); Option longOption = cmd.getOption("-l"); Object widthOption = cmd.getValue("width"); assertNotNull(longOption); assertNotNull(widthOption); assertEquals(2, cmd.getOptions().size()); assertEquals("hell", widthOption.toString().trim()); assertEquals("[-l (--length)]", longOption.toString().trim()); } public void testDemoWithOneOption() throws Exception { CommandLine cmd = parseArgs(new String[]{"-l"}); assertNotNull(cmd); assertEquals(1, cmd.getOptions().size()); Option longOption = cmd.getOption("-l"); assertNotNull(longOption); assertEquals("[-l (--length)]", longOption.toString().trim()); } public void testWithOneArgument() throws Exception { CommandLine cmd = parseArgs(new String[]{"hell"}); assertNotNull(cmd); assertEquals(1, cmd.getOptions().size()); Option widthOption = cmd.getOption("width"); assertNotNull(widthOption); assertEquals("hell", widthOption.toString().trim()); } private CommandLine parseArgs(String[] args) { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); GroupBuilder groupBuilder = new GroupBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Argument widthOption = argumentBuilder .withMinimum(0) .withMaximum(1) .withName("width") .create(); DefaultOption longOption = optionBuilder .withShortName("l") .withLongName("length") .create(); Group group = groupBuilder.withOption(longOption).withOption(widthOption).create(); Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = null; try { cmdLine = parser.parse(args); } catch (OptionException e) { System.out.println(e.getMessage()); } finally { } return cmdLine; } } ==================================== 2008/6/4 andrew ox. <andrew.oxenburgh@...>: > Hi Russel, > > I've been looking at that just now, and it doesn't work as expected. > > I'm assuming you mean 'blah' here is a separate argument to 'foo', not > as an argument to '-q', that 'foo blah' is valid. > > I've also been trying to attach an argument to an option so for > instance -f filename would work, without any luck. I think either I'm > missing something, or there's a bug. > > Please find enclosed a JUnit file which seems to highlight a problem. > The final test in 'testWithOneArgument' fails. > > ======================================= > > package com.wategan.cli.demo; > > import junit.framework.TestCase; > import org.apache.commons.cli2.*; > import org.apache.commons.cli2.builder.ArgumentBuilder; > import org.apache.commons.cli2.builder.DefaultOptionBuilder; > import org.apache.commons.cli2.builder.GroupBuilder; > import org.apache.commons.cli2.commandline.Parser; > import org.apache.commons.cli2.option.DefaultOption; > > public class CLIDemoWithOneOptionWithAnArgumentTest extends TestCase { > > public void testOptionAndArgument() throws Exception { > CommandLine cmd = parseArgs(new String[]{"-l", "hell"}); > assertNotNull(cmd); > Option longOption = cmd.getOption("-l"); > Object widthOption = cmd.getValue("width"); > assertNotNull(longOption); > assertNotNull(widthOption); > > assertEquals(2, cmd.getOptions().size()); > assertEquals("hell", widthOption.toString().trim()); > assertEquals("[-l (--length)]", longOption.toString().trim()); > } > > public void testDemoWithOneOption() throws Exception { > CommandLine cmd = parseArgs(new String[]{"-l"}); > assertNotNull(cmd); > assertEquals(1, cmd.getOptions().size()); > Option longOption = cmd.getOption("-l"); > assertNotNull(longOption); > assertEquals("[-l (--length)]", longOption.toString().trim()); > } > > public void testWithOneArgument() throws Exception { > CommandLine cmd = parseArgs(new String[]{"hell"}); > assertNotNull(cmd); > assertEquals(1, cmd.getOptions().size()); > Option widthOption = cmd.getOption("width"); > assertNotNull(widthOption); > assertEquals("hell", widthOption.toString().trim()); > } > > private CommandLine parseArgs(String[] args) { > DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); > GroupBuilder groupBuilder = new GroupBuilder(); > > ArgumentBuilder argumentBuilder = new ArgumentBuilder(); > > Argument widthOption = argumentBuilder > .withMinimum(0) > .withMaximum(1) > .withName("width") > .create(); > > DefaultOption longOption = optionBuilder > .withShortName("l") > .withLongName("length") > .create(); > > > Group group = > groupBuilder.withOption(longOption).withOption(widthOption).create(); > > Parser parser = new Parser(); > parser.setGroup(group); > > CommandLine cmdLine = null; > try { > cmdLine = parser.parse(args); > } catch (OptionException e) { > System.out.println(e.getMessage()); > } finally { > } > return cmdLine; > } > > } > > ======================================= > > > > > 2008/6/3 Russel Winder <russel.winder@...>: >> I must be missing something very simple. . . >> >> The 2.x appears to have no way of not processing items on the command >> line: it seems that in order to parse a command line you have to >> specify every aspect of the line. This means that a command line such >> as: >> >> foo -q blah >> >> where blah is a variable cannot be described. With 1.x this is easy you >> just specify information about the -q parse the line and then call >> getArgList to get a list of all the items not recognized. This works >> well, but seems to have no equivalent in 2.x. >> >> -- >> Russel. >> ==================================================== >> Dr Russel Winder Partner >> >> Concertant LLP t: +44 20 7585 2200, +44 20 7193 9203 >> 41 Buckmaster Road, f: +44 8700 516 084 >> London SW11 1EN, UK. m: +44 7770 465 077 >> > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
| Free Forum Powered by Nabble | Forum Help |