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