Constructing a variadic template parameter with source in two files

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 21 23:33:42 PST 2016


On 12/21/2016 07:59 PM, Jon Degenhardt wrote:

 > construct the 'opts' parameter from
 > definitions stored in two or more files. The reason for doing this is to
 > create a customization mechanism where-by there are a number of default
 > capabilities built-in to the main code base, but someone can customize
 > their copy of the code, putting definitions in a separate file, and have
 > it added in at compile time, including modifying command line arguments.

I'm not sure this is any better than your mixin solution but getopt can 
be called multiple times on the same arguments. So, for example common 
code can parse them for its arguments and special code can parse them 
for its arguments. Useful bits:

* std.getopt.config.passThrough allows unrecognized arguments

* Although main's args can be passed to any function, program arguments 
are also available through Runtime.args()

The following program calls getopt twice.

import core.runtime : Runtime;
import std.getopt;

void foo() {
     int special;
     // Making a copy as Runtime.args() seems to return rvalue and 
getopt takes by-ref
     auto progArgs = Runtime.args();
     getopt(progArgs, std.getopt.config.passThrough, "special", &special);
}

void main(string[] args) {
     int length;
     getopt(args, std.getopt.config.passThrough, "length", &length);
}

Ali



More information about the Digitalmars-d-learn mailing list