Add commandlets
This commit is contained in:
parent
79a8202121
commit
5491aecd5f
8 changed files with 298 additions and 23 deletions
69
src/commandlets/commandlet.h
Normal file
69
src/commandlets/commandlet.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "templates.h"
|
||||
#include "zstring.h"
|
||||
#include "printf.h"
|
||||
#include "m_argv.h"
|
||||
|
||||
class Commandlet
|
||||
{
|
||||
public:
|
||||
virtual ~Commandlet() = default;
|
||||
|
||||
virtual void OnCommand(FArgs args) = 0;
|
||||
virtual void OnPrintHelp() = 0;
|
||||
|
||||
const FString& GetLongFormName() const { return LongFormName; }
|
||||
const FString& GetShortDescription() const { return ShortDescription; }
|
||||
|
||||
protected:
|
||||
void SetLongFormName(FString name) { LongFormName = std::move(name); }
|
||||
void SetShortDescription(FString desc) { ShortDescription = std::move(desc); }
|
||||
|
||||
private:
|
||||
FString LongFormName;
|
||||
FString ShortDescription;
|
||||
};
|
||||
|
||||
class CommandletGroup
|
||||
{
|
||||
public:
|
||||
void AddGroup(std::unique_ptr<CommandletGroup> group);
|
||||
void AddCommand(std::unique_ptr<Commandlet> command);
|
||||
|
||||
template<typename T>
|
||||
void AddGroup() { AddGroup(std::make_unique<T>()); }
|
||||
|
||||
template<typename T>
|
||||
void AddCommand() { AddCommand(std::make_unique<T>()); }
|
||||
|
||||
const FString& GetLongFormName() const { return LongFormName; }
|
||||
const FString& GetShortDescription() const { return ShortDescription; }
|
||||
|
||||
protected:
|
||||
void SetLongFormName(FString name) { LongFormName = std::move(name); }
|
||||
void SetShortDescription(FString desc) { ShortDescription = std::move(desc); }
|
||||
|
||||
private:
|
||||
FString LongFormName;
|
||||
FString ShortDescription;
|
||||
|
||||
TArray<std::unique_ptr<CommandletGroup>> Groups;
|
||||
TArray<std::unique_ptr<Commandlet>> Commands;
|
||||
|
||||
friend class RootCommandlet;
|
||||
};
|
||||
|
||||
class RootCommandlet : public CommandletGroup
|
||||
{
|
||||
public:
|
||||
RootCommandlet();
|
||||
void RunCommand();
|
||||
|
||||
private:
|
||||
void RunCommand(CommandletGroup* group, FArgs args, const FString prefix);
|
||||
void PrintCommandList(CommandletGroup* group, const FString prefix);
|
||||
void PrintDetailHelp(CommandletGroup* group, FArgs args);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue