Skip to content

Instantly share code, notes, and snippets.

@unique1984
Last active November 16, 2023 01:09
Show Gist options
  • Save unique1984/3d8ebc8c7b9b33162bb6dd7801be6128 to your computer and use it in GitHub Desktop.
Save unique1984/3d8ebc8c7b9b33162bb6dd7801be6128 to your computer and use it in GitHub Desktop.
Symfony Cli bash-completion
_symfony()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local cmd="${COMP_WORDS[0]}"
if ($cmd > /dev/null 2>&1)
then
COMPREPLY=($(compgen -W "$($cmd --help --no-ansi | grep -v 'Available\|Global\|Usage:\|Runs\|Environment' | grep ':' | awk '{print $1}' | grep -Poi '^\K[\w:-]+') new serve server:stop security:check composer console login link projects envs env:create tunnel:open ssh deploy domains vars user:add help" -- $cur))
fi
}
complete -F _symfony symfony
@unique1984
Copy link
Author

unique1984 commented Jun 5, 2023

I don't know MacOs is using "bash" but yes you can use compgen using static list.

compgen is a GNU tool to create programmable completion of a string GNU Compgen

symfony --help --no-ansi | grep -v 'Available\|Global\|Usage:' | grep ':' | awk '{print $1}' | grep -Poi '^\K[\w-:]+'

In this command i am getting the command output of "symfony --help" and parsing it using GNU grep.

As i am using an output of a command (a list of argument, string) it means that i can use an output of a string saved in a document.

for example save your string as a file named symfony_commands.txt and use compgen with it.

symfony --help --no-ansi ... = cat symfony_commands.txt

It is the same. Actually, you should look what type of terminal you are using and revise this code after that. This one works best under bash.

This kind a work on Mac OSX with lots of error before compliting command. Probably due to following 2 things:

1. `grep -v 'Available\|Global\|Usage:'`

2. `grep -Poi '^\K[\w-:]+'`

Is there way to make autocomplete with static list of all commands I quickly extracted?

book

...

@unique1984
Copy link
Author

Found the problems!

  1. grep -v 'Available\|Global\|Usage:' --> grep -v 'Available\|Global\|Usage:\|Runs\|Environment'
  2. grep -Poi '^\K[\w-:]+' --> grep -Poi '^\K[\w:-]+'

for 2nd part -> stackoverflow grep -

This kind a work on Mac OSX with lots of error before compliting command. Probably due to following 2 things:

1. `grep -v 'Available\|Global\|Usage:'`

2. `grep -Poi '^\K[\w-:]+'`

Is there way to make autocomplete with static list of all commands I quickly extracted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment