-
Notifications
You must be signed in to change notification settings - Fork 76
Add alias expand to support alias command #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add alias expand to support alias command #481
Conversation
technic960183
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xuanweishan Thanks for your contribution and sorry for the late reply.
I have investigated an edge case where the expanded of the alias itself contains spaces or quotes. (e.g. alias python="'/home/ymhsu/undergrad'\''s research/python'", where there is a folder named undergrad's research in the path.)
The solution is to store the command in an array and use eval to parse the string with complex quoting (without starting a new interactive bash).
I have proposed the modifications (7 lines in total).
Besides the 4 lines in the comments, we still need to replace another 3 lines:
all_options=$(${configure_command} --autocomplete_info=all) with all_options=$(eval "${configure_command[@]}" --autocomplete_info=all)
and 2 lines similarly for sub_options=.
Tests done
With alias python="'/home/ymhsu/undergrad'\''s research/python'":
python configure.py --[tab][tab]python configure.py --m[tab][tab]-> print out--machine= --max_patch= --mhd= --model= --mpi=python configure.py --mod[tab]-> complete to--model=python configure.py --model=[tab][tab]-> print outELBDM HYDRO PAR_ONLYpython configure.py --model=H[tab]-> complete to--model=HYDRO
And the same combination forpython3,./configure.py, and withalias python="'/home/ymhsu/under\"grad'\''s research/python'"for a path withunder"grad's research.
I hope this solution has enough completeness.
tool/config/config_autocomplete.sh
Outdated
| configure_filename=${COMP_WORDS[1]} | ||
| configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}" | ||
| local alias_expanded | ||
| alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to rename alias_expanded to alias_def, since the expansion catching will be move to the line with eval using #alias ${COMP_WORDS[0]}=.
| alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/") | |
| alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null) |
tool/config/config_autocomplete.sh
Outdated
| local alias_expanded | ||
| alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/") | ||
| if [[ -n "$alias_expanded" ]]; then | ||
| configure_command="${alias_expanded} ${configure_filename}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| configure_command="${alias_expanded} ${configure_filename}" | |
| eval "configure_command=( ${alias_expanded#alias ${COMP_WORDS[0]}=} ${configure_filename} )" |
tool/config/config_autocomplete.sh
Outdated
| if [[ -n "$alias_expanded" ]]; then | ||
| configure_command="${alias_expanded} ${configure_filename}" | ||
| else | ||
| configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an array to store configure_command.
| configure_command="${COMP_WORDS[0]} ${COMP_WORDS[1]}" | |
| configure_command=("${COMP_WORDS[0]}" "${COMP_WORDS[1]}") |
tool/config/config_autocomplete.sh
Outdated
| fi | ||
| else | ||
| configure_filename=${COMP_WORDS[0]} | ||
| configure_command="${COMP_WORDS[0]}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an array to store configure_command.
| configure_command="${COMP_WORDS[0]}" | |
| configure_command=("${COMP_WORDS[0]}") |
|
@technic960183 |
Bug fix #424
Description
This pull request fix #424. Make user able to use autocomplete with python
aliassettingChanges
alias_expandedin/tool/config/config_autocomplete.shwith definition:alias_expanded=$(alias -- "${COMP_WORDS[0]}" 2>/dev/null | sed -E "s/^alias ${COMP_WORDS[0]}='([^']+)'/\1/")configure_commandalias_expandedis not empty:configure_command=${alias_expanded} ${COMP_WORDS[1]}alias_expandedis empty:configure_command=${COMP_WORDS[0] ${COMP_WORDS[1]}}Tests
./configure [tab] [tab]python configure [tab] [tab](w/aliassetting: `alias python="python3")python3 configure [tab] [tab]