summaryrefslogtreecommitdiff
path: root/idlc/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'idlc/main.cc')
-rw-r--r--idlc/main.cc151
1 files changed, 91 insertions, 60 deletions
diff --git a/idlc/main.cc b/idlc/main.cc
index 7487cae..19b4df5 100644
--- a/idlc/main.cc
+++ b/idlc/main.cc
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include <glib.h>
+#include <getopt.h>
#include <iostream>
#include <cstring>
@@ -37,6 +37,7 @@ namespace {
class Options {
public:
+ Options();
~Options() = default;
static std::unique_ptr<Options> Parse(int argc, char** argv);
@@ -52,6 +53,7 @@ class Options {
CMD_PROXY,
CMD_STUB,
CMD_VERSION,
+ CMD_HELP,
CMD_MAX
};
@@ -77,6 +79,28 @@ class Options {
bool hasRpcPortLib_ = false;
};
+Options::Options() {
+ help_ = R"__option_cb(
+Usage:
+ tidlc [OPTION...]
+
+Help Options:
+ -h, --help Show help options
+
+Additional Options:
+ -l, --language=LANGUAGE Select generating language (C, C++, C#).
+ -i, --input=INPUT A tidl interface file.
+ -o, --output=OUTPUT The generated interface file.
+ -n, --namespace Add the prefix in the funtion name as output file name (C language only).
+ -r, --rpclib Generate C# library for rpc-port (C# language only).
+
+Application Options:
+ -p, --proxy Generate proxy code
+ -s, --stub Generate stub code
+ -v, --version Show version information
+)__option_cb";
+}
+
void Options::PrintUsage() {
std::cerr << help_ << std::endl;
}
@@ -86,74 +110,84 @@ void Options::PrintVersion() {
}
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 },
- { "version", 'v', 0, G_OPTION_ARG_NONE, &cmd[CMD_VERSION],
- "Show version information", 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" },
- { "namespace", 'n', 0, G_OPTION_ARG_NONE, &opt[OPT_NAMESPACE],
- "Add the prefix in the funtion name as output file name " \
- "(C language only).",
- NULL },
- { "rpclib", 'r', 0, G_OPTION_ARG_NONE, &opt[OPT_RPCLIB],
- "Generate C# library for rpc-port " \
- "(C# language only).",
- NULL },
- { NULL }
+ int cmd[CMD_MAX] = { 0, };
+ int opt[OPT_MAX] = { 0, };
+ auto options = std::unique_ptr<Options>(new Options());
+ int option_index = 0;
+
+ struct option long_options[] = {
+ {"proxy", no_argument, NULL, 'p'},
+ {"stub", no_argument, NULL, 's'},
+ {"version", no_argument, NULL, 'v'},
+ {"help", no_argument, NULL, 'h'},
+ {"language", required_argument, NULL, 'l'},
+ {"input", required_argument, NULL, 'i'},
+ {"output", required_argument, NULL, 'o'},
+ {"namespace", no_argument, NULL, 'n'},
+ {"rpclib", no_argument, NULL, 'r'},
+ {0, 0, 0, 0}
};
- 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);
+ while (true) {
+ int c = getopt_long(argc, argv, "psvhl:i:o:nr", long_options,
+ &option_index);
+ if (c == -1)
+ break;
- 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);
-
- 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);
- }
+ switch (c) {
+ case 0:
+ break;
+
+ case 'p':
+ cmd[CMD_PROXY] = 1;
+ break;
+
+ case 's':
+ cmd[CMD_STUB] = 1;
+ break;
+
+ case 'v':
+ cmd[CMD_VERSION] = 1;
+ break;
+
+ case 'h':
+ cmd[CMD_HELP] = 1;
+ break;
- gchar* help = g_option_context_get_help(context, TRUE, NULL);
+ case 'n':
+ opt[OPT_NAMESPACE] = 1;
+ break;
- std::unique_ptr<Options> options(new Options());
- options->help_ = help;
+ case 'r':
+ opt[OPT_RPCLIB] = 1;
+ break;
- g_free(help);
- g_option_context_free(context);
+ case 'l':
+ opt[OPT_LANGUAGE] = 1;
+ options->language_ = optarg;
+ break;
+
+ case 'i':
+ opt[OPT_INPUT] = 1;
+ options->input_ = optarg;
+ break;
+
+ case 'o':
+ opt[OPT_OUTPUT] = 1;
+ options->output_ = optarg;
+ break;
+
+ default:
+ cmd[CMD_HELP] = 1;
+ }
+ }
if (cmd[CMD_VERSION]) {
options->PrintVersion();
return std::unique_ptr<Options>(nullptr);
}
- if ((!cmd[CMD_PROXY] && !cmd[CMD_STUB]) ||
+ if (cmd[CMD_HELP] || (!cmd[CMD_PROXY] && !cmd[CMD_STUB]) ||
!opt[OPT_LANGUAGE] || !opt[OPT_INPUT] ||
!opt[OPT_OUTPUT]) {
options->PrintUsage();
@@ -161,9 +195,6 @@ std::unique_ptr<Options> Options::Parse(int argc, char** argv) {
}
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]);
options->hasNamespace_ = opt[OPT_NAMESPACE] ? true : false;
options->hasRpcPortLib_ = opt[OPT_RPCLIB] ? true : false;