java-flagz icon indicating copy to clipboard operation
java-flagz copied to clipboard

scanAndBind appears to be failing to find any flags

Open malachid opened this issue 9 years ago • 5 comments

Test class:

public class FlagTest
{
    @FlagInfo(name="debug", altName="d", help="Enable debug logging")
    private static final Flag<Boolean> debug = Flagz.valueOf(false);

    public static void main(String ... args)
    {
        Arrays.stream(args).forEach(arg -> System.out.format("ARG FOUND: %s\n", arg));

        FlagFieldRegistry registry = Flagz.parse(args);
        if(debug.get()) {
            System.out.format("Debug logging enabled...\n");
        }
    }
}

Whether I make the debug field private or public, when attempting to run this snippet I get the following error (against 2.1.0):

ARG FOUND: --debug
Exception in thread "main" org.flagz.FlagException$UnknownFlag: Could not find Flag for name/alt-name 'debug'
        at org.flagz.FlagFieldRegistry.parseAll(FlagFieldRegistry.java:56)
        at org.flagz.Flagz.parse(Flagz.java:101)
        at org.flagz.Flagz.parse(Flagz.java:112)
        at org.eoti.sandbox2.flagz.FlagTest.main(FlagTest.java:22)

I have tried with both JDK8 and Jigsaw JDK9.

malachid avatar Jul 26 '16 05:07 malachid

In parseAll, I can see that Set<String> unknownNames = Sets.difference(nameToValue.keySet(), allNamesToField.keySet()); nameToValue has my flag, allNamesToField does not. Checking, I ran with '--help' and saw that my flag was not listed as an option. It looks like the init function should be adding all nameToValue values into allNamesToField. Maybe I am not calling something I need to?

malachid avatar Aug 27 '16 04:08 malachid

Ok, I have this resolved but I think it points to a documentation bug.

If I do FlagFieldRegistry registry = Flagz.parse(args, ImmutableList.of("org.eoti.sandbox2.flagz"), ImmutableSet.of(this)); - it works

If I do FlagFieldRegistry registry = Flagz.parse(args); as recommended in the docs, it does not work.

malachid avatar Aug 27 '16 04:08 malachid

It could be because Flagz.parse(args) will have empty scanners and then scanAndBind will have nothing to iterate over.

malachid avatar Aug 27 '16 04:08 malachid

@malachid Can you create Pull request with bazel command? I am not able to use flagz actually.

systemlogic avatar Apr 17 '20 16:04 systemlogic

I'm sorry, I don't use this library anymore.

malachid avatar Apr 24 '20 20:04 malachid