From: Masatake YAMATO Date: Thu, 7 Mar 2024 20:42:34 +0000 (+0900) Subject: lsns: add -H, --list-columns option X-Git-Tag: v2.42-start~457^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1467a668a0049c9efb1fa8be18a4b1fc3dbc8278;p=thirdparty%2Futil-linux.git lsns: add -H, --list-columns option Signed-off-by: Masatake YAMATO --- diff --git a/sys-utils/lsns.8.adoc b/sys-utils/lsns.8.adoc index 016772711..ec79cf0ed 100644 --- a/sys-utils/lsns.8.adoc +++ b/sys-utils/lsns.8.adoc @@ -31,6 +31,9 @@ Note that *lsns* reads information directly from the _/proc_ filesystem and for == OPTIONS +*-H*, *--list-columns*:: +List the available columns, use with *--json* or *--raw* to get output in machine-readable format. + *-J*, *--json*:: Use JSON output format. diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c index 1cd0abb45..f444f5e1a 100644 --- a/sys-utils/lsns.c +++ b/sys-utils/lsns.c @@ -22,9 +22,9 @@ #include #include #include +# include #ifdef HAVE_LINUX_NET_NAMESPACE_H -# include # include # include # include @@ -50,6 +50,7 @@ #include "namespace.h" #include "idcache.h" #include "fileutils.h" +#include "column-list-table.h" #include "debug.h" @@ -1392,7 +1393,6 @@ static struct libscols_filter *new_filter(const char *query) static void __attribute__((__noreturn__)) usage(void) { FILE *out = stdout; - size_t i; fputs(USAGE_HEADER, out); @@ -1417,17 +1417,27 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -T, --tree[=] use tree format (parent, owner, or process)\n"), out); fputs(USAGE_SEPARATOR, out); + fputs(_(" -H, --list-columns list the available columns\n"), out); fprintf(out, USAGE_HELP_OPTIONS(24)); - - fputs(USAGE_COLUMNS, out); - for (i = 0; i < ARRAY_SIZE(infos); i++) - fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); - fprintf(out, USAGE_MAN_TAIL("lsns(8)")); exit(EXIT_SUCCESS); } +static void __attribute__((__noreturn__)) list_colunms(bool raw, bool json) +{ + struct libscols_table *col_tb = xcolumn_list_table_new("lsns-columns", stdout, raw, json); + + for (size_t i = 0; i < ARRAY_SIZE(infos); i++) + xcolumn_list_table_append_line(col_tb, infos[i].name, + infos[i].json_type, NULL, + _(infos[i].help)); + + scols_print_table(col_tb); + scols_unref_table(col_tb); + + exit(EXIT_SUCCESS); +} int main(int argc, char *argv[]) { @@ -1454,6 +1464,7 @@ int main(int argc, char *argv[]) { "raw", no_argument, NULL, 'r' }, { "type", required_argument, NULL, 't' }, { "tree", optional_argument, NULL, 'T' }, + { "list-columns", no_argument, NULL, 'H' }, { NULL, 0, NULL, 0 } }; @@ -1479,7 +1490,7 @@ int main(int argc, char *argv[]) INIT_LIST_HEAD(&netnsids_cache); while ((c = getopt_long(argc, argv, - "JlPp:o:nruhVt:T::WQ:", long_opts, NULL)) != -1) { + "JlPp:o:nruhVt:T::WQ:H", long_opts, NULL)) != -1) { err_exclusive_options(c, long_opts, excl, excl_st); @@ -1542,6 +1553,8 @@ int main(int argc, char *argv[]) case 'Q': ls.filter = new_filter(optarg); break; + case 'H': + list_colunms(ls.raw, ls.json); case 'h': usage();