Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import
- std.stdio,
- std.conv;
- mixin template cli(T){
- this(string[] args){
- foreach(member; __traits(allMembers, T)) {
- foreach(attr; __traits(getAttributes, mixin(member))){
- foreach(i, arg; args){
- if(arg == attr){
- static if(is(typeof(mixin(member)) == bool)){
- mixin(member ~ " = ! " ~ member ~ ";");
- }else{
- mixin(member ~ " = to!(typeof(" ~ member ~ "))(args[i+1]);");
- }
- }
- }
- }
- }
- }
- }
- struct Options {
- @("-v") bool version_;
- @("-t") float time;
- @("-n") string name = "default";
- mixin cli!Options;
- }
- void main(string[] args){
- auto options = Options(args);
- writeln(options.version_);
- writeln(options.time);
- writeln(options.name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement