Refactor usage message (#7803)

* Update usage string formation

* Rework help message to use code generator interface

* update

* refactor
This commit is contained in:
Khanh Nguyen
2023-01-28 11:17:36 -08:00
committed by GitHub
parent 5b7a02d037
commit a105c26eca
3 changed files with 95 additions and 141 deletions

View File

@@ -114,7 +114,7 @@ class FlatCompiler {
explicit FlatCompiler(const InitParams &params) : params_(params) {}
bool RegisterCodeGenerator(const std::string& flag,
bool RegisterCodeGenerator(const FlatCOption &option,
std::shared_ptr<CodeGenerator> code_generator);
int Compile(const FlatCOptions &options);

View File

@@ -241,6 +241,9 @@ const static FlatCOption flatc_options[] = {
"Currently this is required to generate private types in Rust" },
};
auto cmp = [](FlatCOption a, FlatCOption b) { return a.long_opt < b.long_opt; };
static std::set<FlatCOption, decltype(cmp)> language_options(cmp);
static void AppendTextWrappedString(std::stringstream &ss, std::string &text,
size_t max_col, size_t start_col) {
size_t max_line_length = max_col - start_col;
@@ -312,12 +315,19 @@ std::string FlatCompiler::GetShortUsageString(
const std::string &program_name) const {
std::stringstream ss;
ss << "Usage: " << program_name << " [";
for (const FlatCOption &option : language_options) {
AppendShortOption(ss, option);
ss << ", ";
}
// TODO(derekbailey): These should be generated from this.generators
for (size_t i = 0; i < params_.num_generators; ++i) {
const Generator &g = params_.generators[i];
AppendShortOption(ss, g.option);
ss << ", ";
}
for (const FlatCOption &option : flatc_options) {
AppendShortOption(ss, option);
ss << ", ";
@@ -335,6 +345,11 @@ std::string FlatCompiler::GetUsageString(
std::stringstream ss;
ss << "Usage: " << program_name
<< " [OPTION]... FILE... [-- BINARY_FILE...]\n";
for (const FlatCOption &option : language_options) {
AppendOption(ss, option, 80, 25);
}
// TODO(derekbailey): These should be generated from this.generators
for (size_t i = 0; i < params_.num_generators; ++i) {
const Generator &g = params_.generators[i];
@@ -1018,12 +1033,31 @@ int FlatCompiler::Compile(const FlatCOptions &options) {
}
bool FlatCompiler::RegisterCodeGenerator(
const std::string &flag, std::shared_ptr<CodeGenerator> code_generator) {
if (code_generators_.find(flag) != code_generators_.end()) {
Error("multiple generators registered under: " + flag, false, false);
const FlatCOption &option, std::shared_ptr<CodeGenerator> code_generator) {
if (!option.short_opt.empty() &&
code_generators_.find("-" + option.short_opt) != code_generators_.end()) {
Error("multiple generators registered under: -" + option.short_opt, false,
false);
return false;
}
code_generators_[flag] = std::move(code_generator);
if (!option.short_opt.empty()) {
code_generators_["-" + option.short_opt] = code_generator;
}
if (!option.long_opt.empty() &&
code_generators_.find("--" + option.long_opt) != code_generators_.end()) {
Error("multiple generators registered under: --" + option.long_opt, false,
false);
return false;
}
if (!option.long_opt.empty()) {
code_generators_["--" + option.long_opt] = code_generator;
}
language_options.insert(option);
return true;
}

View File

@@ -78,88 +78,17 @@ int main(int argc, const char *argv[]) {
g_program_name = argv[0];
const flatbuffers::FlatCompiler::Generator generators[] = {
{ flatbuffers::GenerateBinary, "binary", false, nullptr,
flatbuffers::IDLOptions::kBinary,
flatbuffers::FlatCOption{
"b", "binary", "",
"Generate wire format binaries for any data definitions" },
flatbuffers::BinaryMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateTextFile, "text", false, nullptr,
flatbuffers::IDLOptions::kJson,
flatbuffers::FlatCOption{
"t", "json", "", "Generate text output for any data definitions" },
flatbuffers::TextMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateCPP, "C++", true, flatbuffers::GenerateCppGRPC,
flatbuffers::IDLOptions::kCpp,
flatbuffers::FlatCOption{ "c", "cpp", "",
"Generate C++ headers for tables/structs" },
flatbuffers::CPPMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateGo, "Go", true, flatbuffers::GenerateGoGRPC,
flatbuffers::IDLOptions::kGo,
flatbuffers::FlatCOption{ "g", "go", "",
"Generate Go files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateJava, "Java", true, flatbuffers::GenerateJavaGRPC,
flatbuffers::IDLOptions::kJava,
flatbuffers::FlatCOption{ "j", "java", "",
"Generate Java classes for tables/structs" },
flatbuffers::JavaMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateDart, "Dart", true, nullptr,
flatbuffers::IDLOptions::kDart,
flatbuffers::FlatCOption{ "d", "dart", "",
"Generate Dart classes for tables/structs" },
flatbuffers::DartMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateTS, "TypeScript", true, flatbuffers::GenerateTSGRPC,
flatbuffers::IDLOptions::kTs,
flatbuffers::FlatCOption{ "T", "ts", "",
"Generate TypeScript code for tables/structs" },
flatbuffers::TSMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateCSharp, "C#", true, nullptr,
flatbuffers::IDLOptions::kCSharp,
flatbuffers::FlatCOption{ "n", "csharp", "",
"Generate C# classes for tables/structs" },
flatbuffers::CSharpMakeRule, nullptr, nullptr },
{ flatbuffers::GeneratePython, "Python", true,
flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
flatbuffers::FlatCOption{ "p", "python", "",
"Generate Python files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateLobster, "Lobster", true, nullptr,
flatbuffers::IDLOptions::kLobster,
flatbuffers::FlatCOption{ "", "lobster", "",
"Generate Lobster files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateLua, "Lua", true, nullptr,
flatbuffers::IDLOptions::kLua,
flatbuffers::FlatCOption{ "l", "lua", "",
"Generate Lua files for tables/structs" },
nullptr, bfbs_gen_lua.get(), nullptr },
{ flatbuffers::GenerateRust, "Rust", true, nullptr,
flatbuffers::IDLOptions::kRust,
flatbuffers::FlatCOption{ "r", "rust", "",
"Generate Rust files for tables/structs" },
flatbuffers::RustMakeRule, nullptr,
flatbuffers::GenerateRustModuleRootFile },
{ flatbuffers::GeneratePhp, "PHP", true, nullptr,
flatbuffers::IDLOptions::kPhp,
flatbuffers::FlatCOption{ "", "php", "",
"Generate PHP files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateKotlin, "Kotlin", true, nullptr,
flatbuffers::IDLOptions::kKotlin,
flatbuffers::FlatCOption{ "", "kotlin", "",
"Generate Kotlin classes for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateJsonSchema, "JsonSchema", true, nullptr,
flatbuffers::IDLOptions::kJsonSchema,
flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateSwift, "swift", true, flatbuffers::GenerateSwiftGRPC,
flatbuffers::IDLOptions::kSwift,
flatbuffers::FlatCOption{ "", "swift", "",
"Generate Swift files for tables/structs" },
nullptr, nullptr, nullptr },
{ nullptr, "Nim", true, nullptr, flatbuffers::IDLOptions::kNim,
flatbuffers::FlatCOption{ "", "nim", "",
"Generate Nim files for tables/structs" },
@@ -174,84 +103,75 @@ int main(int argc, const char *argv[]) {
flatbuffers::FlatCompiler flatc(params);
std::shared_ptr<flatbuffers::CodeGenerator> binary_generator =
flatbuffers::NewBinaryCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{
"b", "binary", "",
"Generate wire format binaries for any data definitions" },
flatbuffers::NewBinaryCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> cpp_generator =
flatbuffers::NewCppCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "c", "cpp", "",
"Generate C++ headers for tables/structs" },
flatbuffers::NewCppCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> csharp_generator =
flatbuffers::NewCSharpCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "n", "csharp", "",
"Generate C# classes for tables/structs" },
flatbuffers::NewCSharpCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> dart_generator =
flatbuffers::NewDartCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "d", "dart", "",
"Generate Dart classes for tables/structs" },
flatbuffers::NewDartCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> go_generator =
flatbuffers::NewGoCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "g", "go", "",
"Generate Go files for tables/structs" },
flatbuffers::NewGoCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> java_generator =
flatbuffers::NewJavaCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "j", "java", "",
"Generate Java classes for tables/structs" },
flatbuffers::NewJavaCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> json_schema_generator =
flatbuffers::NewJsonSchemaCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
flatbuffers::NewJsonSchemaCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> kotlin_generator =
flatbuffers::NewKotlinCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "kotlin", "",
"Generate Kotlin classes for tables/structs" },
flatbuffers::NewKotlinCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> lobster_generator =
flatbuffers::NewLobsterCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "lobster", "",
"Generate Lobster files for tables/structs" },
flatbuffers::NewLobsterCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> php_generator =
flatbuffers::NewPhpCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "php", "",
"Generate PHP files for tables/structs" },
flatbuffers::NewPhpCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> python_generator =
flatbuffers::NewPythonCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "p", "python", "",
"Generate Python files for tables/structs" },
flatbuffers::NewPythonCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> rust_generator =
flatbuffers::NewRustCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "r", "rust", "",
"Generate Rust files for tables/structs" },
flatbuffers::NewRustCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> swift_generator =
flatbuffers::NewSwiftCodeGenerator();
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "swift", "",
"Generate Swift files for tables/structs" },
flatbuffers::NewSwiftCodeGenerator());
std::shared_ptr<flatbuffers::CodeGenerator> ts_generator =
flatbuffers::NewTsCodeGenerator();
flatc.RegisterCodeGenerator("--binary", binary_generator);
flatc.RegisterCodeGenerator("-b", binary_generator);
flatc.RegisterCodeGenerator("--cpp", cpp_generator);
flatc.RegisterCodeGenerator("-c", cpp_generator);
flatc.RegisterCodeGenerator("--csharp", csharp_generator);
flatc.RegisterCodeGenerator("-n", csharp_generator);
flatc.RegisterCodeGenerator("--dart", dart_generator);
flatc.RegisterCodeGenerator("-d", dart_generator);
flatc.RegisterCodeGenerator("--go", go_generator);
flatc.RegisterCodeGenerator("-g", go_generator);
flatc.RegisterCodeGenerator("--java", java_generator);
flatc.RegisterCodeGenerator("-j", java_generator);
flatc.RegisterCodeGenerator("--jsonschema", json_schema_generator);
flatc.RegisterCodeGenerator("--kotlin", kotlin_generator);
flatc.RegisterCodeGenerator("--lobster", lobster_generator);
flatc.RegisterCodeGenerator("--php", php_generator);
flatc.RegisterCodeGenerator("--python", python_generator);
flatc.RegisterCodeGenerator("-p", python_generator);
flatc.RegisterCodeGenerator("--rust", rust_generator);
flatc.RegisterCodeGenerator("-r", rust_generator);
flatc.RegisterCodeGenerator("--swift", swift_generator);
flatc.RegisterCodeGenerator("--ts", ts_generator);
flatc.RegisterCodeGenerator("-T", ts_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "T", "ts", "",
"Generate TypeScript code for tables/structs" },
flatbuffers::NewTsCodeGenerator());
// Create the FlatC options by parsing the command line arguments.
const flatbuffers::FlatCOptions &options =