yanglint REFACTOR print (sub)modules in cmd_print
diff --git a/tools/lint/cmd_print.c b/tools/lint/cmd_print.c
index 3c3b0af..a89ac1a 100644
--- a/tools/lint/cmd_print.c
+++ b/tools/lint/cmd_print.c
@@ -145,6 +145,11 @@
         yo->out_stdout = 1;
     }
 
+    if (yo->schema_out_format == LYS_OUT_TREE) {
+        /* print tree from lysc_nodes */
+        yo->ctx_options |= LY_CTX_SET_PRIV_PARSED;
+    }
+
     return 0;
 }
 
@@ -162,6 +167,14 @@
             lys_print_submodule(out, submodule, format, line_length, options) :
             LY_ENOTFOUND;
 
+    if (!erc) {
+        return 0;
+    } else if ((erc == LY_ENOTFOUND) && revision) {
+        YLMSG_E("No submodule \"%s\" found.\n", name);
+    } else {
+        YLMSG_E("Unable to print submodule %s.\n", name);
+    }
+
     return erc;
 }
 
@@ -179,6 +192,14 @@
             lys_print_module(out, module, format, line_length, options) :
             LY_ENOTFOUND;
 
+    if (!erc) {
+        return 0;
+    } else if ((erc == LY_ENOTFOUND) && revision) {
+        YLMSG_E("No module \"%s\" found.\n", name);
+    } else {
+        YLMSG_E("Unable to print module %s.\n", name);
+    }
+
     return erc;
 }
 
@@ -186,10 +207,8 @@
 cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
         size_t line_length, uint32_t options)
 {
-    int rc = 0;
     LY_ERR erc;
     char *name = NULL, *revision;
-    ly_bool search_submodul;
 
     name = strdup(posv);
     /* get revision */
@@ -202,32 +221,11 @@
     erc = print_module(out, ctx, name, revision, format, line_length, options);
 
     if (erc == LY_ENOTFOUND) {
-        search_submodul = 1;
         erc = print_submodule(out, ctx, name, revision, format, line_length, options);
-    } else {
-        search_submodul = 0;
-    }
-
-    if (erc == LY_SUCCESS) {
-        rc = 0;
-    } else if (erc == LY_ENOTFOUND) {
-        if (revision) {
-            YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
-        } else {
-            YLMSG_E("No (sub)module \"%s\" found.\n", name);
-        }
-        rc = 1;
-    } else {
-        if (search_submodul) {
-            YLMSG_E("Unable to print submodule %s.\n", name);
-        } else {
-            YLMSG_E("Unable to print module %s.\n", name);
-        }
-        rc = 1;
     }
 
     free(name);
-    return rc;
+    return erc;
 }
 
 /**
@@ -280,13 +278,16 @@
 {
     int rc = 0;
 
-    if (yo->schema_out_format == LYS_OUT_TREE) {
+    if (yo->ctx_options & LY_CTX_SET_PRIV_PARSED) {
         /* print tree from lysc_nodes */
         ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
     }
 
     if (yo->schema_node_path) {
         rc = print_node(*ctx, yo);
+    } else if (!yo->interactive && yo->submodule) {
+        rc = print_submodule(yo->out, ctx, yo->submodule, NULL, yo->schema_out_format, yo->line_length,
+                yo->schema_print_options);
     } else {
         rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
         if (!yo->last_one && (yo->schema_out_format == LYS_OUT_TREE)) {
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index e9b6738..10ad792 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -684,11 +684,6 @@
         return -1;
     }
 
-    if (yo->schema_out_format == LYS_OUT_TREE) {
-        /* print tree from lysc_nodes */
-        yo->ctx_options |= LY_CTX_SET_PRIV_PARSED;
-    }
-
     /* Create the libyang context. */
     if (create_ly_context(yo->yang_lib_file, yo->searchpaths, &yo->schema_features, &yo->ctx_options, ctx)) {
         return -1;
@@ -748,58 +743,40 @@
         /* print the list of schemas */
         ret = cmd_list_exec(&ctx, &yo, NULL);
         goto cleanup;
-    } else {
-        if (yo.feature_param_format) {
-            for (u = 0; u < yo.schema_modules.count; u++) {
-                if ((ret = cmd_feature_exec(&ctx, &yo, ((struct lys_module *)yo.schema_modules.objs[u])->name))) {
-                    goto cleanup;
-                }
-            }
-            if ((ret = cmd_feature_fin(ctx, &yo))) {
+    }
+    if (yo.feature_param_format) {
+        for (u = 0; u < yo.schema_modules.count; u++) {
+            if ((ret = cmd_feature_exec(&ctx, &yo, ((struct lys_module *)yo.schema_modules.objs[u])->name))) {
                 goto cleanup;
             }
-        } else if (yo.schema_out_format) {
-            if (yo.schema_node_path) {
-                if ((ret = cmd_print_exec(&ctx, &yo, NULL))) {
-                    goto cleanup;
-                }
-            } else if (yo.submodule) {
-                const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(ctx, yo.submodule);
-
-                if (!submod) {
-                    YLMSG_E("Unable to find submodule %s.\n", yo.submodule);
-                    goto cleanup;
-                }
-
-                ret = lys_print_submodule(yo.out, submod, yo.schema_out_format, yo.line_length, yo.schema_print_options);
-                if (ret) {
-                    YLMSG_E("Unable to print submodule %s.\n", submod->name);
-                    goto cleanup;
-                }
-            } else {
-                for (u = 0; u < yo.schema_modules.count; ++u) {
-                    ret = lys_print_module(yo.out, (struct lys_module *)yo.schema_modules.objs[u], yo.schema_out_format,
-                            yo.line_length, yo.schema_print_options);
-                    /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
-                    if ((yo.schema_out_format == LYS_OUT_TREE) && (u + 1 < yo.schema_modules.count)) {
-                        ly_print(yo.out, "\n");
-                    }
-                    if (ret) {
-                        YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)yo.schema_modules.objs[u])->name);
-                        goto cleanup;
-                    }
-                }
-            }
         }
-
-        /* do the data validation despite the schema was printed */
-        if (yo.data_inputs.size) {
-            if ((ret = cmd_data_fin(ctx, &yo))) {
+        if ((ret = cmd_feature_fin(ctx, &yo))) {
+            goto cleanup;
+        }
+    } else if (yo.schema_out_format && yo.schema_node_path) {
+        if ((ret = cmd_print_exec(&ctx, &yo, NULL))) {
+            goto cleanup;
+        }
+    } else if (yo.schema_out_format && yo.submodule) {
+        if ((ret = cmd_print_exec(&ctx, &yo, yo.submodule))) {
+            goto cleanup;
+        }
+    } else if (yo.schema_out_format) {
+        for (u = 0; u < yo.schema_modules.count; ++u) {
+            yo.last_one = (u + 1) == yo.schema_modules.count;
+            if ((ret = cmd_print_exec(&ctx, &yo, ((struct lys_module *)yo.schema_modules.objs[u])->name))) {
                 goto cleanup;
             }
         }
     }
 
+    /* do the data validation despite the schema was printed */
+    if (yo.data_inputs.size) {
+        if ((ret = cmd_data_fin(ctx, &yo))) {
+            goto cleanup;
+        }
+    }
+
 cleanup:
     /* cleanup */
     yl_opt_erase(&yo);