Skip to content

Commit

Permalink
- Add completely init --nested and explain nested syntax in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Nov 25, 2024
1 parent 742e3cd commit f1e17ed
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 68 deletions.
81 changes: 60 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ or with Docker:
$ alias completely='docker run --rm -it --user $(id -u):$(id -g) --volume "$PWD:/app" dannyben/completely'
```

## Using the `completely` command line
## Configuration syntax

The `completely` command line works with a simple YAML configuration file as
input, and generates a bash completions script as output.
Completely works with a simple YAML configuration file as input, and generates
a bash completions script as output.

The configuration file is built of blocks that look like this:

Expand All @@ -59,45 +59,42 @@ for the auto complete process.
You can save a sample YAML file by running:
```
```bash
$ completely init
```

This will generate a file named `completely.yaml` with this content:

```yaml
mygit:
- -h
- -v
- --help
- --version
- status
- init
- commit
- status

mygit init:
- --bare
- <directory>

mygit status:
- --help
- --verbose
- --branch
- $(git branch --format='%(refname:short)' 2>/dev/null)
- -b

mygit init:
- --bare
- <directory>
mygit status*--branch: &branches
- $(git branch --format='%(refname:short)' 2>/dev/null)

mygit commit:
- <file>
- --help
- --message
- --all
- -a
- --quiet
- -q
mygit status*-b: *branches
```
Each pattern in this configuration file will be checked against the user's
input, and if the input **starts with** a matching pattern, the list that
follows it will be suggested as completions.
input, and if the input matches the pattern, the list that follows it will be
suggested as completions.
Note that the suggested completions will not show flags (string that start with
Note that the suggested completions will not show flags (strings that start with
a hyphen `-`) unless the input ends with a hyphen.

To generate the bash script, simply run:
Expand Down Expand Up @@ -207,6 +204,48 @@ mygit checkout*--branch: &branches
mygit checkout*-b: *branches
```

### Alternative nested syntax

Completely also supports an alternative nested syntax. You can generate this
example by running:

```bash
$ completely init --nested
```

The example configuration below will generate the exact same script as the one
shown at the beginning of this document.

```yaml
mygit:
- -h
- -v
- --help
- --version
- init:
- --bare
- <directory>
- status:
- --help
- --verbose
- +--branch: &branches
- $(git branch --format='%(refname:short)' 2>/dev/null)
- +-b: *branches
```

The rules here are as follows:

- Each pattern (e.g., `mygit`) can have a mixed array of strings and hashes.
- Strings and hash keys (e.e., `--help` and `init` respectively) will be used
as completion strings for that pattern.
- Hashes may contain a nested mixed arrya of the same structure.
- When a hash is provided, teh hash key will be appended to the parent prefix.
In the example above, the `init` hash will create the patter `mygit init`.
- In order to provide a wildcard (like `mygit status*--branch` in the standard
configuration syntax), you can provide either a `*` or a `+` prefix to the
hash key (e.g., `+--branch` or `"*--branch"`). Note that when using a `*`,
the hash key must be quoted since asterisks have special meaning in YAML.

## Using the generated completion scripts

In order to enable the completions, simply source the generated script:
Expand Down
13 changes: 11 additions & 2 deletions lib/completely/commands/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ module Commands
class Init < Base
help 'Create a new sample YAML configuration file'

usage 'completely init [CONFIG_PATH]'
usage 'completely init [--nested] [CONFIG_PATH]'
usage 'completely init (-h|--help)'

option '-n --nested', 'Generate a nested configuration'

param_config_path
environment_config_path

Expand All @@ -24,8 +26,15 @@ def sample
@sample ||= File.read sample_path
end

def nested?
args['--nested']
end

def sample_path
@sample_path ||= File.expand_path '../templates/sample.yaml', __dir__
@sample_path ||= begin
sample_name = nested? ? 'sample-nested' : 'sample'
File.expand_path "../templates/#{sample_name}.yaml", __dir__
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/completely/templates/sample-nested.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mygit:
- -h
- -v
- --help
- --version
- init:
- --bare
- <directory>
- status:
- --help
- --verbose
- +--branch: &branches
- $(git branch --format='%(refname:short)' 2>/dev/null)
- +-b: *branches
25 changes: 11 additions & 14 deletions lib/completely/templates/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
mygit:
- -h
- -v
- --help
- --version
- status
- init
- commit
- status

mygit init:
- --bare
- <directory>

mygit status:
- --help
- --verbose
- --branch
- $(git branch --format='%(refname:short)' 2>/dev/null)
- -b

mygit init:
- --bare
- <directory>
mygit status*--branch: &branches
- $(git branch --format='%(refname:short)' 2>/dev/null)

mygit commit:
- <file>
- --help
- --message
- --all
- -a
- --quiet
- -q
mygit status*-b: *branches
14 changes: 9 additions & 5 deletions spec/approvals/cli/generated-script
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ _mygit_completions() {
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
'status'*'--branch')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'status'*'-b')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'commit'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur")
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch -b")" -- "$cur")
;;

'init'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")
;;

*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur")
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h -v --help --version init status")" -- "$cur")
;;

esac
Expand Down
14 changes: 9 additions & 5 deletions spec/approvals/cli/generated-script-alt
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ _mycomps() {
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "--help --verbose --branch $(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
'status'*'--branch')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'status'*'-b')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'commit'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_mycomps_filter "--help --message --all -a --quiet -q")" -- "$cur")
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "--help --verbose --branch -b")" -- "$cur")
;;

'init'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mycomps_filter "--bare")" -- "$cur")
;;

*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "--help --version status init commit")" -- "$cur")
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "-h -v --help --version init status")" -- "$cur")
;;

esac
Expand Down
14 changes: 9 additions & 5 deletions spec/approvals/cli/generated-wrapped-script
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ give_comps() {
echo $' local compline="${compwords[*]}"'
echo $''
echo $' case "$compline" in'
echo $' \'status\'*)'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'
echo $' \'status\'*\'--branch\')'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'
echo $' ;;'
echo $''
echo $' \'status\'*\'-b\')'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'
echo $' ;;'
echo $''
echo $' \'commit\'*)'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur")'
echo $' \'status\'*)'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch -b")" -- "$cur")'
echo $' ;;'
echo $''
echo $' \'init\'*)'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")'
echo $' ;;'
echo $''
echo $' *)'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur")'
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h -v --help --version init status")" -- "$cur")'
echo $' ;;'
echo $''
echo $' esac'
Expand Down
5 changes: 4 additions & 1 deletion spec/approvals/cli/init/help
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Create a new sample YAML configuration file

Usage:
completely init [CONFIG_PATH]
completely init [--nested] [CONFIG_PATH]
completely init (-h|--help)

Options:
-n --nested
Generate a nested configuration

-h --help
Show this help

Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/init/nested
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Saved completely.yaml
14 changes: 9 additions & 5 deletions spec/approvals/cli/test/completely-tester-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ _mygit_completions() {
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
'status'*'--branch')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'status'*'-b')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'commit'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur")
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch -b")" -- "$cur")
;;

'init'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")
;;

*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur")
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h -v --help --version init status")" -- "$cur")
;;

esac
Expand Down
14 changes: 9 additions & 5 deletions spec/approvals/cli/test/completely-tester-2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ _mygit_completions() {
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
'status'*'--branch')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'status'*'-b')
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
;;

'commit'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur")
'status'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch -b")" -- "$cur")
;;

'init'*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")
;;

*)
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur")
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h -v --help --version init status")" -- "$cur")
;;

esac
Expand Down
Loading

0 comments on commit f1e17ed

Please sign in to comment.