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.inspector; 31 32 import aermicioi.aedi.configurer.annotation.annotation; 33 import aermicioi.aedi_property_reader.convertor.inspector; 34 import std.algorithm; 35 import std.range; 36 import std..string; 37 38 /** 39 Argument inspector that provides info about available properties in a command line field. 40 **/ 41 @component 42 class ArgumentInspector : Inspector!(const(string)[]) { 43 44 /** 45 Identify the type of child field of component. 46 47 Params: 48 component = a composite component (class, struct, assoc array etc.) containing some fields 49 50 Returns: 51 Type of field, or typeid(void) if field is not present in component 52 **/ 53 TypeInfo typeOf(const(string)[] component, in string property) const nothrow { 54 return typeid(const(string)[]); 55 } 56 57 /** 58 Identify the type of component itself. 59 60 Identify the type of component itself. It will inspect the component and will return accurate 61 type info that the component represents. 62 63 Params: 64 component = component which should be identified. 65 66 Returns: 67 Type info of component, or typeid(void) if component cannot be identified by inspector 68 **/ 69 TypeInfo typeOf(const(string)[] component) const nothrow { 70 return typeid(const(string)[]); 71 } 72 73 /** 74 Check if component has a field or a property. 75 76 Params: 77 component = component with fields 78 property = component property that is tested for existence 79 80 Returns: 81 true if field is present either in readonly, or writeonly form (has getters and setters). 82 **/ 83 bool has(const(string)[] component, in string property) const nothrow { 84 try { 85 86 return component.enumerate.filter!( 87 (arg) { 88 switch (property.commonPrefix("--")) { 89 case "--": { 90 return arg[1][2 .. $].splitter('=').front == property; 91 } 92 case "-": { 93 return (arg[1][1 .. $].splitter('=').front == property) || 94 ( 95 (property.length == 1) && (arg[1].canFind(property[0])) 96 ); 97 } 98 99 default: { 100 if (property.isNumeric) { 101 import std.conv : to; 102 return (arg[0] - 1) == property.to!size_t; 103 } 104 } 105 106 return false; 107 } 108 } 109 ).empty; 110 } catch (Exception e) { 111 112 } 113 114 return false; 115 } 116 117 /** 118 Return a list of properties that component holds. 119 120 Params: 121 component = the component with fields 122 123 Returns: 124 an arary of property identities. 125 **/ 126 string[] properties(const(string)[] component) const nothrow { 127 128 try { 129 130 return component.map!( 131 (arg) { 132 switch (arg.commonPrefix("--")) { 133 case "--": { 134 return [ arg[2 .. $].splitter('=').front ]; 135 } 136 case "-": { 137 import std.conv : to; 138 return 139 [ arg[1 .. $].splitter('=').front ] ~ 140 arg[1 .. $].splitter('=').front.map!(to!string).array; 141 } 142 143 default: { 144 return cast(string[]) null; 145 } 146 } 147 } 148 ).joiner.array; 149 } catch (Exception e) { 150 151 } 152 153 return []; 154 } 155 }