JD2022-TU1/main/extern/dtest/dtcore/inc/dtcommandoption.h

120 lines
3.9 KiB
C++

#pragma once
#include "dtcore/inc/dtstring.h"
#include "dtcore/inc/dtvector.h"
#include "dtcore/inc/dtmap.h"
namespace dt
{
class CommandCategory
{
public:
DT_CORE_DLL_ENTRY CommandCategory() = default;
DT_CORE_DLL_ENTRY CommandCategory(const char* name, const char* description = nullptr);
DT_CORE_DLL_ENTRY CommandCategory(const CommandCategory& rhs);
DT_CORE_DLL_ENTRY CommandCategory& operator=(const CommandCategory& rhs);
const char* m_Name = nullptr;
const char* m_Description = nullptr;
};
class CommandDeprecation
{
public:
DT_CORE_DLL_ENTRY CommandDeprecation() = default;
DT_CORE_DLL_ENTRY CommandDeprecation(const char* message);
DT_CORE_DLL_ENTRY const char* GetReason() const { return m_DeprecationMessage; };
DT_CORE_DLL_ENTRY bool IsDeprecated() const { return m_Deprecated; };
private:
const char* m_DeprecationMessage = nullptr;
bool m_Deprecated = false;
};
class Command
{
public:
DT_CORE_DLL_ENTRY Command(const char* name);
DT_CORE_DLL_ENTRY Command(const char* name,
const char* shortcut,
const char* description,
int flags,
const char* defaultValue,
const CommandCategory& category = CommandCategory(),
const CommandDeprecation& deprecation = CommandDeprecation());
// This enum should be used for bit-fields. One command can fit multiple flags.
enum Flags
{
FLAG_NONE = 1 << 0, // Default flag. Describes a command line option that has an associated value: e.g. -t TestName
FLAG_REQUIRED = 1 << 1, // Describes a command line option that needs to be specified, else an error message will be displayed.
FLAG_NOVALUE = 1 << 2, // Describes a command line option that doesn't need an associated value: e.g. -h
};
const char* m_Name = nullptr;
const char* m_Shortcut = nullptr;
const char* m_Description = nullptr;
int m_Flags = 0;
const char* m_DefaultValue = nullptr;
CommandCategory m_Category;
CommandDeprecation m_Deprecation;
};
class CommandOption
{
public:
DT_CORE_DLL_ENTRY CommandOption(const String& appName,
const String& appDescription,
const Command* commands);
DT_CORE_DLL_ENTRY bool Parse(SizeT argc, char** argv);
DT_CORE_DLL_ENTRY bool GetValues(const String& command, Strings& values);
DT_CORE_DLL_ENTRY bool GetValues(const String& command, Vector<S32>& values);
DT_CORE_DLL_ENTRY bool GetValue(const String& command, String& value);
DT_CORE_DLL_ENTRY bool GetValue(const String& command, int& value);
DT_CORE_DLL_ENTRY const String& GetCompleteCommand() const { return m_OriginalArguments; }
DT_CORE_DLL_ENTRY bool IsSpecified(const String& command);
DT_CORE_DLL_ENTRY bool IsValueSpecified(const String& command);
DT_CORE_DLL_ENTRY void ShowHelp();
DT_CORE_DLL_ENTRY void ShowLatexHelp();
DT_CORE_DLL_ENTRY void ShowVersion();
DT_CORE_DLL_ENTRY void ShowLatexVersion();
DT_CORE_DLL_ENTRY void DidNothing();
DT_CORE_DLL_ENTRY bool HasDoneSomething() const { return m_HasDoneSomething; }
DT_CORE_DLL_ENTRY void Error(const String& message);
private:
typedef Vector<const Command*> Commands;
typedef Map<const char*, Strings, ConstCharPtrLess> ValueMap;
String m_AppName;
String m_AppDescription;
String m_OriginalArguments;
Commands m_Commands;
ValueMap m_Values;
bool m_ParsingSucceeded;
bool m_HasDoneSomething;
const Command* GetCommand(const String& name);
Strings GetSimilarCommands(const String& name);
String EscapeLatexSymbols(const String& value);
};
}