@@ -1647,36 +1647,120 @@ def bash(self,):
1647
1647
cur=${COMP_WORDS[COMP_CWORD]}
1648
1648
prev=${COMP_WORDS[COMP_CWORD-1]}
1649
1649
1650
- COMP_WORDS_AS_STRING=$(IFS=. ; echo "$ {COMP_WORDS[*]}")
1650
+ MODULE=$ {COMP_WORDS[1]}
1651
1651
1652
- # if the last parameter was --*file
1653
- if echo "${prev}" | grep -q '\-\-.*file$'
1654
- then
1655
- _filedir;
1656
- return
1657
- fi
1652
+ COMP_WORDS_AS_STRING=$(IFS=. ; echo "${COMP_WORDS[*]}")
1658
1653
1654
+ # if last input was > for redirecting to a file
1655
+ # perform file and directory autocompletion
1659
1656
if echo "${prev}" | grep -q '>'
1660
1657
then
1661
1658
_filedir;
1662
1659
return
1663
1660
fi
1664
1661
1662
+ # from: https://stackoverflow.com/a/58221008/1033422
1663
+
1664
+ declare -A MODULE_COLUMNS=(
1665
+ {% for module_name in modules %}
1666
+ {% set column_names = " ".join(columns[module_name]) %}
1667
+ ['{{module_name}}']="{{column_names}}"
1668
+ {% endfor %}
1669
+ )
1670
+
1665
1671
{% raw %}
1666
- for (( idx=${#COMP_WORDS[@]}-1 ; idx>=0 ; idx-- )) ; do
1667
- item="${COMP_WORDS[idx]}"
1668
- if [[ "${item:0:2}" == "--" ]]; then
1669
- if [[ "${item}" == "--columns" ]]
1672
+ # list has uniform behaviour as it is implemented in BaseProxy
1673
+ if [[ "${COMP_WORDS[2]}" == "list" ]];
1674
+ then
1675
+
1676
+ # if 'list' was the last word
1677
+ if [[ "${prev}" == "list" ]];
1678
+ then
1679
+ COMPREPLY=( $(compgen -W "--columns --query" -- $cur) )
1680
+ return
1681
+ fi
1682
+
1683
+ # FIXME: https://unix.stackexchange.com/questions/124539/bash-completion-for-comma-separated-values
1684
+
1685
+ # '--columns' was the last word and user is entering column names
1686
+ if [[ "${COMP_WORDS[3]}" == "--columns"* && ${#COMP_WORDS[@]} -le 5 ]];
1687
+ then
1688
+ declare -a COLUMNS=(${MODULE_COLUMNS[$MODULE]})
1689
+
1690
+ local realcur prefix
1691
+ realcur=${cur##*,} # everything after the last comma, e.g. a,b,c,d -> d
1692
+ prefix=${cur%,*} # everything before the lat comma, e.g. a,b,c,d -> a,b,c
1693
+
1694
+ if [[ "$cur" == *,* ]];
1695
+ then
1696
+ IFS=',' ENTERED_COLUMNS_LIST=($prefix)
1697
+ unset IFS
1698
+ else
1699
+ IFS=',' ENTERED_COLUMNS_LIST=($prev)
1700
+ unset IFS
1701
+ fi
1702
+
1703
+ for COLUMN in ${COLUMNS[@]}; do
1704
+ for ENTERED_COLUMN in ${ENTERED_COLUMNS_LIST[@]}; do
1705
+ if [[ "${ENTERED_COLUMN}" == "${COLUMN}" ]]
1706
+ then
1707
+ # remove columns already entered by user
1708
+ COLUMNS=(${COLUMNS[*]//$ENTERED_COLUMN/})
1709
+ fi
1710
+ done
1711
+ done
1712
+
1713
+ if [[ "$cur" == *,* ]];
1670
1714
then
1671
- LAST_PARAM_IS_COLUMNS=1
1715
+ COMPREPLY=( $(compgen -W "${COLUMNS[*]}" -P "${prefix}," -S "," -- ${realcur}) )
1716
+ compopt -o nospace
1717
+ return
1672
1718
else
1673
- LAST_PARAM_IS_COLUMNS=0
1719
+ COMPREPLY=( $(compgen -W "${COLUMNS[*]}" -S "," -- ${realcur}) )
1720
+ compopt -o nospace
1721
+ return
1674
1722
fi
1675
- break
1676
1723
fi
1677
- done
1724
+
1725
+ # user has finished entering column list or query
1726
+ if [[ ${#COMP_WORDS[@]} == 6 ]];
1727
+ then
1728
+ COMPREPLY=( $(compgen -W "--output" -- $cur) )
1729
+ return
1730
+ fi
1731
+
1732
+ if [[ "${COMP_WORDS[5]}" == "--output"* ]];
1733
+ then
1734
+ if [[ "${COMP_WORDS[3]}" == "--columns"* ]];
1735
+ then
1736
+ COMPREPLY=( $(compgen -W "table text" -- $cur) )
1737
+ return
1738
+ else
1739
+ COMPREPLY=( $(compgen -W "json json-pp text" -- $cur) )
1740
+ return
1741
+ fi
1742
+ fi
1743
+
1744
+ return
1745
+ fi
1678
1746
{% endraw %}
1679
1747
1748
+ # if the last parameter was --*file perform
1749
+ # file and directory autocompletion
1750
+ if echo "${prev}" | grep -q '\-\-.*file$'
1751
+ then
1752
+ _filedir;
1753
+ return
1754
+ fi
1755
+
1756
+ # if last input was > for redirecting to a file
1757
+ # perform file and directory autocompletion
1758
+ if echo "${prev}" | grep -q '>'
1759
+ then
1760
+ _filedir;
1761
+ return
1762
+ fi
1763
+
1680
1764
case "$COMP_WORDS_AS_STRING" in
1681
1765
1682
1766
{% set module_names = " ".join(modules.keys()) %}
@@ -1685,27 +1769,7 @@ def bash(self,):
1685
1769
{% for function_name in modules[module_name] %}
1686
1770
{% set param_names = " ".join(modules[module_name][function_name]).replace('_', '-') %}
1687
1771
{% if function_name == "list" %}
1688
- *"hpecp.{{module_name}}.{{function_name.replace('_', '-')}}."*)
1689
- PARAM_NAMES="{{param_names}}"
1690
- for PARAM in ${PARAM_NAMES[@]}; do
1691
- PARAM="${PARAM//'\' }"
1692
- for WORD in ${COMP_WORDS[@]}; do
1693
- if [[ "${WORD}" == "${PARAM}" ]]
1694
- then
1695
- # remove parameters already entered by user
1696
- PARAM_NAMES=${PARAM_NAMES//$WORD/}
1697
- fi
1698
- done
1699
- done
1700
- if [[ $LAST_PARAM_IS_COLUMNS == 1 ]]
1701
- then
1702
- {% set column_names = " ".join(columns[module_name]) %}
1703
- COMPREPLY=( $(compgen -W "{{column_names}}" -- $cur) )
1704
- COMPREPLY+=( $(compgen -W "$PARAM_NAMES" -- $cur) )
1705
- else
1706
- COMPREPLY=( $(compgen -W "$PARAM_NAMES" -- $cur) )
1707
- fi
1708
- ;;
1772
+ # do nothing - already handled above
1709
1773
{% else %}
1710
1774
*"hpecp.{{module_name}}.{{function_name.replace('_', '-')}}."*)
1711
1775
PARAM_NAMES="{{param_names}}"
0 commit comments