Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
CommandLine.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Strings/StringView.h"
6
7namespace SC
8{
9struct StringFormatOutput;
10
11struct SC_COMPILER_EXPORT CommandLineParseResult
12{
13 enum class Status : uint8_t
14 {
15 Success,
16 HelpRequested,
17 Error
18 };
19
20 enum class Error : uint8_t
21 {
22 None,
23 UnknownOption,
24 MissingOptionValue,
25 UnexpectedOptionValue,
26 InvalidOptionValue,
27 MissingPositionalValue,
28 TooManyPositionals,
29 InsufficientArgumentStorage,
30 InsufficientPositionalStorage,
31 InvalidShortOptionGroup
32 };
33
34 Status status = Status::Success;
35 Error error = Error::None;
36 StringView argument = {};
37 StringView value = {};
38 StringView name = {};
39};
40
41struct SC_COMPILER_EXPORT CommandLineValue
42{
43 enum class Type : uint8_t
44 {
45 None,
46 Bool,
47 Int32,
48 UInt16,
51 Custom
52 };
53
54 using CustomParser = bool (*)(void* userData, StringSpan value);
55
56 Type type = Type::None;
57 void* storage = nullptr;
58 CustomParser customParse = nullptr;
59
60 static CommandLineValue none();
61 static CommandLineValue boolean(bool& value);
62 static CommandLineValue int32(int32_t& value);
63 static CommandLineValue uint16(uint16_t& value);
64 static CommandLineValue stringView(SC::StringView& value);
65 static CommandLineValue stringSpan(SC::StringSpan& value);
66 static CommandLineValue custom(void* userData, CustomParser customParse);
67
68 [[nodiscard]] bool requiresValue() const;
69};
70
71struct SC_COMPILER_EXPORT CommandLineOption
72{
73 StringView longName = {};
74 StringView negativeLongName = {};
75 StringView help = {};
76 StringView valueName = {};
77 char shortName = 0;
78 char negativeShortName = 0;
79 bool required = false;
80 CommandLineValue value = {};
81};
82
83struct SC_COMPILER_EXPORT CommandLinePositional
84{
85 StringView name = {};
86 StringView help = {};
87
88 bool required = true;
89 bool remaining = false;
90
91 CommandLineValue value = {};
92
93 Span<StringSpan> remainingStorage = {};
94 Span<StringSpan>* parsedValues = nullptr;
95};
96
97struct SC_COMPILER_EXPORT CommandLineSpec
98{
99 StringView programName = {};
100 StringView summary = {};
101
103 Span<const CommandLinePositional> positionals = {};
104
105 [[nodiscard]] CommandLineParseResult parse(Span<const StringSpan> args) const;
106
107 [[nodiscard]] bool writeHelp(StringFormatOutput& output) const;
108 [[nodiscard]] bool writeError(const CommandLineParseResult& result, StringFormatOutput& output) const;
109};
110
111struct SC_COMPILER_EXPORT CommandLineArguments
112{
113 Span<StringSpan> values = {};
114
115 [[nodiscard]] Result setFromMainArguments(int argc, const char* const* argv, Span<StringSpan> storage);
116 [[nodiscard]] Result setFromMainArguments(int argc, char** argv, Span<StringSpan> storage);
117#if SC_PLATFORM_WINDOWS
118 [[nodiscard]] Result setFromMainArguments(int argc, const wchar_t* const* argv, Span<StringSpan> storage);
119 [[nodiscard]] Result setFromMainArguments(int argc, wchar_t** argv, Span<StringSpan> storage);
120#endif
121};
122
123} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
int int32_t
Platform independent (4) bytes signed int.
Definition PrimitiveTypes.h:46
Definition CommandLine.h:112
Definition CommandLine.h:72
Definition CommandLine.h:12
Definition CommandLine.h:84
Definition CommandLine.h:98
Definition CommandLine.h:42
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Definition StringFormat.h:26
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:46