1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2018-2019 Aurora Free Open Source Software.
10 
11 This file is part of the Aurora Free Open Source Software. This
12 organization promote free and open source software that you can
13 redistribute and/or modify under the terms of the GNU Lesser General
14 Public License Version 3 as published by the Free Software Foundation or
15 (at your option) any later version approved by the Aurora Free Open Source
16 Software Organization. The license is available in the package root path
17 as 'LICENSE' file. Please review the following information to ensure the
18 GNU Lesser General Public License version 3 requirements will be met:
19 https://www.gnu.org/licenses/lgpl.html .
20 
21 Alternatively, this file may be used under the terms of the GNU General
22 Public License version 3 or later as published by the Free Software
23 Foundation. Please review the following information to ensure the GNU
24 General Public License requirements will be met:
25 https://www.gnu.org/licenses/gpl-3.0.html.
26 
27 NOTE: All products, services or anything associated to trademarks and
28 service marks used or referenced on this file are the property of their
29 respective companies/owners or its subsidiaries. Other names and brands
30 may be claimed as the property of others.
31 
32 For more info about intellectual property visit: aurorafoss.org or
33 directly send an email to: contact (at) aurorafoss.org .
34 */
35 
36 module aurorafw.core.opt;
37 
38 public import aurorafw.stdx.getopt : defaultRuntimeArgs;
39 
40 class OptionHandlerException : Exception
41 {
42 	import std.exception;
43 	mixin basicExceptionCtors;
44 }
45 
46 static assert(is(typeof(new OptionHandlerException("message"))));
47 static assert(is(typeof(new OptionHandlerException("message", Exception.init))));
48 
49 struct OptionElement {
50 	string[] opts;
51 	string help;
52 	bool val;
53 }
54 
55 enum OptionType {
56 	Posix,
57 	Windows,
58 	None
59 }
60 
61 @safe pure @nogc struct OptionHandler {
62 
63 	this(string[] args, OptionType type = __currentType)
64 	{
65 		_args = args;
66 		_type = type;
67 	}
68 
69 	bool read(T)(string opts, string help, T* pval = null)
70 	{
71 		OptionElement opte;
72 		import std.array : split;
73 		import std.algorithm : sort, any;
74 		import std.array : array;
75 		opte.opts = opts.split("|").sort!((a, b) => a.length < b.length)().array();
76 		opte.val = !is(T == bool);
77 		opte.help = help;
78 
79 		if(_opts.any!(o => o.opts == opte.opts)())
80 			throw new OptionHandlerException("Trying to read the same option name twice");
81 
82 		_opts ~= opte;
83 	}
84 
85 private:
86 	string[] _args;
87 	OptionElement[] _opts;
88 	OptionType _type;
89 
90 	version(Windows) enum OptionType __currentType = OptionType.Windows;
91 	else enum OptionType __currentType = OptionType.Posix;
92 }
93 
94 @system unittest
95 {
96 	auto args = ["prog", "--foo", "-b"];
97 	bool isFoo;
98 	OptionHandler optHandler = OptionHandler(args);
99 	optHandler.read("foo|f", "Some information about foo.", &isFoo);
100 	//assert(isFoo == true);
101 }