Search code examples
javadefault-valueargs4j

Remove default value in printUsage() method. [args4j library]


How to get rid of "(default:value)" when using printUsage() method? Image

I tried to look at the source code of args4j library and searched for printUsage() method in CmdLineParser Class but didn't find anything there. (link) The problem appears only with boolean type.


Solution

  • Seems late, but maybe this answer still helps someone:

    You need to overwrite the default BooleanOptionHandler with your own OptionHandler. First, create a class (in a separate file, because of reflection calls) like this:

    public class HelpOptionHandler extends BooleanOptionHandler {
        public HelpOptionHandler(CmdLineParser parser, OptionDef option, Setter<Boolean> setter) {
            super(parser, option, setter);
        }
        public String printDefaultValue() {
             return null;  // this prevents the default value to be printed in usage info
        }
    }
    

    Then, register this option handler in your help annotation like this:

    @Option(name = "-h", aliases = "-help", help = true, usage = "print this message", 
            handler = jondos.vodafone.helper.HelpOptionHandler.class)
    protected boolean help;