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_container; 31 32 import aermicioi.aedi.storage.locator; 33 import aermicioi.aedi_property_reader.generic_convertor_container; 34 import aermicioi.aedi_property_reader.arg.arg_factory; 35 import aermicioi.aedi_property_reader.helper.help_decorating_exception; 36 import aermicioi.aedi.exception.not_found_exception; 37 import aermicioi.aedi.storage.alias_aware; 38 import std.algorithm; 39 import std.range; 40 import std.utf; 41 42 /** 43 Command line arguments data source/locator used by converting containers. 44 **/ 45 class GetoptIdentityLocator : Locator!(string, string) { 46 47 private { 48 string[] args; 49 } 50 51 public { 52 53 /** 54 Default constructor for GetoptIdentityLocator 55 **/ 56 this() { 57 import core.runtime; 58 this(Runtime.args()); 59 } 60 61 /** 62 Constructor for GetoptIdentityLocator 63 64 Params: 65 args = command line arguments used as source of them. 66 **/ 67 this(string[] args) { 68 this.args = args; 69 } 70 71 /** 72 Check if command line argument exists, and return identity of it. 73 74 Params: 75 identity = the command line argument id. 76 77 Throws: 78 NotFoundException in case if the command line argument wasn't found. 79 80 Returns: 81 string identity of command line argument if it is available. 82 **/ 83 string get(string identity) { 84 if (!this.has(identity)) { 85 86 throw new NotFoundException("Could not find argument in command line"); 87 } 88 89 return identity; 90 } 91 92 /** 93 Check if an command line argument is present in list of arguments by key id. 94 95 Params: 96 identity = identity of element. 97 98 Returns: 99 bool true if an command line argument by key is present in arguments. 100 **/ 101 bool has(in string identity) inout { 102 foreach (const arg; this.args[1 .. $]) { 103 if (arg.startsWith('-')) { 104 105 if (arg.stripLeft('-').until('=').equal(identity)) { 106 return true; 107 } 108 109 if (!arg.startsWith("--") && (identity.length == 1)) { 110 if (arg.stripLeft('-').until('=').canFind(identity)) { 111 return true; 112 } 113 } 114 } 115 } 116 117 return false; 118 } 119 } 120 } 121 122 /** 123 Generic convertor container version for command line arguments. 124 **/ 125 alias GetoptConvertorContainer = GenericConvertorContainer!(string, GetoptConvertorFactory);