Advertisement
WeltEnSTurm

cli

Jan 17th, 2015
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.73 KB | None | 0 0
  1. import
  2.     std.stdio,
  3.     std.conv;
  4.  
  5.  
  6. mixin template cli(T){
  7.  
  8.     this(string[] args){
  9.         foreach(member; __traits(allMembers, T)) {
  10.             foreach(attr; __traits(getAttributes, mixin(member))){
  11.                 foreach(i, arg; args){
  12.                     if(arg == attr){
  13.                         static if(is(typeof(mixin(member)) == bool)){
  14.                             mixin(member ~ " = ! " ~ member ~ ";");
  15.                         }else{
  16.                             mixin(member ~ " = to!(typeof(" ~ member ~ "))(args[i+1]);");
  17.                         }
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.     }
  23.  
  24. }
  25.  
  26.  
  27. struct Options {
  28.  
  29.     @("-v") bool version_;
  30.     @("-t") float time;
  31.     @("-n") string name = "default";
  32.  
  33.     mixin cli!Options;
  34.  
  35. }
  36.  
  37. void main(string[] args){
  38.     auto options = Options(args);
  39.     writeln(options.version_);
  40.     writeln(options.time);
  41.     writeln(options.name);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement