summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorLee Jarvis <ljjarvis@gmail.com>2014-12-17 14:38:56 +0000
committerLee Jarvis <ljjarvis@gmail.com>2014-12-17 14:40:11 +0000
commita13b9578359f21e14f5a2fa5274a6edd59cd64a1 (patch)
tree9288aefcc8ee18b5ad0b700763fbc8b443f8bb35 /README.md
parent05cc99be8fcaec86a51c021f8b8245a549a50288 (diff)
downloadslop-a13b9578359f21e14f5a2fa5274a6edd59cd64a1.tar.gz
Add readme section for printing help
Diffstat (limited to 'README.md')
-rw-r--r--README.md54
1 files changed, 54 insertions, 0 deletions
diff --git a/README.md b/README.md
index c24beb0..bf84542 100644
--- a/README.md
+++ b/README.md
@@ -170,3 +170,57 @@ opts = Slop.parse do
o.int '-port'
end
```
+
+Printing help
+-------------
+
+The return value of `Slop.parse` is a `Slop::Result` which provides a nice
+help string to display your options. Just `puts opts` or call `opts.to_s`:
+
+```ruby
+opts = Slop.parse do |o|
+ o.string '-h', '--host', 'hostname'
+ o.int '-p', '--port', 'port (default: 80)', default: 80
+ o.string '--username'
+ o.separator ''
+ o.separator 'other options:'
+ o.bool '--quiet', 'suppress output'
+ o.on '-v', '--version' do
+ puts "1.1.1"
+ end
+end
+
+puts opts
+```
+
+Output:
+
+```
+% ruby run.rb
+usage: run.rb [options]
+ -h, --host hostname
+ -p, --port port (default: 80)
+ --username
+
+other options:
+ --quiet suppress output
+ -v, --version
+```
+
+This method takes an optional `prefix` value, which defaults to `" " * 4`:
+
+```
+puts opts.to_s(prefix: " ")
+```
+
+It'll deal with aligning your descriptions according to the longest option
+flag.
+
+Here's an example of adding your own help option:
+
+```ruby
+o.on '--help' do
+ puts o
+ exit
+end
+```