Merge lp:~bcsaller/pyjuju/bash-completion into lp:pyjuju

Proposed by Dustin Kirkland 
Status: Merged
Approved by: Kapil Thangavelu
Approved revision: 257
Merged at revision: 282
Proposed branch: lp:~bcsaller/pyjuju/bash-completion
Merge into: lp:pyjuju
Diff against target: 113 lines (+102/-0)
2 files modified
debian/install (+1/-0)
misc/bash_completion.d/ensemble (+101/-0)
To merge this branch: bzr merge lp:~bcsaller/pyjuju/bash-completion
Reviewer Review Type Date Requested Status
dann frazier (community) Approve
Gustavo Niemeyer Approve
Review via email: mp+68124@code.launchpad.net

Description of the change

I had a similar but different implementation of bash completion for Ensemble in lp:~kirkland/ensemble/bash-completion.

I'm deferring my branch in favor of Ben's, which is a bit better about supporting positional arguments. I have tested Ben's code and it works well for me. Could we get this merged into trunk so that Ensemble users can benefit from bash tab completion?

Thanks,
Dustin

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

I'm not familiar with that shell syntax, so might be nice to have a second review
from someone that actually does. Here are a few points:

[1]

=== added directory 'etc'

Can we please name this as "misc" to avoid binding the content to /etc?

[2]

+ if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then

Isn't the normal form [[ A -a B ]] instead of [[ A ]] && [[ B ]] ?

[3]

+ globalOpts=( -h --verbose -v --log-file)
(...)
+ cmdOpts=( --environment --repository )
(...)
+ cmdOpts=( --dry-run -n --environment --repository)
(...)
+ cmdOpts=(--help -h)

Spacing is very uneven. All of these entries should either
have a space on both ends, or in none.

[4]

82 + status)
83 + cmdOpts=( --output --format --environment)
84 + case $curOpt in
85 + --format) optEnums=( json yaml png svg dot )
86 + esac
87 + ;;
88 + upgrade-formula)
89 + cmdOpts=( --dry-run -n --environment --repository)
90 + ;;
91 + *)
92 + cmdOpts=(--help -h)
93 + ;;

Indentation seems broken, probably due to the mix of spaces and tabs. Tabs are probably
better as they allow the reader to choose, but they should be taken as 8 spaces in that
case. 4-space tabs are likely going to break most people's expectations (and they break
Launchpad's.. see the diff below).

[5]

84 + case $curOpt in
85 + --format) optEnums=( json yaml png svg dot )
86 + esac

case is unclosed (;;).

review: Approve
Revision history for this message
dann frazier (dannf) wrote :

I'd suggest quoting around $cur and $curOpt references to be more liberal wrt user input.

review: Approve
lp:~bcsaller/pyjuju/bash-completion updated
258. By Benjamin Saller

review changes, spacing, indentation, quoting

Revision history for this message
Benjamin Saller (bcsaller) wrote :

While I think this code is an important useability tool I don't think
the implementation needs much time or focus. It was a stop gap till we
can generate the bash script from the parser automatically in a way
similar to how `bzr` does it.

That said I made minor cleanup where they were simple and when I
encountered issues I moved on. comments below.

On Wed, Jul 20, 2011 at 7:18 AM, Gustavo Niemeyer <email address hidden> wrote:
> Review: Approve
> I'm not familiar with that shell syntax, so might be nice to have a second review
> from someone that actually does.  Here are a few points:
>
> [1]
>
> === added directory 'etc'
>
> Can we please name this as "misc" to avoid binding the content to /etc?
>

Called misc in the source tree. debian/install still maps it into
/etc/bash_completion.d/

> [2]
>
> +       if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
>
> Isn't the normal form [[ A -a B ]] instead of [[ A ]] && [[ B ]] ?
>

I didn't arrive at a form that work with complex expressions. So I
ended up leaving this as is.

> [3]
>
> +       globalOpts=( -h --verbose -v --log-file)
> (...)
> +               cmdOpts=( --environment --repository )
> (...)
> +            cmdOpts=( --dry-run -n --environment --repository)
> (...)
> +               cmdOpts=(--help -h)
>
> Spacing is very uneven.  All of these entries should either
> have a space on both ends, or in none.
>
> [4]
>
> 82      +    status)
> 83      +        cmdOpts=( --output --format --environment)
> 84      +        case $curOpt in
> 85      +            --format) optEnums=( json yaml png svg dot )
> 86      +        esac
> 87      +        ;;
> 88      +    upgrade-formula)
> 89      +            cmdOpts=( --dry-run -n --environment --repository)
> 90      +        ;;
> 91      +       *)
> 92      +               cmdOpts=(--help -h)
> 93      +               ;;
>
> Indentation seems broken, probably due to the mix of spaces and tabs.  Tabs are probably
> better as they allow the reader to choose, but they should be taken as 8 spaces in that
> case.  4-space tabs are likely going to break most people's expectations (and they break
> Launchpad's.. see the diff below).
>

