2023-03-29 16:27:02 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/StringView.h>
|
|
|
|
#include <luna/Vector.h>
|
|
|
|
|
2023-04-07 08:40:46 +00:00
|
|
|
namespace os
|
2023-03-29 16:27:02 +00:00
|
|
|
{
|
2023-04-07 08:40:46 +00:00
|
|
|
class ArgumentParser
|
2023-03-29 19:46:07 +00:00
|
|
|
{
|
2023-04-07 08:40:46 +00:00
|
|
|
public:
|
2023-04-28 14:33:05 +00:00
|
|
|
ArgumentParser();
|
2023-04-19 17:16:45 +00:00
|
|
|
|
|
|
|
void add_description(StringView description);
|
2023-04-07 08:40:46 +00:00
|
|
|
|
|
|
|
Result<void> add_positional_argument(StringView& out, StringView name, bool required);
|
|
|
|
Result<void> add_positional_argument(StringView& out, StringView name, StringView fallback);
|
|
|
|
|
2023-04-19 17:16:45 +00:00
|
|
|
Result<void> add_switch_argument(bool& out, char short_flag, StringView long_flag, StringView help = {});
|
2023-04-07 08:40:46 +00:00
|
|
|
|
2023-04-19 17:16:45 +00:00
|
|
|
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, bool value_required,
|
|
|
|
StringView help = {});
|
|
|
|
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, StringView fallback,
|
|
|
|
StringView help = {});
|
2023-04-07 08:40:46 +00:00
|
|
|
|
|
|
|
Result<Vector<StringView>> parse(int argc, char* const* argv);
|
|
|
|
|
2023-04-28 14:33:05 +00:00
|
|
|
struct ProgramInfo
|
|
|
|
{
|
|
|
|
StringView name;
|
|
|
|
StringView version;
|
|
|
|
StringView copyright;
|
|
|
|
StringView license;
|
|
|
|
StringView authors;
|
|
|
|
StringView package;
|
|
|
|
};
|
|
|
|
|
|
|
|
void add_program_info(ProgramInfo info);
|
|
|
|
|
|
|
|
/* Used from programs that are part of the Luna source tree, to add the same version info for all programs.
|
|
|
|
* Should not be used otherwise. */
|
|
|
|
void add_system_program_info(StringView name);
|
|
|
|
|
2023-04-07 08:40:46 +00:00
|
|
|
private:
|
|
|
|
struct PositionalArgument
|
|
|
|
{
|
|
|
|
StringView* out;
|
|
|
|
StringView name;
|
|
|
|
bool required;
|
|
|
|
StringView fallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SwitchArgument
|
|
|
|
{
|
|
|
|
bool* out;
|
|
|
|
char short_flag;
|
|
|
|
StringView long_flag;
|
2023-04-19 17:16:45 +00:00
|
|
|
StringView help;
|
2023-04-07 08:40:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ValueArgument
|
|
|
|
{
|
|
|
|
StringView* out;
|
|
|
|
char short_flag;
|
|
|
|
StringView long_flag;
|
|
|
|
bool required;
|
|
|
|
StringView fallback;
|
2023-04-19 17:16:45 +00:00
|
|
|
StringView help;
|
2023-04-07 08:40:46 +00:00
|
|
|
};
|
|
|
|
|
2023-04-19 17:16:45 +00:00
|
|
|
Result<void> usage(StringView program_name);
|
|
|
|
void short_usage(StringView program_name);
|
|
|
|
|
2023-04-28 14:33:05 +00:00
|
|
|
void version();
|
|
|
|
|
2023-04-07 08:40:46 +00:00
|
|
|
Vector<PositionalArgument> m_positional_args;
|
|
|
|
Vector<SwitchArgument> m_switch_args;
|
|
|
|
Vector<ValueArgument> m_value_args;
|
2023-04-28 14:33:05 +00:00
|
|
|
ProgramInfo m_program_info;
|
2023-04-19 17:16:45 +00:00
|
|
|
StringView m_description = {};
|
|
|
|
bool m_add_short_help_flag { false };
|
|
|
|
bool m_add_long_help_flag { false };
|
2023-04-28 14:33:05 +00:00
|
|
|
bool m_add_short_version_flag { false };
|
|
|
|
bool m_add_long_version_flag { false };
|
2023-03-29 19:46:07 +00:00
|
|
|
};
|
2023-04-07 08:40:46 +00:00
|
|
|
}
|