yanglint REFACTOR setting dbg_groups
diff --git a/tools/lint/cmd_debug.c b/tools/lint/cmd_debug.c
index 6c59586..c99527d 100644
--- a/tools/lint/cmd_debug.c
+++ b/tools/lint/cmd_debug.c
@@ -27,10 +27,30 @@
 #include "common.h"
 #include "yl_opt.h"
 
+struct debug_groups {
+    char *name;
+    uint32_t flag;
+} const dg [] = {
+    {"dict", LY_LDGDICT},
+    {"xpath", LY_LDGXPATH},
+    {"dep-sets", LY_LDGDEPSETS},
+};
+#define DG_LENGTH (sizeof dg / sizeof *dg)
+
 void
 cmd_debug_help(void)
 {
-    printf("Usage: debug (dict | xpath | dep-sets)+\n");
+    uint32_t i;
+
+    printf("Usage: debug (");
+    for (i = 0; i < DG_LENGTH; i++) {
+        if ((i + 1) == DG_LENGTH) {
+            printf("%s", dg[i].name);
+        } else {
+            printf("%s | ", dg[i].name);
+        }
+    }
+    printf(")+\n");
 }
 
 int
@@ -69,7 +89,7 @@
 {
     (void) yo;
 
-    if (!posc) {
+    if (yo->interactive && !posc) {
         /* no argument */
         cmd_debug_help();
         return 1;
@@ -82,16 +102,21 @@
 cmd_debug_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
 {
     (void) ctx;
+    uint32_t i;
+    ly_bool set;
 
     assert(posv);
 
-    if (!strcasecmp("dict", posv)) {
-        yo->dbg_groups |= LY_LDGDICT;
-    } else if (!strcasecmp("xpath", posv)) {
-        yo->dbg_groups |= LY_LDGXPATH;
-    } else if (!strcasecmp("dep-sets", posv)) {
-        yo->dbg_groups |= LY_LDGDEPSETS;
-    } else {
+    set = 0;
+    for (i = 0; i < DG_LENGTH; i++) {
+        if (!strcasecmp(posv, dg[i].name)) {
+            yo->dbg_groups |= dg[i].flag;
+            set = 1;
+            break;
+        }
+    }
+
+    if (!set) {
         YLMSG_E("Unknown debug group \"%s\"\n", posv);
         return 1;
     }
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index 1db3518..696460b 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -394,6 +394,44 @@
     return -1;
 }
 
+#ifndef NDEBUG
+/**
+ * @brief Enable specific debugging messages.
+ *
+ * @param[in] groups String in the form <group>[,group>]*.
+ * @param[in,out] yo Options for yanglint.
+ * return 0 on success.
+ */
+static int
+set_debug_groups(char *groups, struct yl_opt *yo)
+{
+    int rc;
+    char *str, *end;
+
+    /* Process all debug arguments except the last one. */
+    for (str = groups; (end = strchr(str, ',')); str = end + 1) {
+        /* Temporary modify input string. */
+        *end = '\0';
+        rc = cmd_debug_exec(NULL, yo, str);
+        *end = ',';
+        if (rc) {
+            return -1;
+        }
+    }
+    /* Process single/last debug argument. */
+    if (cmd_debug_exec(NULL, yo, str)) {
+        return -1;
+    }
+    /* All debug arguments are valid, so they can apply. */
+    if (cmd_debug_fin(NULL, yo)) {
+        return -1;
+    }
+
+    return 0;
+}
+
+#endif
+
 /**
  * @brief Process command line options and store the settings into the context.
  *
@@ -705,33 +743,12 @@
             break;
 
 #ifndef NDEBUG
-        case 'G': { /* --debug */
-            uint32_t dbg_groups = 0;
-            const char *ptr = optarg;
-
-            while (ptr[0]) {
-                if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
-                    dbg_groups |= LY_LDGDICT;
-                    ptr += sizeof "dict" - 1;
-                } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
-                    dbg_groups |= LY_LDGXPATH;
-                    ptr += sizeof "xpath" - 1;
-                } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
-                    dbg_groups |= LY_LDGDEPSETS;
-                    ptr += sizeof "dep-sets" - 1;
-                }
-
-                if (ptr[0]) {
-                    if (ptr[0] != ',') {
-                        YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
-                        return -1;
-                    }
-                    ++ptr;
-                }
+        case 'G':   /* --debug */
+            if (set_debug_groups(optarg, yo)) {
+                return -1;
             }
-            ly_log_dbg_groups(dbg_groups);
             break;
-        } /* case 'G' */
+            /* case 'G' */
 #endif
         default:
             YLMSG_E("Invalid option or missing argument: -%c\n", optopt);