Ran indent over the file.

> [5]
>
> 84      +        case $curOpt in
> 85      +            --format) optEnums=( json yaml png svg dot )
> 86      +        esac
>
> case is unclosed (;;).
>

fixed

> --
> https://code.launchpad.net/~bcsaller/ensemble/bash-completion/+merge/68124
> You are the owner of lp:~bcsaller/ensemble/bash-completion.
>

lp:~bcsaller/pyjuju/bash-completion updated
259. By Benjamin Saller

white space changes

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'debian/install'
2--- debian/install 1970-01-01 00:00:00 +0000
3+++ debian/install 2011-07-26 01:33:30 +0000
4@@ -0,0 +1,1 @@
5+misc/bash_completion.d/ensemble /etc/bash_completion.d/
6
7=== added directory 'misc'
8=== added directory 'misc/bash_completion.d'
9=== added file 'misc/bash_completion.d/ensemble'
10--- misc/bash_completion.d/ensemble 1970-01-01 00:00:00 +0000
11+++ misc/bash_completion.d/ensemble 2011-07-26 01:33:30 +0000
12@@ -0,0 +1,101 @@
13+shopt -s progcomp
14+_ensemble () {
15+ local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
16+ local curOpt optEnums
17+ local IFS=$' \n'
18+
19+ COMPREPLY=()
20+ cur=${COMP_WORDS[COMP_CWORD]}
21+ cmds='add-relation add-unit bootstrap debug-hooks debug-log deploy destroy-service expose open-tunnel remove-relation remove-unit resolved set shutdown ssh status terminate-machine unexpose upgrade-formula'
22+ globalOpts=( -h --verbose -v --log-file)
23+
24+ # do ordinary expansion if we are anywhere after a -- argument
25+ for ((i = 1; i < COMP_CWORD; ++i)); do
26+ [[ ${COMP_WORDS[i]} == "--" ]] && return 0
27+ done
28+
29+ # find the command; it's the first word not starting in -
30+ cmd=
31+ for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
32+ if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
33+ cmd=${COMP_WORDS[cmdIdx]}
34+ break
35+ fi
36+ done
37+
38+ # complete command name if we are not already past the command
39+ if [[ $COMP_CWORD -le cmdIdx ]]; then
40+ COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- "$cur" ) )
41+ return 0
42+ fi
43+
44+ # find the option for which we want to complete a value
45+ curOpt=
46+ if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
47+ curOpt=${COMP_WORDS[COMP_CWORD - 1]}
48+ if [[ "$curOpt" == = ]]; then
49+ curOpt=${COMP_WORDS[COMP_CWORD - 2]}
50+ elif [[ "$cur" == : ]]; then
51+ cur=
52+ curOpt="$curOpt:"
53+ elif [[ "$curOpt" == : ]]; then
54+ curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
55+ fi
56+ fi
57+
58+ cmdOpts=( )
59+ optEnums=( )
60+ fixedWords=( )
61+ case "$cmd" in
62+ add-relation|add-unit|debug-hooks|destory-service|expose-service|unexpose-service|open-tunnel|remove-relation|remove-unit|ssh|terminate-machine)
63+ cmdOpts=( --environment )
64+ ;;
65+ bootstrap)
66+ cmdOpts=( )
67+ ;;
68+ set|deploy)
69+ cmdOpts=( --environment --repository )
70+ ;;
71+ debug-log)
72+ cmdOpts=( --environment -e --replay -r -i -x -l -n -o --output)
73+ case "$curOpt" in
74+ -l) optEnums=( DEBUG INFO ERROR WARNING CRITICAL ) ;;
75+ esac
76+ ;;
77+ resolved)
78+ cmdOpts=( --retry --environment )
79+ ;;
80+ status)
81+ cmdOpts=( --output --format --environment)
82+ case "$curOpt" in
83+ --format) optEnums=( json yaml png svg dot ) ;;
84+ esac
85+ ;;
86+ upgrade-formula)
87+ cmdOpts=( --dry-run -n --environment --repository)
88+ ;;
89+ *)
90+ cmdOpts=(--help -h)
91+ ;;
92+ esac
93+
94+ IFS=$'\n'
95+ if [[ "$cur" == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
96+ # complete directly after "--option=", list all enum values
97+ COMPREPLY=( "${optEnums[@]}" )
98+ return 0
99+ else
100+ fixedWords=( "${cmdOpts[@]}"
101+ "${globalOpts[@]}"
102+ "${optEnums[@]}"
103+ "${fixedWords[@]}" )
104+ fi
105+
106+ if [[ ${#fixedWords[@]} -gt 0 ]]; then
107+ COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- "$cur" ) )
108+ fi
109+
110+ return 0
111+}
112+
113+complete -F _ensemble -o default ensemble

Subscribers

People subscribed via source and target branches

to status/vote changes: