Package net.sourceforge.argparse4j.inf

Examples of net.sourceforge.argparse4j.inf.Namespace


    @Test
    public void testParseArgsWithCount() throws ArgumentParserException {
        ap.addArgument("-v", "--verbose").action(count());
        ap.addArgument("--foo");
        Namespace res = ap.parseArgs("-v -vv -vvvv".split(" "));
        assertEquals(7, res.get("verbose"));
    }
View Full Code Here


    }

    @Test
    public void testParseArgsWithConst() throws ArgumentParserException {
        ap.addArgument("--foo").setConst("X").nargs("?");
        Namespace res = ap.parseArgs("--foo".split(" "));
        assertEquals("X", res.get("foo"));
    }
View Full Code Here

    @Test
    public void testParseArgsWithQ() throws ArgumentParserException {
        ap.addArgument("--foo").nargs("?").setConst("c").setDefault("d");
        ap.addArgument("bar").nargs("?").setDefault("d");
        Namespace res = ap.parseArgs("XX --foo YY".split(" "));
        assertEquals("YY", res.get("foo"));
        assertEquals("XX", res.get("bar"));
        res = ap.parseArgs("XX --foo".split(" "));
        assertEquals("c", res.get("foo"));
        assertEquals("XX", res.get("bar"));
        res = ap.parseArgs(new String[] {});
        assertEquals("d", res.get("foo"));
        assertEquals("d", res.get("bar"));       
    }
View Full Code Here

        ap.addArgument("-2");
        ap.addArgument("-3");
        ap.addArgument("-ff");
        ap.addArgument("-f");
        ap.addArgument("-c").action(appendConst()).setConst(true);
        Namespace res = ap.parseArgs("-123=x -ff=a -fx -cccc".split(" "));
        assertEquals(true, res.get("1"));
        assertEquals("3=x", res.get("2"));
        assertEquals("a", res.get("ff"));
        assertEquals("x", res.get("f"));
        assertEquals(list(true, true, true, true), res.get("c"));
        // If last option requires argument but the argument is not
        // embedded in the same term, it must take next term as an
        // argument.
        res = ap.parseArgs("-12 foo".split(" "));
        assertEquals(true, res.get("1"));
        assertEquals("foo", res.get("2"));
        // If -12" " is given in the terminal, program get this
        res = ap.parseArgs(new String[] { "-12 "});
        assertEquals(true, res.get("1"));
        assertEquals(" ", res.get("2"));
        // This is error case because the next term -fx is flag.
        try {
            res = ap.parseArgs("-12 -fx".split(" "));
            fail();
        } catch(ArgumentParserException e) {
View Full Code Here

    }

    @Test
    public void testParseArgsWithStringChoices() throws ArgumentParserException {
        ap.addArgument("--foo").choices("chocolate", "icecream", "froyo");
        Namespace res = ap.parseArgs("--foo icecream".split(" "));
        assertEquals("icecream", res.get("foo"));
        try {
            ap.parseArgs("--foo pudding".split(" "));
            fail("Exception must be thrown");
        } catch (ArgumentParserException e) {
            // success
View Full Code Here

    @Test
    public void testParseArgsWithIntegerRange() throws ArgumentParserException {
        ap.addArgument("--port").type(Integer.class)
                .choices(range(1025, 65535));
        Namespace res = ap.parseArgs("--port 3000".split(" "));
        assertEquals(3000, res.get("port"));
        try {
            ap.parseArgs("--port 80".split(" "));
            fail("Exception must be thrown");
        } catch (ArgumentParserException e) {
            // success
View Full Code Here

        Subparsers subparsers = ap.addSubparsers();
        Subparser subparser = subparsers.addParser("add");
        subparser.addArgument("--foo");
        subparser.addArgument("--bar").action(Arguments.storeTrue());

        Namespace res = ap.parseArgs("-f foo @target/test-classes/args.txt --baz alpha @target/test-classes/args2.txt x y1 @target/test-classes/args3.txt add --bar @target/test-classes/args4.txt".split(" "));
        assertEquals("bar", res.getString("f"));
        assertEquals(list("alpha", "bravo"), res.getList("baz"));
        assertEquals("x", res.getString("x"));
        assertEquals(list("y1", "y2"), res.getList("y"));
        assertEquals("HELLO", res.getString("foo"));
    }
View Full Code Here

        parserA.addArgument("pkg1");
        parserA.setDefault("func", "install");
        Subparser parserB = subparsers.addParser("search");
        parserB.addArgument("pkg2");
        parserB.setDefault("func", "search");
        Namespace res = ap.parseArgs("install aria2".split(" "));
        assertEquals("aria2", res.get("pkg1"));
        assertEquals("install", res.get("func"));
    }
View Full Code Here

    @Test
    public void testParseArgsWithSubparsersAlias() throws ArgumentParserException {
        Subparsers subparsers = ap.addSubparsers();
        Subparser checkout = subparsers.addParser("checkout").aliases("co");
        checkout.setDefault("func", "checkout");
        Namespace res = ap.parseArgs("co".split(" "));
        assertEquals("checkout", res.get("func"));
    }
View Full Code Here

        assertEquals("checkout", res.get("func"));
    }

    @Test
    public void testParseArgsWithSubparsersAmbiguousCommand() throws ArgumentParserException {
        Namespace res;
        Subparsers subparsers = ap.addSubparsers();
        Subparser checkout = subparsers.addParser("clone")
                .setDefault("func", "clone");
        Subparser clean = subparsers.addParser("clean")
                .setDefault("func", "clean");

        res = ap.parseArgs("clo".split(" "));
        assertEquals("clone", res.get("func"));

        res = ap.parseArgs("cle".split(" "));
        assertEquals("clean", res.get("func"));
        try {
            ap.parseArgs("cl".split(" "));
            fail();
        } catch(ArgumentParserException e) {
            assertEquals("ambiguous command: cl could match clean, clone",
View Full Code Here

TOP

Related Classes of net.sourceforge.argparse4j.inf.Namespace

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.