]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --json
authorKarel Zak <kzak@redhat.com>
Mon, 6 Mar 2017 10:58:15 +0000 (11:58 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 2 May 2017 10:18:00 +0000 (12:18 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/column.1
text-utils/column.c

index df097c37f0b7e916e4b4e27819a9928466414982..1dd1f8d99ce398f6bfbc5ccf9956bf465bc9d2d7 100644 (file)
@@ -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"
index 1d560fb72457d6403a175e197b9a358c0ed6ba37..e51ea5576c8d03edc70714730a19810974361d4a 100644 (file)
@@ -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