[PATCH] osmo-iuh[master]: hnbgw: add cmdline arg parsing with default options

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Thu Aug 18 01:26:41 UTC 2016


Review at  https://gerrit.osmocom.org/706

hnbgw: add cmdline arg parsing with default options

Default options taken from osmo-nitb:

    -h --help                  This text.
    -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN  Enable debugging.
    -D --daemonize             Fork the process into a background daemon.
    -s --disable-color
    -T --timestamp             Prefix every log line with a timestamp.
    -V --version               Print the version of OsmoHNBGW.
    -e --log-level number      Set a global loglevel.

Change-Id: I931cee01c605c1b507c16041ada226cf963ea433
---
M src/hnbgw.c
1 file changed, 81 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-iuh refs/changes/06/706/1

diff --git a/src/hnbgw.c b/src/hnbgw.c
index 4211da9..ff5fafe 100644
--- a/src/hnbgw.c
+++ b/src/hnbgw.c
@@ -326,8 +326,6 @@
 	.version	= "0",
 };
 
-static int daemonize = 0;
-
 static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb)
 {
 	struct hnbgw_context_map *map;
@@ -388,6 +386,83 @@
 	logging_vty_add_cmds(&hnbgw_log_info);
 }
 
+
+static struct {
+	int daemonize;
+} hnbgw_cmdline_config = {
+	0
+};
+
+static void print_usage()
+{
+	printf("Usage: osmo-hnbgw\n");
+}
+
+static void print_help()
+{
+	printf("  -h --help                  This text.\n");
+	printf("  -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN  Enable debugging.\n");
+	printf("  -D --daemonize             Fork the process into a background daemon.\n");
+	printf("  -s --disable-color\n");
+	printf("  -T --timestamp             Prefix every log line with a timestamp.\n");
+	printf("  -V --version               Print the version of OsmoHNBGW.\n");
+	printf("  -e --log-level number      Set a global loglevel.\n");
+}
+
+static void handle_options(int argc, char **argv)
+{
+	while (1) {
+		int option_index = 0, c;
+		static struct option long_options[] = {
+			{"help", 0, 0, 'h'},
+			{"debug", 1, 0, 'd'},
+			{"daemonize", 0, 0, 'D'},
+			{"disable-color", 0, 0, 's'},
+			{"timestamp", 0, 0, 'T'},
+			{"version", 0, 0, 'V' },
+			{"log-level", 1, 0, 'e'},
+			{0, 0, 0, 0}
+		};
+
+		c = getopt_long(argc, argv, "hd:DsTVe:",
+				long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_usage();
+			print_help();
+			exit(0);
+		case 's':
+			log_set_use_color(osmo_stderr_target, 0);
+			break;
+		case 'd':
+			log_parse_category_mask(osmo_stderr_target, optarg);
+			break;
+		case 'D':
+			hnbgw_cmdline_config.daemonize = 1;
+			break;
+		case 'T':
+			log_set_print_timestamp(osmo_stderr_target, 1);
+			break;
+		case 'e':
+			log_set_log_level(osmo_stderr_target, atoi(optarg));
+			break;
+		case 'V':
+			print_version(1);
+			exit(0);
+			break;
+		default:
+			/* catch unknown options *as well as* missing arguments. */
+			fprintf(stderr, "Error in command line options. Exiting.\n");
+			exit(-1);
+			break;
+		}
+	}
+}
+
+
 int main(int argc, char **argv)
 {
 	struct osmo_sccp_user *sua_user;
@@ -409,6 +484,9 @@
 	vty_init(&vty_info);
 
 	hnbgw_vty_init();
+
+	/* Handle options after vty_init(), for --version */
+	handle_options(argc, argv);
 
 	/* NOTE: if we add a config file, read the config before
 	 * fetching the telnet address with vty_get_bind_addr() */
@@ -443,7 +521,7 @@
 	}
 	g_hnb_gw->iuh = srv;
 
-	if (daemonize) {
+	if (hnbgw_cmdline_config.daemonize) {
 		rc = osmo_daemonize();
 		if (rc < 0) {
 			perror("Error during daemonize");

-- 
To view, visit https://gerrit.osmocom.org/706
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I931cee01c605c1b507c16041ada226cf963ea433
Gerrit-PatchSet: 1
Gerrit-Project: osmo-iuh
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list