Skip to content

Commit ced4cef

Browse files
committed
port examples from docopt
1 parent 1467738 commit ced4cef

9 files changed

+199
-31
lines changed

docopt.gemspec

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ Gem::Specification.new do |s|
6363
README.md
6464
Rakefile
6565
docopt.gemspec
66-
example.rb
66+
examples/any_options_example.rb
67+
examples/calculator.rb
68+
examples/counted_example.rb
69+
examples/example_options.rb
70+
examples/git_example.rb
71+
examples/naval_fate.rb
72+
examples/odd_even_example.rb
73+
examples/quick_example.rb
6774
lib/docopt.rb
6875
test/test_docopt.rb
6976
test/testee.rb

examples/calculator.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Usage:
5+
#{__FILE__} tcp <host> <port> [--timeout=<seconds>]
6+
#{__FILE__} serial <port> [--baud=9600] [--timeout=<seconds>]
7+
#{__FILE__} -h | --help | --version
8+
9+
DOCOPT
10+
11+
begin
12+
require "pp"
13+
pp Docopt::docopt(doc)
14+
rescue Docopt::Exit => e
15+
puts e.message
16+
end

examples/counted_example.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Usage: #{__FILE__} --help
5+
#{__FILE__} -v...
6+
#{__FILE__} go [go]
7+
#{__FILE__} (--path=<path>)...
8+
#{__FILE__} <file> <file>
9+
10+
Try: #{__FILE__} -vvvvvvvvvv
11+
#{__FILE__} go go
12+
#{__FILE__} --path ./here --path ./there
13+
#{__FILE__} this.txt that.txt
14+
15+
DOCOPT
16+
17+
begin
18+
require "pp"
19+
pp Docopt::docopt(doc)
20+
rescue Docopt::Exit => e
21+
puts e.message
22+
end

examples/example.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/example_options.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Example of program with many options using docopt.
5+
6+
Usage:
7+
#{__FILE__} [-hvqrf NAME] [--exclude=PATTERNS]
8+
[--select=ERRORS | --ignore=ERRORS] [--show-source]
9+
[--statistics] [--count] [--benchmark] PATH...
10+
#{__FILE__} (--doctest | --testsuite=DIR)
11+
#{__FILE__} --version
12+
13+
Arguments:
14+
PATH destination path
15+
16+
Options:
17+
-h --help show this help message and exit
18+
--version show version and exit
19+
-v --verbose print status messages
20+
-q --quiet report only file names
21+
-r --repeat show all occurrences of the same error
22+
--exclude=PATTERNS exclude files or directories which match these comma
23+
separated patterns [default: .svn,CVS,.bzr,.hg,.git]
24+
-f NAME --file=NAME when parsing directories, only check filenames matching
25+
these comma separated patterns [default: *#{__FILE__}]
26+
--select=ERRORS select errors and warnings (e.g. E,W6)
27+
--ignore=ERRORS skip errors and warnings (e.g. E4,W)
28+
--show-source show source code for each error
29+
--statistics count errors and warnings
30+
--count print total number of errors and warnings to standard
31+
error and set exit code to 1 if total is not null
32+
--benchmark measure processing speed
33+
--testsuite=DIR run regression tests from dir
34+
--doctest run doctest on myself
35+
36+
37+
DOCOPT
38+
39+
begin
40+
require "pp"
41+
pp Docopt::docopt(doc)
42+
rescue Docopt::Exit => e
43+
puts e.message
44+
end

examples/git_example.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Usage:
5+
#{__FILE__} remote [-v | --verbose]
6+
#{__FILE__} remote add [-t <branch>] [-m <master>] [-f]
7+
[--tags|--no-tags] [--mirror] <name> <url>
8+
#{__FILE__} remote rename <old> <new>
9+
#{__FILE__} remote rm <name>
10+
#{__FILE__} remote set-head <name> (-a | -d | <branch>)
11+
#{__FILE__} remote set-branches <name> [--add] <branch>...
12+
#{__FILE__} remote set-url [--push] <name> <newurl> [<oldurl>]
13+
#{__FILE__} remote set-url --add [--push] <name> <newurl>
14+
#{__FILE__} remote set-url --delete [--push] <name> <url>
15+
#{__FILE__} remote [-v | --verbose] show [-n] <name>
16+
#{__FILE__} remote prune [-n | --dry-run] <name>
17+
#{__FILE__} remote [-v | --verbose] update [-p | --prune]
18+
[(<group> | <remote>)...]
19+
20+
Options:
21+
-v, --verbose
22+
-t <branch>
23+
-m <master>
24+
-f
25+
--tags
26+
--no-tags
27+
--mittor
28+
-a
29+
-d
30+
-n, --dry-run
31+
-p, --prune
32+
--add
33+
--delete
34+
--push
35+
--mirror
36+
37+
DOCOPT
38+
39+
begin
40+
require "pp"
41+
pp Docopt::docopt(doc)
42+
rescue Docopt::Exit => e
43+
puts e.message
44+
end

examples/naval_fate.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
#The *popular* naval fate example
4+
5+
doc = <<DOCOPT
6+
Naval Fate.
7+
8+
Usage:
9+
#{__FILE__} ship new <name>...
10+
#{__FILE__} ship <name> move <x> <y> [--speed=<kn>]
11+
#{__FILE__} ship shoot <x> <y>
12+
#{__FILE__} mine (set|remove) <x> <y> [--moored|--drifting]
13+
#{__FILE__} -h | --help
14+
#{__FILE__} --version
15+
16+
Options:
17+
-h --help Show this screen.
18+
--version Show version.
19+
--speed=<kn> Speed in knots [default: 10].
20+
--moored Moored (anchored) mine.
21+
--drifting Drifting mine.
22+
23+
DOCOPT
24+
25+
begin
26+
require "pp"
27+
pp Docopt::docopt(doc)
28+
rescue Docopt::Exit => e
29+
puts e.message
30+
end

examples/odd_even_example.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Usage: #{__FILE__} [-h | --help] (ODD EVEN)...
5+
6+
Example, try:
7+
#{__FILE__} 1 2 3 4
8+
9+
Options:
10+
-h, --help
11+
12+
DOCOPT
13+
14+
begin
15+
require "pp"
16+
pp Docopt::docopt(doc)
17+
rescue Docopt::Exit => e
18+
puts e.message
19+
end

examples/quick_example.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require File.expand_path("../../lib/docopt.rb", __FILE__)
2+
3+
doc = <<DOCOPT
4+
Usage:
5+
#{__FILE__} tcp <host> <port> [--timeout=<seconds>]
6+
#{__FILE__} serial <port> [--baud=9600] [--timeout=<seconds>]
7+
#{__FILE__} -h | --help | --version
8+
9+
DOCOPT
10+
11+
begin
12+
require "pp"
13+
pp Docopt::docopt(doc)
14+
rescue Docopt::Exit => e
15+
puts e.message
16+
end

0 commit comments

Comments
 (0)