On Mon, 18 Sept 2023 at 15:56, Jean Delvare <jdelv...@suse.de> wrote:

> Hi Ville,
>
> On Sat, 22 Jul 2023 22:55:31 +0300, Ville Skyttä wrote:
> > Offer only long options, but recognize short ones, too, per rationale at
> >
> https://github.com/scop/bash-completion/blob/4d0bffb791c34c96114aeb2e4f6726b80aa8698e/CONTRIBUTING.md?plain=1#L136-L153
> >
> > Signed-off-by: Ville Skyttä <ville.sky...@iki.fi>
> > ---
> >  Makefile        | 14 ++++++--
> >  biosdecode.bash | 40 +++++++++++++++++++++
> >  dmidecode.bash  | 95 +++++++++++++++++++++++++++++++++++++++++++++++++
> >  ownership.bash  | 33 +++++++++++++++++
> >  vpddecode.bash  | 45 +++++++++++++++++++++++
>
> Please create a directory for completion files, the root directory of
> the project is already cluttered enough as is.
>

Done.

I'm attaching the revised changeset as a single patch attachment here, hope
that's ok (my git/send-email-fu isn't up to dealing with followups
properly). This new one includes also scraping of --string and --type
values as discussed in another message.

> +compdir = $(shell pkg-config --variable=completionsdir bash-completion
> 2>/dev/null || echo $(prefix)/etc/bash_completion.d)
>
> This is going to potentially install files outside of $(prefix) by
> default, which is questionable.
>

True. Then again if the prefix or lookup dirs of bash-completion are
actually different from our $(prefix), installing in our $(prefix) will
cause the completions to not be found. I think this is the best we can do.


> > -install : install-bin install-man install-doc
> > +install : install-bin install-man install-doc install-completion
>
> I'm worried about this being enabled by default while the system may not
> even have bash installed. Can we enable this by default only if
> $(compdir) exists? This directory would typically be owned by the bash
> package.
>

I guess that'd work, done.

Ville
From 1a727665d43ae90bcff83ddc7dc1f4ed90a7d98a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@iki.fi>
Date: Fri, 21 Jul 2023 16:33:28 +0300
Subject: [PATCH] bash: add completions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Offer only long options, but recognize short ones, too, per rationale at
https://github.com/scop/bash-completion/blob/4d0bffb791c34c96114aeb2e4f6726b80aa8698e/CONTRIBUTING.md?plain=1#L136-L153

Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
---
 Makefile                   | 18 +++++++++--
 completion/biosdecode.bash | 40 +++++++++++++++++++++++
 completion/dmidecode.bash  | 66 ++++++++++++++++++++++++++++++++++++++
 completion/ownership.bash  | 33 +++++++++++++++++++
 completion/vpddecode.bash  | 43 +++++++++++++++++++++++++
 5 files changed, 198 insertions(+), 2 deletions(-)
 create mode 100644 completion/biosdecode.bash
 create mode 100644 completion/dmidecode.bash
 create mode 100644 completion/ownership.bash
 create mode 100644 completion/vpddecode.bash

diff --git a/Makefile b/Makefile
index 7aa729d..6fc946b 100644
--- a/Makefile
+++ b/Makefile
@@ -36,6 +36,7 @@ sbindir = $(prefix)/sbin
 mandir  = $(prefix)/share/man
 man8dir = $(mandir)/man8
 docdir  = $(prefix)/share/doc/dmidecode
+compdir = $(shell pkg-config --variable=completionsdir bash-completion 2>/dev/null || echo $(prefix)/etc/bash_completion.d)
 
 INSTALL         := install
 INSTALL_DATA    := $(INSTALL) -m 644
@@ -113,9 +114,9 @@ util.o : util.c types.h util.h config.h
 strip : $(PROGRAMS)
 	strip $(PROGRAMS)
 
-install : install-bin install-man install-doc
+install : install-bin install-man install-doc install-completion
 
-uninstall : uninstall-bin uninstall-man uninstall-doc
+uninstall : uninstall-bin uninstall-man uninstall-doc uninstall-completion
 
 install-bin : $(PROGRAMS)
 	$(INSTALL_DIR) $(DESTDIR)$(sbindir)
@@ -144,5 +145,18 @@ install-doc :
 uninstall-doc :
 	$(RM) -r $(DESTDIR)$(docdir)
 
+install-completion :
+	if [ -d $(compdir) ] ; then \
+	$(INSTALL_DIR) $(DESTDIR)$(compdir) ; \
+	for program in $(PROGRAMS) ; do \
+	$(INSTALL_DATA) completion/$$program.bash $(DESTDIR)$(compdir)/$$program ; done ; \
+	fi
+
+uninstall-completion :
+	if [ -d $(DESTDIR)$(compdir) ]; then \
+	for program in $(PROGRAMS) ; do \
+	$(RM) $(DESTDIR)$(compdir)/$$program ; done ; \
+	fi
+
 clean :
 	$(RM) *.o $(PROGRAMS) core
