Skip to content

Commit d33b363

Browse files
author
Kimmo Lehto
committed
Update README.md
1 parent 1f62526 commit d33b363

File tree

1 file changed

+75
-12
lines changed

1 file changed

+75
-12
lines changed

README.md

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Clamp::Completer
1+
[![Build Status](https://travis-ci.org/kontena/clamp-completer.svg?branch=master)](https://travis-ci.org/kontena/clamp-completer)
2+
[![Gem Version](https://badge.fury.io/rb/clamp-completer)](https://badge.fury.io/rb/clamp-completer)
23

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clamp/completer`. To experiment with that code, run `bin/console` for an interactive prompt.
4+
# Clamp::Completer
45

5-
TODO: Delete this and the text above, and describe your gem
6+
Automatically generate shell auto-completion scripts for Ruby command-line tools built using the [clamp](https://github.com/mdub/clamp) gem.
67

78
## Installation
89

@@ -12,23 +13,85 @@ Add this line to your application's Gemfile:
1213
gem 'clamp-completer'
1314
```
1415

15-
And then execute:
16-
17-
$ bundle
18-
1916
Or install it yourself as:
2017

21-
$ gem install clamp-completer
18+
```
19+
$ gem install clamp-completer
20+
```
2221

2322
## Usage
2423

25-
TODO: Write usage instructions here
24+
Require the `clamp/completer` and in your application's root command, add a subcommand:
25+
26+
```ruby
27+
require 'clamp/completer'
28+
29+
Clamp do
30+
subcommand "complete", "shell autocompletions", Clamp::Completer.new(self)
31+
end
32+
```
33+
34+
This will add a `complete` subcommand:
35+
36+
```
37+
$ your_app complete zsh
38+
$ your_app complete bash
39+
```
40+
41+
You can redirect the output to a static file or load the output directly into the running environment:
42+
43+
```
44+
# zsh / bash:
45+
$ source <(your_app complete)
46+
# or for the macOs preinstalled bash version:
47+
$ source /dev/stdin <<<"$(your_app complete bash)"
48+
```
49+
50+
### Customizing completions
51+
52+
Currently, subcommand completions and flag-type options defined through `option '--debug', :flag, 'enable debug'` should work correctly out-of-the-box. For options that take parameters,
53+
such as file paths or a predefined set of words, you can define methods on your command classes that will be used to determine how the values should be completed:
54+
55+
```ruby
56+
Clamp do
57+
option '--config', 'YAML_FILE', "configuration YAML file path"
58+
59+
def complete_yaml_file # name derived from the YAML_FILE argument description
60+
{ glob: '*.yml' }
61+
end
62+
end
63+
```
64+
65+
```ruby
66+
Clamp do
67+
option '--role', 'ROLE_NAME', "node role"
68+
69+
def complete_role # name derived from the attribute name
70+
"master worker" # will add "master" and "worker" as completion responses when you do: your_app --role <tab>
71+
end
72+
end
73+
```
74+
75+
#### Completion method response types
76+
77+
##### String
78+
79+
A space separated string of possible values for the option
80+
81+
##### Symbol
82+
83+
Currently known symbols:
84+
85+
* `:dirs` will complete directory names
86+
* `:files` will complete file names
87+
* `:hosts` will complete known host names
2688

27-
## Development
89+
##### Hash
2890

29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91+
Much like the Symbols, but returned in Hash format to enable passing options:
3092

31-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93+
* `{ glob: '*.yml' }` will complete files endinging with `.yml`
94+
* `{ command: 'cut -d':' -f1 /etc/passwd' }` will run a command to get completion candidates, in this case the usernames from `/etc/password`
3295

3396
## Contributing
3497

0 commit comments

Comments
 (0)