[lttng-dev] [PATCH] Add a '--list-options' option to each command.

Mathieu Desnoyers compudj at krystal.dyndns.org
Tue Jan 24 11:32:17 EST 2012


* Simon Marchi (simon.marchi at polymtl.ca) wrote:
> This is intended to be used for programmable Bash completion.
> 
> Modified from previous version:
> - Changed '\0' to 0 to match current coding style.
> 
> Signed-off-by: Simon Marchi <simon.marchi at polymtl.ca>

Thanks!

Acked-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>

> ---
>  src/bin/lttng/commands/add_context.c      |    6 ++++++
>  src/bin/lttng/commands/calibrate.c        |    6 ++++++
>  src/bin/lttng/commands/create.c           |   11 +++++++++--
>  src/bin/lttng/commands/destroy.c          |    6 ++++++
>  src/bin/lttng/commands/disable_channels.c |    6 ++++++
>  src/bin/lttng/commands/disable_events.c   |    6 ++++++
>  src/bin/lttng/commands/enable_channels.c  |    5 +++++
>  src/bin/lttng/commands/enable_events.c    |    6 ++++++
>  src/bin/lttng/commands/list.c             |    6 ++++++
>  src/bin/lttng/commands/set_session.c      |    6 ++++++
>  src/bin/lttng/commands/start.c            |    6 ++++++
>  src/bin/lttng/commands/stop.c             |    6 ++++++
>  src/bin/lttng/commands/version.c          |    6 ++++++
>  src/bin/lttng/utils.c                     |   25 +++++++++++++++++++++++++
>  src/bin/lttng/utils.h                     |    3 +++
>  15 files changed, 108 insertions(+), 2 deletions(-)
> 
> diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c
> index 260c300..a49046c 100644
> --- a/src/bin/lttng/commands/add_context.c
> +++ b/src/bin/lttng/commands/add_context.c
> @@ -48,6 +48,7 @@ enum {
>  	OPT_HELP = 1,
>  	OPT_TYPE,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -149,6 +150,7 @@ static struct poptOption long_options[] = {
>  	{"userspace",      'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
>  #endif
>  	{"type",           't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
> +	{"list-options",   0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -463,6 +465,10 @@ int cmd_add_context(int argc, const char **argv)
>  			opt_cmd_name = poptGetOptArg(pc);
>  #endif
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/calibrate.c b/src/bin/lttng/commands/calibrate.c
> index a0e852f..0561f4d 100644
> --- a/src/bin/lttng/commands/calibrate.c
> +++ b/src/bin/lttng/commands/calibrate.c
> @@ -48,6 +48,7 @@ enum {
>  	OPT_FUNCTION_ENTRY,
>  	OPT_SYSCALL,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -75,6 +76,7 @@ static struct poptOption long_options[] = {
>  	{"function:entry", 0,   POPT_ARG_NONE, 0, OPT_FUNCTION_ENTRY, 0, 0},
>  #endif
>  	{"syscall",        0,   POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -217,6 +219,10 @@ int cmd_calibrate(int argc, const char **argv)
>  		case OPT_USERSPACE:
>  			opt_userspace = 1;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c
> index 835d82a..8f19301 100644
> --- a/src/bin/lttng/commands/create.c
> +++ b/src/bin/lttng/commands/create.c
> @@ -27,18 +27,21 @@
>  #include <unistd.h>
>  
>  #include "../command.h"
> +#include "../utils.h"
>  
>  static char *opt_output_path;
>  static char *opt_session_name;
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
> -	{"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> -	{"output",    'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0},
> +	{"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
> +	{"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -160,6 +163,10 @@ int cmd_create(int argc, const char **argv)
>  		case OPT_HELP:
>  			usage(stderr);
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c
> index b793159..d324c7b 100644
> --- a/src/bin/lttng/commands/destroy.c
> +++ b/src/bin/lttng/commands/destroy.c
> @@ -31,11 +31,13 @@ static char *opt_session_name;
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
>  	{"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -116,6 +118,10 @@ int cmd_destroy(int argc, const char **argv)
>  		case OPT_HELP:
>  			usage(stderr);
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/disable_channels.c b/src/bin/lttng/commands/disable_channels.c
> index f8ed517..2e5fe0c 100644
> --- a/src/bin/lttng/commands/disable_channels.c
> +++ b/src/bin/lttng/commands/disable_channels.c
> @@ -40,6 +40,7 @@ static pid_t opt_pid;
>  enum {
>  	OPT_HELP = 1,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -56,6 +57,7 @@ static struct poptOption long_options[] = {
>  #else
>  	{"userspace",      'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
>  #endif
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -153,6 +155,10 @@ int cmd_disable_channels(int argc, const char **argv)
>  		case OPT_USERSPACE:
>  			opt_userspace = 1;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/disable_events.c b/src/bin/lttng/commands/disable_events.c
> index 7446168..80bae3c 100644
> --- a/src/bin/lttng/commands/disable_events.c
> +++ b/src/bin/lttng/commands/disable_events.c
> @@ -42,6 +42,7 @@ static pid_t opt_pid;
>  enum {
>  	OPT_HELP = 1,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -60,6 +61,7 @@ static struct poptOption long_options[] = {
>  #else
>  	{"userspace",      'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
>  #endif
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -211,6 +213,10 @@ int cmd_disable_events(int argc, const char **argv)
>  		case OPT_USERSPACE:
>  			opt_userspace = 1;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c
> index 5443d78..4e7f0c6 100644
> --- a/src/bin/lttng/commands/enable_channels.c
> +++ b/src/bin/lttng/commands/enable_channels.c
> @@ -48,6 +48,7 @@ enum {
>  	OPT_SWITCH_TIMER,
>  	OPT_READ_TIMER,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -70,6 +71,7 @@ static struct poptOption long_options[] = {
>  	{"num-subbuf",     0,   POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
>  	{"switch-timer",   0,   POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
>  	{"read-timer",     0,   POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
> +	{"list-options",   0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -258,6 +260,9 @@ int cmd_enable_channels(int argc, const char **argv)
>  		case OPT_USERSPACE:
>  			opt_userspace = 1;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/enable_events.c b/src/bin/lttng/commands/enable_events.c
> index c5a969f..58b3b23 100644
> --- a/src/bin/lttng/commands/enable_events.c
> +++ b/src/bin/lttng/commands/enable_events.c
> @@ -54,6 +54,7 @@ enum {
>  	OPT_SYSCALL,
>  	OPT_USERSPACE,
>  	OPT_TRACEPOINT_LOGLEVEL,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -84,6 +85,7 @@ static struct poptOption long_options[] = {
>  #endif
>  	{"syscall",        0,   POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
>  	{"loglevel",     0,     POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -432,6 +434,10 @@ int cmd_enable_events(int argc, const char **argv)
>  		case OPT_TRACEPOINT_LOGLEVEL:
>  			opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
> index f09edae..1d63e15 100644
> --- a/src/bin/lttng/commands/list.c
> +++ b/src/bin/lttng/commands/list.c
> @@ -43,6 +43,7 @@ const char *indent8 = "        ";
>  enum {
>  	OPT_HELP = 1,
>  	OPT_USERSPACE,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct lttng_handle *handle;
> @@ -60,6 +61,7 @@ static struct poptOption long_options[] = {
>  #endif
>  	{"channel",   'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
>  	{"domain",    'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -556,6 +558,10 @@ int cmd_list(int argc, const char **argv)
>  		case OPT_USERSPACE:
>  			opt_userspace = 1;
>  			break;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c
> index bc25b8d..5e23343 100644
> --- a/src/bin/lttng/commands/set_session.c
> +++ b/src/bin/lttng/commands/set_session.c
> @@ -31,11 +31,13 @@ static char *opt_session_name;
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
>  	{"help",           'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> +	{"list-options",   0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -89,6 +91,10 @@ int cmd_set_session(int argc, const char **argv)
>  			usage(stderr);
>  			ret = CMD_SUCCESS;
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/start.c b/src/bin/lttng/commands/start.c
> index 8292787..0a11c40 100644
> --- a/src/bin/lttng/commands/start.c
> +++ b/src/bin/lttng/commands/start.c
> @@ -31,11 +31,13 @@ static char *opt_session_name;
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
>  	{"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -109,6 +111,10 @@ int cmd_start(int argc, const char **argv)
>  			usage(stderr);
>  			ret = CMD_SUCCESS;
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c
> index 0a94264..1a4f8c3 100644
> --- a/src/bin/lttng/commands/stop.c
> +++ b/src/bin/lttng/commands/stop.c
> @@ -31,11 +31,13 @@ static char *opt_session_name;
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
>  	{"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -106,6 +108,10 @@ int cmd_stop(int argc, const char **argv)
>  			usage(stderr);
>  			ret = CMD_SUCCESS;
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/commands/version.c b/src/bin/lttng/commands/version.c
> index c9b547b..cc93d69 100644
> --- a/src/bin/lttng/commands/version.c
> +++ b/src/bin/lttng/commands/version.c
> @@ -30,11 +30,13 @@
>  
>  enum {
>  	OPT_HELP = 1,
> +	OPT_LIST_OPTIONS,
>  };
>  
>  static struct poptOption long_options[] = {
>  	/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
>  	{"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
> +	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
>  	{0, 0, 0, 0, 0, 0, 0}
>  };
>  
> @@ -66,6 +68,10 @@ int cmd_version(int argc, const char **argv)
>  			usage(stderr);
>  			ret = CMD_SUCCESS;
>  			goto end;
> +		case OPT_LIST_OPTIONS:
> +			list_cmd_options(stdout, long_options);
> +			ret = CMD_SUCCESS;
> +			goto end;
>  		default:
>  			usage(stderr);
>  			ret = CMD_UNDEFINED;
> diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c
> index e993f0f..635bf77 100644
> --- a/src/bin/lttng/utils.c
> +++ b/src/bin/lttng/utils.c
> @@ -17,10 +17,12 @@
>   */
>  
>  #include <stdlib.h>
> +#include <ctype.h>
>  
>  #include <common/error.h>
>  
>  #include "conf.h"
> +#include "utils.h"
>  
>  /*
>   *  get_session_name
> @@ -48,3 +50,26 @@ error:
>  	DBG("Session name found: %s", session_name);
>  	return session_name;
>  }
> +
> +
> +/*
> + * list_cmd_options
> + *
> + * Prints a simple list of the options available to a command. This is intended
> + * to be easily parsed for bash completion.
> + */
> +void list_cmd_options(FILE *ofp, struct poptOption *options)
> +{
> +	int i;
> +	struct poptOption *option = NULL;
> +
> +	for (i = 0; options[i].longName != NULL; i++) {
> +		option = &options[i];
> +
> +		fprintf(ofp, "--%s\n", option->longName);
> +
> +		if (isprint(option->shortName)) {
> +			fprintf(ofp, "-%c\n", option->shortName);
> +		}
> +	}
> +}
> diff --git a/src/bin/lttng/utils.h b/src/bin/lttng/utils.h
> index 5492d5e..e609b70 100644
> --- a/src/bin/lttng/utils.h
> +++ b/src/bin/lttng/utils.h
> @@ -19,8 +19,11 @@
>  #ifndef _LTTNG_UTILS_H
>  #define _LTTNG_UTILS_H
>  
> +#include <popt.h>
> +
>  char *get_config_file_path(void);
>  char *get_session_name(void);
>  int set_session_name(char *name);
> +void list_cmd_options(FILE *ofp, struct poptOption *options);
>  
>  #endif /* _LTTNG_UTILS_H */
> -- 
> 1.7.5.4
> 
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
> 

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com



More information about the lttng-dev mailing list