Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
From: Ronan Pigott <rpigott314@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Ronan Pigott <rpigott@berkeley.edu>
Subject: [PATCH BlueZ v2 2/4] shared/shell: add --zsh-complete option
Date: Fri, 16 Aug 2019 23:34:50 -0700
Message-ID: <20190817063452.23273-3-rpigott@berkeley.edu> (raw)
In-Reply-To: <20190817063452.23273-1-rpigott@berkeley.edu>

From: Ronan Pigott <rpigott@berkeley.edu>

This adds a new long form option --zsh-complete to provide all available
commands in an output format suitable for parsing by zsh or other shell
completion scripts.

Invoke like: `bluetoothctl --zsh-complete help`

There is no corresponding short form option.

---
 src/shared/shell.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index eac654f40..bbf9f9e7a 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -79,6 +79,7 @@ static struct {
 	int argc;
 	char **argv;
 	bool mode;
+	bool zsh;
 	bool monitor;
 	int timeout;
 	struct io *input;
@@ -98,6 +99,7 @@ static struct {
 } data;
 
 static void shell_print_menu(void);
+static void shell_print_menu_zsh_complete(void);
 
 static void cmd_version(int argc, char *argv[])
 {
@@ -288,6 +290,11 @@ static void shell_print_menu(void)
 	if (!data.menu)
 		return;
 
+	if (data.zsh) {
+		shell_print_menu_zsh_complete();
+		return;
+	}
+
 	print_text(COLOR_HIGHLIGHT, "Menu %s:", data.menu->name);
 	print_text(COLOR_HIGHLIGHT, "Available commands:");
 	print_text(COLOR_HIGHLIGHT, "-------------------");
@@ -314,6 +321,22 @@ static void shell_print_menu(void)
 	}
 }
 
+static void shell_print_menu_zsh_complete(void)
+{
+	const struct bt_shell_menu_entry *entry;
+
+	for (entry = data.menu->entries; entry->cmd; entry++) {
+		printf("%s:%s\n", entry->cmd, entry->desc ? : "");
+	}
+
+	for (entry = default_menu; entry->cmd; entry++) {
+		if (entry->exists && !entry->exists(data.menu))
+			continue;
+
+		printf("%s:%s\n", entry->cmd, entry->desc ? : "");
+	}
+}
+
 static int parse_args(char *arg, wordexp_t *w, char *del, int flags)
 {
 	char *str;
@@ -1015,6 +1038,7 @@ static const struct option main_options[] = {
 	{ "help",	no_argument, 0, 'h' },
 	{ "timeout",	required_argument, 0, 't' },
 	{ "monitor",	no_argument, 0, 'm' },
+	{ "zsh-complete",	no_argument, 0, 'z' },
 };
 
 static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
@@ -1075,6 +1099,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 		case 't':
 			data.timeout = atoi(optarg);
 			break;
+		case 'z':
+			data.zsh = 1;
+			break;
 		case 'm':
 			data.monitor = true;
 			if (bt_log_open() < 0) {
-- 
2.22.1


  parent reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-17  6:34 [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Ronan Pigott
2019-08-17  6:34 ` [PATCH BlueZ v2 1/4] client/main: add help option for available args Ronan Pigott
2019-08-17  6:34 ` Ronan Pigott [this message]
2019-08-17  6:34 ` [PATCH BlueZ v2 3/4] completion: add bluetoothctl zsh completions Ronan Pigott
2019-08-18  0:57   ` Ronan Pigott
2019-08-17  6:34 ` [PATCH BlueZ v2 4/4] build: install " Ronan Pigott
2019-09-06  8:13 ` [PATCH BlueZ v2 0/4] Add zsh completions for bluetoothctl Luiz Augusto von Dentz

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190817063452.23273-3-rpigott@berkeley.edu \
    --to=rpigott314@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=rpigott@berkeley.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git