column: add --json
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
11a1092ad6
commit
9dbe8e1c02
|
@ -45,6 +45,9 @@ are filled before rows. Input is taken from \fIfile\fR, or
|
|||
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.
|
||||
|
@ -60,6 +63,8 @@ Table output is useful for pretty-printing.
|
|||
.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"
|
||||
|
|
|
@ -70,6 +70,7 @@ struct column_control {
|
|||
struct libscols_table *tab;
|
||||
|
||||
char **tab_colnames; /* parsed -H option */
|
||||
const char *tab_name;
|
||||
|
||||
wchar_t *input_separator;
|
||||
const char *output_separator;
|
||||
|
@ -78,7 +79,8 @@ struct column_control {
|
|||
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)
|
||||
|
@ -182,7 +184,10 @@ static void init_table(struct column_control *ctl)
|
|||
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;
|
||||
|
||||
|
@ -359,8 +364,10 @@ static void __attribute__((__noreturn__)) usage(int rc)
|
|||
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);
|
||||
|
@ -387,6 +394,7 @@ int main(int argc, char **argv)
|
|||
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' },
|
||||
|
@ -394,6 +402,7 @@ int main(int argc, char **argv)
|
|||
{ "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 },
|
||||
};
|
||||
|
@ -407,8 +416,15 @@ int main(int argc, char **argv)
|
|||
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;
|
||||
|
@ -441,6 +457,9 @@ int main(int argc, char **argv)
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue