negated boolean

52 views
Skip to first unread message

slymz

unread,
Apr 25, 2017, 10:48:04 PM4/25/17
to jcommander
Is it possible to have negated boolean options?

@Parameter(names= "-nofoo", description="do not use foo")
public boolean withFoo = true; // but false if the option is given

It is occasionally more natural to expose negation as an option to the user, but work with the positive boolean in code. Current workaround for this requires an extra boolean, set during postprocessing, which becomes a bit unwieldy when such options are numerous.

Also, related, is it somehow possible to have options '-bar' and '-nobar' act as boolean toggles to the same boolean member variable?  (similarly, ugly workaround with an extra useless boolean in option set)

thx,
slymz






Cédric Beust ♔

unread,
Apr 25, 2017, 11:10:58 PM4/25/17
to jcommander

That’s a good idea, I just added support for this in 1.70, which is now available on JCenter:

    @Test
    public void invertedBoolean() {
        class Args {
            @Parameter(names = {"--f"})
            private boolean f = true;
        }
        Args args = new Args();
        JCommander.newBuilder()
                .addObject(args)
                .args(new String[] { "--f"})
                .build();
        Assert.assertEquals(args.f, false);
    }

-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

slymz

unread,
Apr 26, 2017, 2:20:32 PM4/26/17
to jcommander, ced...@beust.com
works great! thanks

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

Al Le

unread,
Apr 26, 2017, 2:24:49 PM4/26/17
to jcomm...@googlegroups.com
Does this mean that boolean @Parameter's just invert the default value?
 
Gesendet: Mittwoch, 26. April 2017 um 05:10 Uhr
Von: "Cédric Beust ♔" <ced...@beust.com>
An: jcommander <jcomm...@googlegroups.com>
Betreff: Re: [JCommander] negated boolean
 
-- 
Cédric
 
 
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

 

--

You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

Cédric Beust ♔

unread,
Apr 26, 2017, 2:38:15 PM4/26/17
to jcommander
Yes: if the flag is found on the command line, JCommander will simply invert the value the field was initialized with.


-- 
Cédric


 
-- 
Cédric
 
 
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages