* limitations under the License.
*/
+#include <glib.h>
+
#include <iostream>
#include <cstring>
+#include <memory>
#include "idlc/parser.h"
#include "idlc/proxy_gen.h"
namespace {
-enum class Option {
- NONE = 0,
- PROXY,
- STUB
+class Options {
+ public:
+ ~Options() = default;
+
+ static std::unique_ptr<Options> Parse(int argc, char** argv);
+ bool IsProxy() const { return isProxy_; }
+ std::string GetLanguage() const { return language_; }
+ std::string GetInput() const { return input_; }
+ std::string GetOutput() const { return output_; }
+
+ private:
+ enum Cmd {
+ CMD_PROXY,
+ CMD_STUB,
+ CMD_MAX
+ };
+
+ enum Opt {
+ OPT_LANGUAGE,
+ OPT_INPUT,
+ OPT_OUTPUT,
+ OPT_MAX
+ };
+
+ void PrintUsage();
+
+ private:
+ bool isProxy_;
+ std::string language_;
+ std::string input_;
+ std::string output_;
+ std::string help_;
};
-void PrintUsage() {
- std::cout << "usage: tidlc <option> <input_file> <output_file>"
- << std::endl
- << "options:" << std::endl
- << " -p generate proxy code" << std::endl
- << " -s generate stub code" << std::endl;
+void Options::PrintUsage() {
+ std::cerr << help_ << std::endl;
}
-} // namespace
+std::unique_ptr<Options> Options::Parse(int argc, char** argv) {
+ gpointer cmd[CMD_MAX] = { 0, };
+ static GOptionEntry cmdEntries[] = {
+ { "proxy", 'p', 0, G_OPTION_ARG_NONE, &cmd[CMD_PROXY],
+ "Generate proxy code", NULL },
+ { "stub", 's', 0, G_OPTION_ARG_NONE, &cmd[CMD_STUB],
+ "Generate stub code", NULL },
+ { NULL }
+ };
+ gpointer opt[OPT_MAX] = { 0, };
+ static GOptionEntry optEntries[] = {
+ { "language", 'l', 0, G_OPTION_ARG_STRING, &opt[OPT_LANGUAGE],
+ "Select generating language (C, C++, C#).", "LANGUAGE" },
+ { "input", 'i', 0, G_OPTION_ARG_STRING, &opt[OPT_INPUT],
+ "A tidl interface file.", "INPUT" },
+ { "output", 'o', 0, G_OPTION_ARG_STRING, &opt[OPT_OUTPUT],
+ "The generated interface file.", "OUTPUT" },
+ { NULL }
+ };
-int main(int argc, char** argv) {
- if (argc != 4) {
- ::PrintUsage();
- exit(1);
+ GOptionContext* context = g_option_context_new(NULL);
+ if (!context) {
+ std::cerr << "Failed to create context" << std::endl;
+ return std::unique_ptr<Options>(nullptr);
+ }
+ g_option_context_add_main_entries(context, cmdEntries, NULL);
+
+ GOptionGroup* optGroup = g_option_group_new("option", "Additional Options:",
+ "Additional options", NULL, NULL);
+ if (!optGroup) {
+ std::cerr << "Failed to create option group" << std::endl;
+ g_option_context_free(context);
+ return std::unique_ptr<Options>(nullptr);
}
+ g_option_group_add_entries(optGroup, optEntries);
+ g_option_context_add_group(context, optGroup);
- ::Option opt = ::Option::NONE;
+ GError* err = NULL;
+ if (!g_option_context_parse(context, &argc, &argv, &err)) {
+ std::cerr << argv[0] << ": " << err->message << std::endl;
+ g_option_context_free(context);
+ g_clear_error(&err);
+ return std::unique_ptr<Options>(nullptr);
+ }
- if (strcmp(argv[1], "-p") == 0)
- opt = ::Option::PROXY;
+ gchar* help = g_option_context_get_help(context, TRUE, NULL);
- if (strcmp(argv[1], "-s") == 0)
- opt = ::Option::STUB;
+ std::unique_ptr<Options> options(new Options());
+ options->help_ = help;
- if (opt == ::Option::NONE) {
- PrintUsage();
- exit(1);
+ g_free(help);
+ g_option_context_free(context);
+
+ if ((!cmd[CMD_PROXY] && !cmd[CMD_STUB]) ||
+ !opt[OPT_LANGUAGE] || !opt[OPT_INPUT] ||
+ !opt[OPT_OUTPUT]) {
+ options->PrintUsage();
+ return std::unique_ptr<Options>(nullptr);
}
+ options->isProxy_ = cmd[CMD_PROXY] ? true : false;
+ options->language_ = static_cast<char*>(opt[OPT_LANGUAGE]);
+ options->input_ = static_cast<char*>(opt[OPT_INPUT]);
+ options->output_ = static_cast<char*>(opt[OPT_OUTPUT]);
+
+ return options;
+}
+
+} // namespace
+
+int main(int argc, char** argv) {
+ std::unique_ptr<::Options> options = ::Options::Parse(argc, argv);
+ if (!options)
+ exit(1);
+
tidl::Parser ps;
- std::string path(argv[2]);
+ std::string path(options->GetInput());
if (!ps.ParseFromFile(path))
exit(1);
- if (opt == ::Option::PROXY) {
+ if (options->IsProxy()) {
tidl::ProxyGen proxy(ps.GetDoc());
- proxy.Run(argv[3]);
+ proxy.Run(options->GetOutput());
} else {
tidl::StubGen stub(ps.GetDoc());
- stub.Run(argv[3]);
+ stub.Run(options->GetOutput());
}
return 0;