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