DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
cmdLineOptions.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: cmdLineOptions.hpp
5// Author: crdrisko
6// Date: 11/18/2020-10:26:33
7// Description:
8
9#ifndef CMDLINEOPTIONS_HPP
10#define CMDLINEOPTIONS_HPP
11
12#include <filesystem>
13#include <iostream>
14#include <sstream>
15#include <string>
16#include <typeindex>
17#include <typeinfo>
18#include <unordered_map>
19#include <vector>
20
21namespace GetOpts
22{
23 namespace fs = std::filesystem;
24
25 // clang-format off
26 static inline std::unordered_map<std::type_index, std::string> type_names
27 {
28 {std::type_index(typeid(std::vector<double>)), "ARRAY<DOUBLE>"},
29 {std::type_index(typeid(std::vector<fs::path>)), "ARRAY<FILE>"},
30 {std::type_index(typeid(std::vector<int>)), "ARRAY<INT>"},
31 {std::type_index(typeid(std::vector<std::string>)), "ARRAY<STRING>"},
32 {std::type_index(typeid(double)), "DOUBLE"},
33 {std::type_index(typeid(fs::path)), "FILE"},
34 {std::type_index(typeid(int)), "INT"},
35 {std::type_index(typeid(std::string)), "STRING"}
36 };
37 // clang-format on
38
39 template<template<typename> class... Mixins>
40 struct CommandLineOption : Mixins<CommandLineOption<Mixins...>>...
41 {
42 char flag;
43 std::string helpMessageLine;
44
45 CommandLineOption(char Flag, const std::string& HelpMessageLine)
46 : Mixins<CommandLineOption>()..., flag {Flag}, helpMessageLine {HelpMessageLine}
47 {
48 }
49 };
50
51 template<typename Derived>
53 {
54 private:
55 const Derived* pDerived {static_cast<const Derived*>(this)};
56
57 public:
58 std::string getHelpMessage() const
59 {
60 std::stringstream strm;
61 strm << " -" << pDerived->flag << (Derived::usePrefix ? " REQUIRED: " : " ") << pDerived->helpMessageLine;
62
63 return strm.str();
64 }
65 };
66
67 template<typename Derived>
69 {
70 private:
71 const Derived* pDerived {static_cast<const Derived*>(this)};
72
73 public:
74 std::string getHelpMessage() const
75 {
76 std::stringstream strm;
77 strm << " -" << pDerived->flag << (Derived::usePrefix ? " OPTIONAL: " : " ") << pDerived->helpMessageLine;
78
79 return strm.str();
80 }
81 };
82
83 template<typename Derived>
85 {
86 private:
87 const Derived* pDerived {static_cast<const Derived*>(this)};
88
89 public:
90 static inline constexpr bool usePrefix = false;
91 std::string generateUsage() const { return std::string {pDerived->flag}; }
92 };
93
94 template<typename T, typename Derived>
96 {
97 private:
98 T var;
99 const Derived* pDerived {static_cast<const Derived*>(this)};
100
101 public:
102 static inline constexpr bool usePrefix = true;
103
104 std::string generateUsage() const
105 {
106 std::stringstream strm;
107 strm << "[-" << pDerived->flag << " " << type_names[std::type_index(typeid(var))] << "]";
108
109 return strm.str();
110 }
111 };
112
113
114 // Convinence aliases for possible input types
115 template<typename Derived>
117
118 template<typename Derived>
120
121 template<typename Derived>
123
124 template<typename Derived>
126
127
128 // Possible Command-Line Options
134
140} // namespace GetOpts
141
142#endif
Definition cmdLineOptions.hpp:69
const Derived * pDerived
Definition cmdLineOptions.hpp:71
std::string getHelpMessage() const
Definition cmdLineOptions.hpp:74
Definition cmdLineOptions.hpp:53
std::string getHelpMessage() const
Definition cmdLineOptions.hpp:58
const Derived * pDerived
Definition cmdLineOptions.hpp:55
Definition cmdLineOptions.hpp:96
static constexpr bool usePrefix
Definition cmdLineOptions.hpp:102
std::string generateUsage() const
Definition cmdLineOptions.hpp:104
double var
Definition cmdLineOptions.hpp:98
const Derived * pDerived
Definition cmdLineOptions.hpp:99
Definition cmdLineOptions.hpp:85
std::string generateUsage() const
Definition cmdLineOptions.hpp:91
const Derived * pDerived
Definition cmdLineOptions.hpp:87
static constexpr bool usePrefix
Definition cmdLineOptions.hpp:90
Definition cmdLineOptions.hpp:22
CommandLineOption< Required, WithDoubleArgument > RequiredFlagWithDouble
Definition cmdLineOptions.hpp:136
CommandLineOption< Required, WithStringArgument > RequiredFlagWithString
Definition cmdLineOptions.hpp:139
CommandLineOption< Optional, WithIntegerArgument > OptionalFlagWithInteger
Definition cmdLineOptions.hpp:132
static std::unordered_map< std::type_index, std::string > type_names
Definition cmdLineOptions.hpp:27
WithArgument< fs::path, Derived > WithFileArgument
Definition cmdLineOptions.hpp:119
WithArgument< std::string, Derived > WithStringArgument
Definition cmdLineOptions.hpp:125
CommandLineOption< Optional, WithStringArgument > OptionalFlagWithString
Definition cmdLineOptions.hpp:133
CommandLineOption< Required, WithFileArgument > RequiredFlagWithFile
Definition cmdLineOptions.hpp:137
CommandLineOption< Optional, WithFileArgument > OptionalFlagWithFile
Definition cmdLineOptions.hpp:131
CommandLineOption< Optional, WithDoubleArgument > OptionalFlagWithDouble
Definition cmdLineOptions.hpp:130
CommandLineOption< Required, WithIntegerArgument > RequiredFlagWithInteger
Definition cmdLineOptions.hpp:138
CommandLineOption< Optional, WithoutArgument > OptionalFlag
Definition cmdLineOptions.hpp:129
WithArgument< int, Derived > WithIntegerArgument
Definition cmdLineOptions.hpp:122
CommandLineOption< Required, WithoutArgument > RequiredFlag
Definition cmdLineOptions.hpp:135
WithArgument< double, Derived > WithDoubleArgument
Definition cmdLineOptions.hpp:116
Definition cmdLineOptions.hpp:41
char flag
Definition cmdLineOptions.hpp:42
CommandLineOption(char Flag, const std::string &HelpMessageLine)
Definition cmdLineOptions.hpp:45
std::string helpMessageLine
Definition cmdLineOptions.hpp:43