diff --git a/completion/biosdecode.bash b/completion/biosdecode.bash
new file mode 100644
index 0000000..42e0fae
--- /dev/null
+++ b/completion/biosdecode.bash
@@ -0,0 +1,40 @@
+# bash completion for biosdecode                           -*- shell-script -*-
+
+_comp_cmd_biosdecode() {
+	local cur prev
+	COMPREPLY=()
+	cur=${COMP_WORDS[COMP_CWORD]}
+	prev=${COMP_WORDS[COMP_CWORD - 1]}
+
+	case $prev in
+	-d | --dev-mem)
+		: "${cur:=/dev/}"
+		local IFS=$'\n'
+		compopt -o filenames
+		COMPREPLY=($(compgen -f -- "$cur"))
+		return 0
+		;;
+	--pir)
+		COMPREPLY=($(compgen -W '
+			full
+		' -- "$cur"))
+		return 0
+		;;
+	-[hV] | --help | --version)
+		return 0
+		;;
+	esac
+
+	if [[ $cur == -* ]]; then
+		COMPREPLY=($(compgen -W '
+			--dev-mem
+			--pir
+			--help
+			--version
+		' -- "$cur"))
+		return 0
+	fi
+
+} && complete -F _comp_cmd_biosdecode biosdecode
+
+# ex: filetype=sh
diff --git a/completion/dmidecode.bash b/completion/dmidecode.bash
new file mode 100644
index 0000000..3f08623
--- /dev/null
+++ b/completion/dmidecode.bash
@@ -0,0 +1,66 @@
+# bash completion for dmidecode                            -*- shell-script -*-
+
+_comp_cmd_dmidecode() {
+	local cur prev
+	COMPREPLY=()
+	cur=${COMP_WORDS[COMP_CWORD]}
+	prev=${COMP_WORDS[COMP_CWORD - 1]}
+
+	case $prev in
+	-d | --dev-mem | --dump-bin | --from-dump)
+		if [[ $prev == -d || $prev == --dev-mem ]]; then
+			: "${cur:=/dev/}"
+		fi
+		local IFS=$'\n'
+		compopt -o filenames
+		COMPREPLY=($(compgen -f -- "$cur"))
+		return 0
+		;;
+	-s | --string)
+		COMPREPLY=($(compgen -W '$(
+			"$1" --string 2>&1 | while IFS=\$'\\n' read -r line ; do
+				[[ $line == "  "* ]] && printf "%s\n" "$line"
+			done
+		)' -- "$cur"))
+		return 0
+		;;
+	-t | --type)
+		COMPREPLY=($(compgen -W '$(
+			"$1" --type 2>&1 | while IFS=\$'\\n' read -r line ; do
+				[[ $line == "  "* ]] && printf "%s\n" "$line"
+			done
+		)' -- "$cur"))
+		return 0
+		;;
+	--dump-bin | --from-dump)
+		local IFS=$'\n'
+		compopt -o filenames
+		COMPREPLY=($(compgen -f -- "$cur"))
+		return 0
+		;;
+	-[hVH] | --help | --version | --handle | --oem-string)
+		return 0
+		;;
+	esac
+
+	if [[ $cur == -* ]]; then
+		COMPREPLY=($(compgen -W '
+			--dev-mem
+			--help
+			--quiet
+			--string
+			--type
+			--handle
+			--dump
+			--dump-bin
+			--from-dump
+			--no-sysfs
+			--oem-string
+			--version
+		' -- "$cur"))
+		return 0
+	fi
+
+} && complete -F _comp_cmd_dmidecode dmidecode
+
+# ex: filetype=sh
diff --git a/completion/ownership.bash b/completion/ownership.bash
new file mode 100644
index 0000000..6a25d29
--- /dev/null
+++ b/completion/ownership.bash
@@ -0,0 +1,33 @@
+# bash completion for ownership                            -*- shell-script -*-
+
+_comp_cmd_ownership() {
+	local cur prev
+	COMPREPLY=()
+	cur=${COMP_WORDS[COMP_CWORD]}
+	prev=${COMP_WORDS[COMP_CWORD - 1]}
+
+	case $prev in
+	-d | --dev-mem)
+		: "${cur:=/dev/}"
+		local IFS=$'\n'
+		compopt -o filenames
+		COMPREPLY=($(compgen -f -- "$cur"))
+		return 0
+		;;
+	-[hV] | --help | --version)
+		return 0
+		;;
+	esac
+
+	if [[ $cur == -* ]]; then
+		COMPREPLY=($(compgen -W '
+			--dev-mem
+			--help
+			--version
+		' -- "$cur"))
+		return 0
+	fi
+
+} && complete -F _comp_cmd_ownership ownership
+
+# ex: filetype=sh
diff --git a/completion/vpddecode.bash b/completion/vpddecode.bash
new file mode 100644
index 0000000..0745127
--- /dev/null
+++ b/completion/vpddecode.bash
@@ -0,0 +1,43 @@
+# bash completion for vpddecode                            -*- shell-script -*-
+
+_comp_cmd_vpddecode() {
+	local cur prev
+	COMPREPLY=()
+	cur=${COMP_WORDS[COMP_CWORD]}
+	prev=${COMP_WORDS[COMP_CWORD - 1]}
+
+	case $prev in
+	-d | --dev-mem)
+		: "${cur:=/dev/}"
+		local IFS=$'\n'
+		compopt -o filenames
+		COMPREPLY=($(compgen -f -- "$cur"))
+		return 0
+		;;
+	-s | --string)
+		COMPREPLY=($(compgen -W '$(
+			"$1" --string 2>&1 | while IFS=\$'\\n' read -r line ; do
+				[[ $line == "  "* ]] && printf "%s\n" "$line"
+			done
+		' -- "$cur"))
+		return 0
+		;;
+	-[hV] | --help | --version)
+		return 0
+		;;
+	esac
+
+	if [[ $cur == -* ]]; then
+		COMPREPLY=($(compgen -W '
+			--dev-mem
+			--help
+			--string
+			--dump
+			--version
+		' -- "$cur"))
+		return 0
+	fi
+
+} && complete -F _comp_cmd_vpddecode vpddecode
+
+# ex: filetype=sh
-- 
2.25.1

Reply via email to