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 Alexandru Ermicioi
29 **/30 moduleaermicioi.aedi_property_reader.arg.convertor;
31 32 importstd.algorithm;
33 importstd.array;
34 importstd.conv;
35 importstd.exception;
36 importstd.getopt;
37 importstd.range;
38 importstd..string;
39 importstd.traits;
40 importstd.experimental.allocator;
41 importaermicioi.aedi.configurer.annotation.annotation;
42 importaermicioi.aedi_property_reader.arg.accessor;
43 importaermicioi.aedi_property_reader.arg.inspector;
44 importaermicioi.aedi_property_reader.convertor.accessor;
45 importaermicioi.aedi_property_reader.convertor.convertor;
46 importaermicioi.aedi_property_reader.convertor.exception;
47 importaermicioi.aedi_property_reader.convertor.inspector;
48 importaermicioi.aedi_property_reader.convertor.placeholder;
49 importaermicioi.aedi_property_reader.convertor.setter;
50 51 /**
52 Converts command line arguments in string[] to string[string] format.
53 54 Converted associative array (aa) will contain each argument by it's position as a key.
55 Compound arguments of form --key=value will be split, and available as key -> value pair
56 in aa, furthermore if following sequence is encountered --key value, it will be treated as
57 previous construct, and registered in aa as well. Single dashed arguments will be treated all
58 as single bool flags, i.e. -uni will expand into --u=true --n=true --i=true and be registered
59 in by value arguments.
60 **/61 @component62 classArgumentArrayToAssociativeArray : Convertor {
63 64 mixinConvertsFromToMixin!(string[], ArgumentsHolder);
65 mixinEqualToHashToStringOpCmpMixin;
66 67 /**
68 Convert from component to component.
69 70 Params:
71 from = original component that is to be converted.
72 to = destination object that will be constructed out for original one.
73 allocator = optional allocator that could be used to construct to component.
74 Throws:
75 ConvertorException when there is a converting error
76 InvalidArgumentException when arguments passed are not of right type or state
77 Returns:
78 Resulting converted component.
79 **/80 Objectconvert(inObjectfrom, constTypeInfoto, RCIAllocatorallocator = theAllocator) const {
81 enforce!ConvertorException(this.converts(from, to), text(
82 "Cannot convert component ", from.identify, " to ", to, " expected original component of ", this.from, " and destination of ", this.to83 ));
84 importstd.algorithm;
85 importstd.range;
86 importstd.conv : to;
87 88 ArgumentsHolderargumentsHolder;
89 string[] original = from.unwrap!(string[]);
90 argumentsHolder.byValue = original;
91 92 foreach (index, element; original) {
93 stringargument, key, value;
94 this.processDoubleDash(original[index .. $], element, argument, key, value);
95 96 if (argument !isnull) {
97 if ((keyinargumentsHolder.byKeyValues) || (keyinargumentsHolder.byKeyValue)) {
98 if (keyinargumentsHolder.byKeyValue) {
99 argumentsHolder.byKeyValues[key] ~= argumentsHolder.byKeyValue[key];
100 argumentsHolder.byKeyValue.remove(key);
101 }
102 103 argumentsHolder.byKeyValues[key] ~= value;
104 } else {
105 argumentsHolder.byKeyValue[key] = value;
106 }
107 } else {
108 this.processSingleDash(element, argumentsHolder.byKeyValue);
109 }
110 111 }
112 113 returnargumentsHolder.pack(from, this, allocator);
114 }
115 116 /**
117 Destroy component created using this convertor.
118 119 Destroy component created using this convertor.
120 Since convertor could potentially allocate memory for
121 converted component, only itself is containing history of allocation,
122 and therefore it is responsible as well to destroy and free allocated
123 memory with allocator.
124 125 Params:
126 converted = component that should be destroyed.
127 allocator = allocator used to allocate converted component.
128 Return:
129 true if component is destroyed, false otherwise
130 **/131 voiddestruct(constTypeInfofrom, refObjectconverted, RCIAllocatorallocator = theAllocator) const {
132 enforce!ConvertorException(this.destroys(from, converted), text(
133 "Cannot destroy component ", to.identify, ". Passed component isn't converted by this convertor"134 ));
135 136 converted.unpack!(string[string])(allocator);
137 }
138 139 privatevoidprocessDoubleDash(string[] remainingArguments, stringunprocessed, outstringargument, outstringkey, outstringvalue) const {
140 if (unprocessed.startsWith("--")) {
141 argument = unprocessed.stripLeft('-');
142 ptrdiff_tpointcut = argument.indexOf('=');
143 144 if (pointcut != -1) {
145 key = argument[0 .. pointcut];
146 value = argument[pointcut + 1 .. $];
147 } elseif ((remainingArguments.length > 1) && (!remainingArguments[1].startsWith('-'))) {
148 key = argument;
149 value = remainingArguments[1];
150 } else {
151 key = argument;
152 value = "true";
153 }
154 }
155 }
156 157 privatevoidprocessSingleDash(stringunprocessed, string[string] array) const {
158 importstd.uni : isAlpha;
159 importstd.algorithm;
160 importstd.utf : byDchar;
161 162 if (unprocessed.startsWith("-")) {
163 stringargument = unprocessed.stripLeft('-');
164 165 if (argument.byDchar.all!(dch => dch.isAlpha)) {
166 foreach (index, dch; argument) {
167 array[argument[index .. index + 1]] = "true";
168 }
169 }
170 }
171 }
172 }