#include "strutils.h"
#include "closestream.h"
#include "ttyutils.h"
+#include "strv.h"
#include "libsmartcols.h"
struct libscols_table *tab;
+ char **tab_colnames; /* parsed -H option */
+
wchar_t *input_separator;
const char *output_separator;
return result;
}
+static int parse_table_colnames(struct column_control *ctl, const char *names)
+{
+ ctl->tab_colnames = strv_split(names, ",");
+ if (!ctl->tab_colnames) {
+ if (errno == ENOMEM)
+ err_oom();
+ errx(EXIT_FAILURE, _("failed to parse column names"));
+ }
+ return 0;
+}
+
static void init_table(struct column_control *ctl)
{
scols_init_debug(0);
err(EXIT_FAILURE, _("failed to allocate output table"));
scols_table_set_column_separator(ctl->tab, ctl->output_separator);
- scols_table_enable_noheadings(ctl->tab, 1);
+
+ if (ctl->tab_colnames) {
+ char **name;
+
+ STRV_FOREACH(name, ctl->tab_colnames)
+ scols_table_new_column(ctl->tab, *name, 0, 0);
+ } else
+ scols_table_enable_noheadings(ctl->tab, 1);
}
static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
fputs(USAGE_OPTIONS, out);
fputs(_(" -t, --table create a table\n"), out);
+ fputs(_(" -N, --table-colnames <names> comma separated columns names\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 */
- { "fillrows", no_argument, NULL, 'x' },
- { "help", no_argument, NULL, 'h' },
- { "output-separator", required_argument, NULL, 'o' },
- { "output-width", required_argument, NULL, 'c' },
- { "separator", required_argument, NULL, 's' },
- { "table", no_argument, NULL, 't' },
- { "version", no_argument, NULL, 'V' },
+ { "columns", required_argument, NULL, 'c' }, /* deprecated */
+ { "fillrows", no_argument, NULL, 'x' },
+ { "help", no_argument, NULL, 'h' },
+ { "output-separator", required_argument, NULL, 'o' },
+ { "output-width", required_argument, NULL, 'c' },
+ { "separator", required_argument, NULL, 's' },
+ { "table", no_argument, NULL, 't' },
+ { "table-colnames", 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:s:txo:", longopts, NULL)) != -1)
+ while ((ch = getopt_long(argc, argv, "hVc:N:s:txo:", longopts, NULL)) != -1)
switch(ch) {
+ case 'N':
+ parse_table_colnames(&ctl, optarg);
+ break;
case 'h':
usage(EXIT_SUCCESS);
break;