otherwise from standard input. Empty lines are ignored.
.PP
.SH OPTIONS
+.IP "\fB\-J, \-\-json\fP"
+Use JSON output format to print the table, the option
+\fB\-\-table\-colnames\fP is required and the option \fB\-\-table\-name\fP is recommended.
.IP "\fB\-c, \-\-output\-width\fP \fIwidth\fP"
Output is formatted to a width specified as number of characters. The original
name of this option is --columns; this name deprecated since v2.30.
.IP "\fB\-N, \-\-table-colnames\fP \fInames\fP"
Specify the columns names by comma separated list of names. The names are used
for the table header.
+.IP "\fB\-n, \-\-table-name\fP \fIname\fP"
+Specify the table name used for JSON output. The defaout is "table".
.IP "\fB\-x, \-\-fillrows\fP"
Fill rows before filling columns.
.IP "\fB\-V\fR, \fB\-\-version\fR"
struct libscols_table *tab;
char **tab_colnames; /* parsed -H option */
+ const char *tab_name;
wchar_t *input_separator;
const char *output_separator;
size_t nents; /* number of entries */
size_t maxlength; /* longest input record (line) */
- unsigned int greedy :1;
+ unsigned int greedy :1,
+ json :1;
};
static size_t width(const wchar_t *str)
err(EXIT_FAILURE, _("failed to allocate output table"));
scols_table_set_column_separator(ctl->tab, ctl->output_separator);
-
+ if (ctl->json) {
+ scols_table_enable_json(ctl->tab, 1);
+ scols_table_set_name(ctl->tab, ctl->tab_name ? : "table");
+ }
if (ctl->tab_colnames) {
char **name;
fputs(_("Columnate lists.\n"), out);
fputs(USAGE_OPTIONS, out);
+ fputs(_(" -J, --json use JSON output format for table\n"), out);
fputs(_(" -t, --table create a table\n"), out);
fputs(_(" -N, --table-colnames <names> comma separated columns names\n"), out);
+ fputs(_(" -n, --table-name <name> table name for JSON output\n"), out);
fputs(_(" -s, --separator <string> possible table delimiters\n"), out);
fputs(_(" -o, --output-separator <string> columns separator for table output\n"
" (default is two spaces)\n"), out);
static const struct option longopts[] =
{
{ "columns", required_argument, NULL, 'c' }, /* deprecated */
+ { "json", no_argument, NULL, 'J' },
{ "fillrows", no_argument, NULL, 'x' },
{ "help", no_argument, NULL, 'h' },
{ "output-separator", required_argument, NULL, 'o' },
{ "separator", required_argument, NULL, 's' },
{ "table", no_argument, NULL, 't' },
{ "table-colnames", required_argument, NULL, 'N' },
+ { "table-name", required_argument, NULL, 'n' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
ctl.output_separator = " ";
ctl.input_separator = mbs_to_wcs("\t ");
- while ((ch = getopt_long(argc, argv, "hVc:N:s:txo:", longopts, NULL)) != -1)
+ while ((ch = getopt_long(argc, argv, "hVc:Jn:N:s:txo:", longopts, NULL)) != -1)
switch(ch) {
+ case 'J':
+ ctl.json = 1;
+ ctl.mode = COLUMN_MODE_TABLE;
+ break;
+ case 'n':
+ ctl.tab_name = optarg;
+ break;
case 'N':
parse_table_colnames(&ctl, optarg);
break;
argc -= optind;
argv += optind;
+ if (ctl.tab_colnames == NULL && ctl.json)
+ errx(EXIT_FAILURE, _("option --table-colnames required for --json"));
+
if (!*argv)
eval += read_input(&ctl, stdin);
else