1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 	
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 	
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	aermicioi
29 **/
30 module aermicioi.aedi_property_reader.arg.arg_factory;
31 
32 import aermicioi.aedi_property_reader.convertor_factory;
33 import aermicioi.aedi_property_reader.helper.help_decorating_exception;
34 import aermicioi.aedi.storage.locator;
35 
36 /**
37 Convertor Factory for command line arguments.
38 
39 Params:
40 	FromType = original representation form of data to be converted.
41 	ToType = type of component that is built based on FromType data.
42 **/
43 class GetoptConvertorFactory(To, From : string = string) : ConvertorFactory!(From, To) {
44     
45     private {
46         string identity_;
47         string[] args;
48         Locator!() locator_;
49     }
50     
51     public {
52         /**
53         Default constructor for GetoptConvertorFactory!(To, From)
54         **/
55         this() {
56             import core.runtime;
57             this(Runtime.args());
58         }
59         
60         /**
61         Constructor for GetoptConvertorFactory!(To, From)
62         
63         Params: 
64             args = source from which constructor creates component.
65         **/
66         this(string[] args) {
67             this.args = args;
68         }
69         
70         @property {
71         	
72             /**
73             Set convertible.
74             
75             Params:
76             	identity = the value to be set
77             Returns:
78             	GetoptConvertorFactory!(To, From)
79             **/
80             GetoptConvertorFactory!(To, From) convertible(string identity) @safe nothrow {
81             	this.identity_ = identity;
82             
83             	return this;
84             }
85             
86             /**
87             Get convertible
88             
89             Returns:
90             	string
91             **/
92             string convertible() @safe nothrow {
93             	return this.identity_;
94             }
95             
96             /**
97             Set locator.
98             
99             Params:
100             	locator = the value to be set
101             Returns:
102             	Locator!()
103             **/
104             GetoptConvertorFactory!(To, From) locator(Locator!() locator) @safe nothrow {
105             	this.locator_ = locator;
106             
107             	return this;
108             }
109             
110             /**
111             Get the type info of To that is created.
112         		
113     		Returns:
114     			TypeInfo object of created object.
115     		**/
116             TypeInfo type() {
117                 return typeid(To);
118             }
119         }
120         
121         /**
122 		Instantiates component of type To.
123 		
124 		Returns:
125 			To instantiated data of type To.
126 		**/
127         To factory() {
128             import std.getopt;
129 
130             To value;
131             string[] args = this.args;
132             
133             auto help = getopt(args, std.getopt.config.passThrough, this.convertible, &value);
134             
135             if (help.helpWanted) {
136                 throw new HelpDecoratingException("help needed");
137             }
138             
139             return value;
140         }
141     }
142 }