yanglint FEATURE new option for specific input format
So that the data files do not have to have
specific extensions. Also added support for
LYB format.
diff --git a/tools/lint/cmd_data.c b/tools/lint/cmd_data.c
index 53045b6..b73d778 100644
--- a/tools/lint/cmd_data.c
+++ b/tools/lint/cmd_data.c
@@ -31,11 +31,11 @@
void
cmd_data_help(void)
{
- printf("Usage: data [-ems] [-t TYPE]\n"
- " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
- " data [-s] -t (rpc | notif | reply) [-O FILE]\n"
- " [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
- " data [-es] [-t TYPE] -x XPATH [-o OUTFILE] <data1> ...\n"
+ printf("Usage: data [-emn] [-t TYPE]\n"
+ " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
+ " data [-n] -t (rpc | notif | reply) [-O FILE]\n"
+ " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n"
+ " data [-en] [-t TYPE] [-F FORMAT] -x XPATH [-o OUTFILE] <data1> ...\n"
" Parse, validate and optionally print data instances\n\n"
" -t TYPE, --type=TYPE\n"
@@ -75,6 +75,10 @@
" Print the data in one of the following formats:\n"
" xml, json, lyb\n"
" Note that the LYB format requires the -o option specified.\n"
+ " -F FORMAT, --in-format=FORMAT\n"
+ " Load the data in one of the following formats:\n"
+ " xml, json, lyb\n"
+ " If input format not specified, it is detected from the file extension.\n"
" -d MODE, --default=MODE\n"
" Print data with default values, according to the MODE\n"
" (to print attributes, ietf-netconf-with-defaults model\n"
@@ -106,6 +110,7 @@
{"defaults", required_argument, NULL, 'd'},
{"present", no_argument, NULL, 'e'},
{"format", required_argument, NULL, 'f'},
+ {"in-format", required_argument, NULL, 'F'},
{"help", no_argument, NULL, 'h'},
{"merge", no_argument, NULL, 'm'},
{"output", required_argument, NULL, 'o'},
@@ -122,7 +127,8 @@
uint32_t options_validate = 0;
uint8_t data_type = 0;
uint8_t data_type_set = 0;
- LYD_FORMAT format = LYD_UNKNOWN;
+ LYD_FORMAT outformat = LYD_UNKNOWN;
+ LYD_FORMAT informat = LYD_UNKNOWN;
struct ly_out *out = NULL;
struct cmdline_file *operational = NULL;
struct ly_set inputs = {0};
@@ -132,7 +138,7 @@
goto cleanup;
}
- while ((opt = getopt_long(argc, argv, "d:ef:hmo:O:r:nt:x:", options, &opt_index)) != -1) {
+ while ((opt = getopt_long(argc, argv, "d:ef:F:hmo:O:r:nt:x:", options, &opt_index)) != -1) {
switch (opt) {
case 'd': /* --default */
if (!strcasecmp(optarg, "all")) {
@@ -151,17 +157,30 @@
break;
case 'f': /* --format */
if (!strcasecmp(optarg, "xml")) {
- format = LYD_XML;
+ outformat = LYD_XML;
} else if (!strcasecmp(optarg, "json")) {
- format = LYD_JSON;
+ outformat = LYD_JSON;
} else if (!strcasecmp(optarg, "lyb")) {
- format = LYD_LYB;
+ outformat = LYD_LYB;
} else {
YLMSG_E("Unknown output format %s\n", optarg);
cmd_data_help();
goto cleanup;
}
break;
+ case 'F': /* --in-format */
+ if (!strcasecmp(optarg, "xml")) {
+ informat = LYD_XML;
+ } else if (!strcasecmp(optarg, "json")) {
+ informat = LYD_JSON;
+ } else if (!strcasecmp(optarg, "lyb")) {
+ informat = LYD_LYB;
+ } else {
+ YLMSG_E("Unknown input format %s\n", optarg);
+ cmd_data_help();
+ goto cleanup;
+ }
+ break;
case 'o': /* --output */
if (out) {
YLMSG_E("Only a single output can be specified.\n");
@@ -262,7 +281,7 @@
data_merge = 1;
}
- if (xpaths.count && format) {
+ if (xpaths.count && outformat) {
YLMSG_E("The --format option cannot be combined with --xpath option.\n");
cmd_data_help();
goto cleanup;
@@ -277,13 +296,12 @@
/* process input data files provided as standalone command line arguments */
for (int i = 0; i < argc - optind; i++) {
struct ly_in *in;
- LYD_FORMAT format = LYD_UNKNOWN;
- if (get_input(argv[optind + i], NULL, &format, &in)) {
+ if (get_input(argv[optind + i], NULL, &informat, &in)) {
goto cleanup;
}
- if (!fill_cmdline_file(&inputs, in, argv[optind + i], format)) {
+ if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) {
ly_in_free(in, 1);
goto cleanup;
}
@@ -298,7 +316,7 @@
}
/* parse, validate and print data */
- if (process_data(*ctx, data_type, data_merge, format, out,
+ if (process_data(*ctx, data_type, data_merge, outformat, out,
options_parse, options_validate, options_print,
operational, &inputs, &xpaths)) {
goto cleanup;
diff --git a/tools/lint/common.c b/tools/lint/common.c
index 968d00f..ed28665 100644
--- a/tools/lint/common.c
+++ b/tools/lint/common.c
@@ -73,9 +73,11 @@
return -1;
}
- /* get the file format */
- if (get_format(filepath, format_schema, format_data)) {
- return -1;
+ if ((format_schema && !*format_schema) || (format_data && !*format_data)) {
+ /* get the file format */
+ if (get_format(filepath, format_schema, format_data)) {
+ return -1;
+ }
}
if (ly_in_new_filepath(filepath, 0, in)) {
@@ -310,6 +312,9 @@
} else if (!strcmp(ptr, "json")) {
informat_s = 0;
informat_d = LYD_JSON;
+ } else if (!strcmp(ptr, "lyb")) {
+ informat_s = 0;
+ informat_d = LYD_LYB;
} else {
YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr);
return 0;