0

I have bash function directory located at /git/function This is structure:

/git/function/
├── delete
│ ├── delete.dir
│ └── delete.file
├── main.sh
├── get
│├── get.dir.sh
│└── get.file.sh

How to use main.sh

usage: get + space + TAB(to autocomplete from /git/function/get/ directory)
output:
dir file
Next: when I select select get file it runs script from /git/function/get/get.file.sh

#!/bin/bash

# Directory where the functions are located
functiondir="/git/function"

# List of supported commands:
commands=(
  "get"
  "delete"
)

# Function to execute the 'get' command
get() {
  echo "Running $functiondir/get/get.$1.sh"
}

# Function to execute the 'delete' command
delete() {
  echo "Running $functiondir/delete/delete.$1.sh"
}

# Autocompletion function for command names
_completion() {
  # Find all files in the specified function directory with the given command name
  local function_files=$(find "$functiondir/$1" -maxdepth 1 -type f -name "$1.*.sh" -printf '%f\n' | sed "s/^$1\.//;s/\.sh$//")
  
  # Generate autocompletion options based on the found files
  COMPREPLY=($(compgen -W "$function_files" -- "${COMP_WORDS[COMP_CWORD]}"))
}

# Set autocompletion for 'get' and 'delete' commands using the _completion function
complete -F _completion -o filenames "${commands[@]}"

I have couple questions:

  • main.sh is sourced from .bashrc
  • I want to use . instead of space for command completion,
  • I need dynamically generate content from sub-dir /git/function/ without adding additional functions to main.sh
  • the fist part of the command for example: if I type get.TAB (I should check if sub-dir exist and if true then generate content of this directory /git/function/get as autocomplete)
  • if I type delete.TAB (I should check if sub-dir exist and if true then generate content of this directory /git/function/delete as autocomplete)

If you know better solution to dynamically load custom functions I will appreciate if you can share some tips

3
  • I am struggling to parse you question. I have tried to improve it, but then realise that I misinterpreted it. So tried again and again. Re-write so that it can be groked with just one reading. Jun 3, 2023 at 17:55
  • How are the scripts to be started? If the completion creates e.g. delete.dir as a command word, how does the shell find this command? Are the directories in the $PATH? Would it be an option to create an alias for each of them? Jun 4, 2023 at 1:09
  • so main.sh is sourced from .bashrc. Than in command line you should be able to type get or delete than space and TAB for autocomplete from the mapped folder assigned to the given VERB Jun 4, 2023 at 6:39

1 Answer 1

0

The programmable completion character can easily be changed:

bind '".":complete'

I don't think that this is the way to go here, though.

I guess the better approach is to get all executable files from the hierarchy (or just certain directories like get and delete) and use this file list as result set for complete -E # ... and complete -I # ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .