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;
31 
32 import aermicioi.aedi.configurer.annotation.annotation;
33 import aermicioi.aedi_property_reader.arg.accessor;
34 import aermicioi.aedi_property_reader.arg.arg;
35 import aermicioi.aedi_property_reader.arg.convertor;
36 import aermicioi.aedi_property_reader.convertor.accessor;
37 import aermicioi.aedi_property_reader.convertor.convertor : CombinedConvertor, Convertor;
38 import aermicioi.aedi_property_reader.convertor.exception;
39 import aermicioi.aedi_property_reader.convertor.placeholder;
40 import aermicioi.aedi_property_reader.convertor.std_conv;
41 import aermicioi.aedi_property_reader.convertor.type_guesser;
42 import aermicioi.aedi_property_reader.core.core;
43 import aermicioi.aedi_property_reader.core.document;
44 import std.exception;
45 import std.experimental.allocator;
46 
47 alias ArgumentDocumentContainer = PolicyDocument!(
48     ArgumentsHolder,
49     string,
50 	WithConvertorSources!(string[])(),
51     WithContainerScanning!(
52         aermicioi.aedi_property_reader.core.config,
53     )(),
54     WithInitializer!(
55         StdConvStringPrebuiltConvertorsFactory
56     )(),
57     WithContainerScanning!(
58         aermicioi.aedi_property_reader.arg.arg,
59     )(),
60     WithConvertorAggregation!CombinedConvertor(),
61     WithLocatorForUnregisteredComponents("arg-document-locator"),
62     WithDefaultRegisterers
63 );
64 
65 @component
66 auto runtimeArgumentTypeGuesser(
67     TypeGuesser!string guesser
68 ) {
69     return new DelegatingObjectTypeGuesser!string(guesser);
70 }
71 
72 @component
73 auto rootAccessor(
74 	PropertyAccessor!(string[][string], string[]) byKeyValuesAccessor,
75 	PropertyAccessor!(string[string], string) byKeyValueAccessor,
76 	PropertyAccessor!(string[], string, size_t) byValueAccessor,
77     Convertor defaultConvertor
78 ) {
79     return new ArgumentAccessor(
80 		byKeyValuesAccessor,
81 		byKeyValueAccessor,
82 		new KeyConvertingAccessor!(string[], string, string, size_t)(
83 			byValueAccessor,
84             defaultConvertor
85         )
86 	);
87 }
88 
89 @component
90 auto byKeyValuesAccessor() {
91 	return new AssociativeArrayAccessor!(string[][string]);
92 }
93 
94 @component
95 auto byKeyValueAccessor() {
96 	return new AssociativeArrayAccessor!(string[string]);
97 }
98 
99 @component
100 auto byValueAccessor() {
101 	return new ArrayAccessor!(string[]);
102 }
103 
104 /**
105 Create container for application arguments passed on command line
106 
107 Params:
108 	args = list of console arguments
109 	accessor = accessor that knows how to extract a field out of command line arguments
110 	guesser = type guesser for fields that no information on type is available
111 	allocator = optional allocator used to convert data to D types when possible.
112 
113 Returns:
114 	DocumentType!(string[], string)
115 **/
116 auto argument(
117 	string[] args,
118 ) {
119 	ArgumentDocumentContainer container = ArgumentDocumentContainer(
120 		(new ArgumentArrayToAssociativeArray()).convert(args.stored, typeid(ArgumentsHolder), theAllocator).unpack!ArgumentsHolder
121 	);
122 
123 	return container;
124 }
125 
126 /**
127 ditto
128 **/
129 auto argument() {
130 	import core.runtime : Runtime;
131 
132 	return Runtime.args.argument;
133 }