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 "../Common/Result.h"
5#include "../Strings/StringView.h"
6#include "StringsError.h"
7#include "StringsExport.h"
8
9namespace SC
10{
11struct StringFormatOutput;
12
13struct SC_STRINGS_EXPORT CommandLineParseResult
14{
15 enum class Status : uint8_t
16 {
17 Success,
18 HelpRequested,
19 Error
20 };
21
22 enum class Error : uint8_t
23 {
24 None,
25 UnknownOption,
26 MissingOptionValue,
27 UnexpectedOptionValue,
28 InvalidOptionValue,
29 MissingPositionalValue,
30 TooManyPositionals,
31 InsufficientArgumentStorage,
32 InsufficientPositionalStorage,
33 InvalidShortOptionGroup
34 };
35
36 Status status = Status::Success;
37 Error error = Error::None;
38 StringView argument = {};
39 StringView value = {};
40 StringView name = {};
41};
42
43struct SC_STRINGS_EXPORT CommandLineValue
44{
45 enum class Type : uint8_t
46 {
47 None,
48 Bool,
49 Int32,
50 UInt16,
52 StringSpan,
53 Custom
54 };
55
56 using CustomParser = bool (*)(void* userData, StringSpan value);
57
58 Type type = Type::None;
59 void* storage = nullptr;
60 CustomParser customParse = nullptr;
61
62 static CommandLineValue none();
63 static CommandLineValue boolean(bool& value);
64 static CommandLineValue int32(int32_t& value);
65 static CommandLineValue uint16(uint16_t& value);
66 static CommandLineValue stringView(SC::StringView& value);
67 static CommandLineValue stringSpan(SC::StringSpan& value);
68 static CommandLineValue custom(void* userData, CustomParser customParse);
69
70 [[nodiscard]] bool requiresValue() const;
71};
72
73struct SC_STRINGS_EXPORT CommandLineOption
74{
75 StringView longName = {};
76 StringView negativeLongName = {};
77 StringView help = {};
78 StringView valueName = {};
79 char shortName = 0;
80 char negativeShortName = 0;
81 bool required = false;
82 CommandLineValue value = {};
83};
84
85struct SC_STRINGS_EXPORT CommandLinePositional
86{
87 StringView name = {};
88 StringView help = {};
89
90 bool required = true;
91 bool remaining = false;
92
93 CommandLineValue value = {};
94
95 Span<StringSpan> remainingStorage = {};
96 Span<StringSpan>* parsedValues = nullptr;
97};
98
99struct SC_STRINGS_EXPORT CommandLineSpec
100{
101 StringView programName = {};
102 StringView summary = {};
103
104 Span<const CommandLineOption> options = {};
105 Span<const CommandLinePositional> positionals = {};
106
107 [[nodiscard]] CommandLineParseResult parse(Span<const StringSpan> args) const;
108
109 [[nodiscard]] bool writeHelp(StringFormatOutput& output) const;
110 [[nodiscard]] bool writeError(const CommandLineParseResult& result, StringFormatOutput& output) const;
111};
112
113struct SC_STRINGS_EXPORT CommandLineArguments
114{
115 Span<StringSpan> values = {};
116
117 [[nodiscard]] Result setFromMainArguments(int argc, const char* const* argv, Span<StringSpan> storage);
118 [[nodiscard]] Result setFromMainArguments(int argc, char** argv, Span<StringSpan> storage);
119#if SC_PLATFORM_WINDOWS
120 [[nodiscard]] Result setFromMainArguments(int argc, const wchar_t* const* argv, Span<StringSpan> storage);
121 [[nodiscard]] Result setFromMainArguments(int argc, wchar_t** argv, Span<StringSpan> storage);
122#endif
123};
124
125} // namespace SC
Definition CommandLine.h:114
Definition CommandLine.h:74
Definition CommandLine.h:14
Definition CommandLine.h:86
Definition CommandLine.h:100
Definition CommandLine.h:44
Definition StringFormat.h:28
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:49