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 module aermicioi.aedi_property_reader.arg.convertor; 31 32 import std.algorithm; 33 import std.array; 34 import std.conv; 35 import std.exception; 36 import std.getopt; 37 import std.range; 38 import std..string; 39 import std.traits; 40 import std.experimental.allocator; 41 import aermicioi.aedi.configurer.annotation.annotation; 42 import aermicioi.aedi_property_reader.arg.accessor; 43 import aermicioi.aedi_property_reader.arg.inspector; 44 import aermicioi.aedi_property_reader.convertor.accessor; 45 import aermicioi.aedi_property_reader.convertor.convertor; 46 import aermicioi.aedi_property_reader.convertor.exception; 47 import aermicioi.aedi_property_reader.convertor.inspector; 48 import aermicioi.aedi_property_reader.convertor.placeholder; 49 import aermicioi.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 @component 62 class ArgumentArrayToAssociativeArray : Convertor { 63 64 mixin ConvertsFromToMixin!(string[], ArgumentsHolder); 65 mixin EqualToHashToStringOpCmpMixin; 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 Object convert(in Object from, const TypeInfo to, RCIAllocator allocator = 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.to 83 )); 84 import std.algorithm; 85 import std.range; 86 import std.conv : to; 87 88 ArgumentsHolder argumentsHolder; 89 string[] original = from.unwrap!(string[]); 90 argumentsHolder.byValue = original; 91 92 foreach (index, element; original) { 93 string argument, key, value; 94 this.processDoubleDash(original[index .. $], element, argument, key, value); 95 96 if (argument !is null) { 97 if ((key in argumentsHolder.byKeyValues) || (key in argumentsHolder.byKeyValue)) { 98 if (key in argumentsHolder.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 return argumentsHolder.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 void destruct(const TypeInfo from, ref Object converted, RCIAllocator allocator = 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 private void processDoubleDash(string[] remainingArguments, string unprocessed, out string argument, out string key, out string value) const { 140 if (unprocessed.startsWith("--")) { 141 argument = unprocessed.stripLeft('-'); 142 ptrdiff_t pointcut = argument.indexOf('='); 143 144 if (pointcut != -1) { 145 key = argument[0 .. pointcut]; 146 value = argument[pointcut + 1 .. $]; 147 } else if ((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 private void processSingleDash(string unprocessed, string[string] array) const { 158 import std.uni : isAlpha; 159 import std.algorithm; 160 import std.utf : byDchar; 161 162 if (unprocessed.startsWith("-")) { 163 string argument = 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 }