Provide a short help text and default in error case (#6992)

* Provide a short help text and default in error case

* clean up short options a bit
This commit is contained in:
Derek Bailey
2022-01-31 21:24:48 -08:00
committed by GitHub
parent 14b19d446f
commit bc366a7f9e
3 changed files with 40 additions and 6 deletions

View File

@@ -84,6 +84,7 @@ class FlatCompiler {
int Compile(int argc, const char **argv);
std::string GetShortUsageString(const char *program_name) const;
std::string GetUsageString(const char *program_name) const;
private:

View File

@@ -65,7 +65,7 @@ const static FlatCOption options[] = {
{ "I", "", "PATH", "Search for includes in the specified path." },
{ "M", "", "", "Print make rules for generated files." },
{ "", "version", "", "Print the version number of flatc and exit." },
{ "", "help", "", "Prints this help text and exit." },
{ "h", "help", "", "Prints this help text and exit." },
{ "", "string-json", "",
"Strict JSON: field names must be / will be quoted, no trailing commas in "
"tables/vectors." },
@@ -184,7 +184,8 @@ const static FlatCOption options[] = {
{ "", "reflect-types", "",
"Add minimal type reflection to code generation." },
{ "", "reflect-names", "", "Add minimal type/name reflection." },
{ "", "rust-serialize", "", "Implement serde::Serialize on generated Rust types." },
{ "", "rust-serialize", "",
"Implement serde::Serialize on generated Rust types." },
{ "", "root-type", "T", "Select or override the default root_type." },
{ "", "require-explicit-ids", "",
"When parsing schemas, require explicit ids (id: x)." },
@@ -270,6 +271,35 @@ static void AppendOption(std::stringstream &ss, const FlatCOption &option,
ss << "\n";
}
static void AppendShortOption(std::stringstream &ss,
const FlatCOption &option) {
if (!option.short_opt.empty()) {
ss << "-" << option.short_opt;
if (!option.long_opt.empty()) { ss << "|"; }
}
if (!option.long_opt.empty()) { ss << "--" << option.long_opt; }
}
std::string FlatCompiler::GetShortUsageString(const char *program_name) const {
std::stringstream ss;
ss << "Usage: " << program_name << " [";
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 : options) {
AppendShortOption(ss, option);
ss << ", ";
}
ss.seekp(-2, ss.cur);
ss << "]... FILE... [-- FILE...]";
std::string help = ss.str();
std::stringstream ss_textwrap;
AppendTextWrappedString(ss_textwrap, help, 80, 0);
return ss_textwrap.str();
}
std::string FlatCompiler::GetUsageString(const char *program_name) const {
std::stringstream ss;
ss << "Usage: " << program_name << " [OPTION]... FILE... [-- FILE...]\n";
@@ -301,6 +331,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
return 0;
}
if (argc <= 1) { Error("Need to provide at least one argument."); }
flatbuffers::IDLOptions opts;
std::string output_path;
@@ -455,7 +487,7 @@ int FlatCompiler::Compile(int argc, const char **argv) {
} else if (arg == "--version") {
printf("flatc version %s\n", FLATC_VERSION());
exit(0);
} else if (arg == "--help") {
} else if (arg == "--help" || arg == "-h") {
printf("%s\n", GetUsageString(program_name).c_str());
exit(0);
} else if (arg == "--grpc") {

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <cstdio>
#include <memory>
#include "bfbs_gen_lua.h"
@@ -27,16 +28,16 @@ static void Warn(const flatbuffers::FlatCompiler *flatc,
const std::string &warn, bool show_exe_name) {
(void)flatc;
if (show_exe_name) { printf("%s: ", g_program_name); }
fprintf(stderr, "warning:\n %s\n\n", warn.c_str());
fprintf(stderr, "\nwarning:\n %s\n\n", warn.c_str());
}
static void Error(const flatbuffers::FlatCompiler *flatc,
const std::string &err, bool usage, bool show_exe_name) {
if (show_exe_name) { printf("%s: ", g_program_name); }
if (usage && flatc) {
fprintf(stderr, "%s\n", flatc->GetUsageString(g_program_name).c_str());
fprintf(stderr, "%s\n", flatc->GetShortUsageString(g_program_name).c_str());
}
fprintf(stderr, "error:\n %s\n\n", err.c_str());
fprintf(stderr, "\nerror:\n %s\n\n", err.c_str());
exit(1);
}