Combining numeric options

  <option>
    <name>debugflags</name>
    <type>int</type>
    <default>0</default>

    <combine>
      <type>or</type>
      <option>
	<value>1</value>
	<longopt>debugmsg</longopt>
	<descr>Debug messages</descr>
      </option>
      <option>
	<value>2</value>
	<longopt>debugio</longopt>
	<descr>Debug input and output</descr>
      </option>
      <option>
	<value>4</value>
	<longopt>debugauth</longopt>
	<descr>Debug authentication</descr>
      </option>
      <option>
	<value>8</value>
	<longopt>debugcommit</longopt>
	<descr>Debug commits</descr>
      </option>

      <!-- set all bits in debug flags -->

      <option>
	<value>255</value>
	<longopt>debugall</longopt>
	<descr>Debug everything</descr>
      </option>
    </combine>

    <combine>
      <type>and</type>
      <option>
	<value>~1</value>
	<longopt>nodebugmsg</longopt>
	<descr>Do not debug messages</descr>
      </option>
      <option>
	<value>~2</value>
	<longopt>nodebugio</longopt>
	<descr>Do not debug input and output</descr>
      </option>
      <option>
	<value>~4</value>
	<longopt>nodebugauth</longopt>
	<descr>Do not debug authentication</descr>
      </option>
      <option>
	<value>~8</value>
	<longopt>nodebugcommit</longopt>
	<descr>Do not debug commits</descr>
      </option>
    </combine>
  </option>

/* ... */

#include "testoptions.h"


int main(int argc, char **argv)
{
    testoptions opts;

    std::list<std::string> args(opts.parse(argc, argv)->args);

    std::cout << "Debug level: " << opts.debugflags->value << std::endl;
}

combine sets any natural integer option 's value by combining multiple values using an OR, AND, or an XOR operation, as specified by its type. The above example defines a single option value called debugflags with multiple independent options that set the value of debugflags: --debugmsg, --debugio, --debugauth, --debugcommit. Specifying each option results in the associated value logically or-ed into debugflags. The value is inserted directly into the generated code, like an option's default value.

In the above example, --debugall or-s all the bits into debugflags, and corresponding --nodebug* turns off the individual debug bits. --debugall --nodebugio, in this order, results in debugflags having all the individual bits set except for the debugio bit. opts.debugflags->value gives the final int value after all options are processed.