Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingRequiredParams error shows optional indexed Parameters #99

Closed
remkop opened this issue Apr 17, 2017 · 1 comment
Closed

MissingRequiredParams error shows optional indexed Parameters #99

remkop opened this issue Apr 17, 2017 · 1 comment

Comments

@remkop
Copy link
Owner

remkop commented Apr 17, 2017

    @Test
    public void testMissingRequiredParams() {
        class Example {
            @Parameters(index = "1", arity = "0..1") String optional;
            @Parameters(index = "0") String mandatory;
        }
        try { CommandLine.parse(new Example(), new String[] {"mandatory"}); }
        catch(MissingParameterException ex) { fail(); }

        try {
            CommandLine.parse(new Example(), new String[0]);
            fail("Should not accept missing mandatory parameter");
        } catch(MissingParameterException ex) {
            assertEquals("Missing required parameter: mandatory", ex.getMessage());
        }
    }
    @Test
    public void testMissingRequiredParams1() {
        class Tricky1 {
            @Parameters(index = "2") String anotherMandatory;
            @Parameters(index = "1", arity = "0..1") String optional;
            @Parameters(index = "0") String mandatory;
        }
        try {
            CommandLine.parse(new Tricky1(), new String[0]);
            fail("Should not accept missing mandatory parameter");
        } catch(MissingParameterException ex) {
            assertEquals("Missing required parameters: mandatory, anotherMandatory", ex.getMessage());
        }
        try {
            CommandLine.parse(new Tricky1(), new String[] {"firstonly"});
            fail("Should not accept missing mandatory parameter");
        } catch(MissingParameterException ex) {
            assertEquals("Missing required parameter: anotherMandatory", ex.getMessage());
        }
    }
    @Test
    public void testMissingRequiredParams2() {
        class Tricky2 {
            @Parameters(index = "2", arity = "0..1") String anotherOptional;
            @Parameters(index = "1", arity = "0..1") String optional;
            @Parameters(index = "0") String mandatory;
        }
        try { CommandLine.parse(new Tricky2(), new String[] {"mandatory"}); }
        catch(MissingParameterException ex) { fail(); }

        try {
            CommandLine.parse(new Tricky2(), new String[0]);
            fail("Should not accept missing mandatory parameter");
        } catch(MissingParameterException ex) {
            assertEquals("Missing required parameter: mandatory", ex.getMessage());
        }
    }
@remkop remkop added this to the 0.4.0 user manual milestone Apr 17, 2017
@remkop remkop modified the milestones: 0.4.0 user manual, 0.9.1 bugfixes Apr 18, 2017
@remkop
Copy link
Owner Author

remkop commented Apr 18, 2017

assertNoMissingParameters method

                if (arity == 1) {
                    if (field.isAnnotationPresent(Option.class)) {
                        throw new MissingParameterException("Missing required parameter for " +
                                optionDescription("", field, 0));
                    }
                    Arity indexRange = Arity.valueOf(field.getAnnotation(Parameters.class).index());
                    Help.IParamLabelRenderer labelRenderer = Help.createMinimalParamLabelRenderer();
                    String sep = "";
                    String names = "";
                    int count = 0;
                    for (int i = indexRange.min; i < positionalParametersFields.size(); i++) {
                        if (Arity.forParameters(positionalParametersFields.get(i)).min > 0) {
                            names += sep + labelRenderer.renderParameterLabel(positionalParametersFields.get(i),
                                    Collections.<IStyle>emptyList());
                            sep = ", ";
                            count++;
                        }
                    }
                    String msg = "Missing required parameter";
                    Arity paramArity = Arity.forParameters(field);
                    if (paramArity.isVariable) {
                        msg += "s at positions " + paramArity + ": ";
                    } else {
                        msg += (count > 1 ? "s: " : ": ");
                    }
                    throw new MissingParameterException(msg + names);
                }

@remkop remkop closed this as completed in 9e662b3 Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant