minor README.txt tweaks
[trollop:mainline.git] / www / index.html
1 <html>
2 <head>
3 <title>Trollop</title>
4 <style type="text/css">
5 .ruby { background: #111122; padding: 10px; color: #228822; width: 70em; }
6 .ruby .normal { color: #fff; }
7 .ruby .comment { color: #99f }
8 .ruby .keyword { color: #A44; font-weight: bold; }
9 .ruby .method { color: #44f; }
10 .ruby .class { color: #0c4; }
11 .ruby .module { color: #050; }
12 .ruby .punct { color: #FF0; font-weight: bold; }
13 .ruby .symbol { color: #ff0; }
14 .ruby .string { color: #4f4 }
15 .ruby .char { color: #F07; }
16 .ruby .ident { color: #fff; }
17 .ruby .constant { color: #0c4; }
18 .ruby .regex { color: #B66; background: #444; }
19 .ruby .number { color: #F99; }
20 .ruby .attribute { color: #fc4; }
21 .ruby .global { color: #7FB; }
22 .ruby .expr { color: #827; }
23 .ruby .escape { color: #277; }
24 </style>
25 </head>
26
27 <body>
28
29 <h1>Trollop</h1>
30
31 <p style="border: solid 1px black; background: #ccc; padding: 0.5em;">
32 Current project news: see the <a href="http://all-thing.net/label/trollop">blog</a>.
33 </p>
34
35 <p>Trollop is a commandline option parser for Ruby that <i>gets out of your
36 way</i>. One line of code per option is all you need to write. For that, you get
37 a nice automatically-generated help page (fit to your screen size!), robust
38 option parsing, command subcompletion, and sensible defaults for everything you
39 don't specify.</p>
40
41 <p> Reasons to use Trollop:
42 <ol>
43 <li> It requires <i>fewer lines of code</i> than any other option out there. </li>
44 <li> It doesn't require you to subclass some shit just to use a damn option parser.</li>
45 </li>
46 <li> It's a single file. You don't even need to make it a dependency. Just throw it in <code>lib/</code>.</li>
47 <li> This gradient-free webpage. Fight the candy! </li>
48 </ol>
49 </p>
50
51 <p> To install, you have three options:
52 <ul>
53 <li>Trollop is a single file with no dependencies. You can <a href="http://gitorious.org/trollop/mainline/blobs/raw/master/lib/trollop.rb">just download it</a> and throw it in your code directory.</li>
54 <li>You can use rubygems and command your computer to <code>gem install trollop</gem>.</li>
55 <li>You can download the tarball (why?) from the <a href="http://rubyforge.org/projects/trollop/">Trollop Rubyforge page</a>.</p>
56 </ul>
57
58 <p> To hack, command your computer to <code>git clone git://gitorious.org/trollop/mainline.git</code>, or see the <a href="http://gitorious.org/projects/trollop">Trollop Gitorious page</a>.</p>
59
60 <p>To understand, read the examples below, the <a href="FAQ.txt">FAQ</a>, and the <a href="trollop/">API docs</a>.
61
62 <h2>Examples</h2>
63 <h3>Simple</h3>
64 <pre class="ruby"><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
65 <span class="ident">opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
66   <span class="ident">opt</span> <span class="symbol">:monkey</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Use monkey mode</span><span class="punct">&quot;</span>                     <span class="comment"># a flag --monkey, defaulting to false</span>
67   <span class="ident">opt</span> <span class="symbol">:goat</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Use goat mode</span><span class="punct">&quot;,</span> <span class="symbol">:default</span> <span class="punct">=&gt;</span> <span class="constant">true</span>       <span class="comment"># a flag --goat, defaulting to true</span>
68   <span class="ident">opt</span> <span class="symbol">:num_limbs</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Number of limbs</span><span class="punct">&quot;,</span> <span class="symbol">:default</span> <span class="punct">=&gt;</span> <span class="number">4</span>   <span class="comment"># an integer --num-limbs &lt;i&gt;, defaulting to 4</span>
69   <span class="ident">opt</span> <span class="symbol">:num_thumbs</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Number of thumbs</span><span class="punct">&quot;,</span> <span class="symbol">:type</span> <span class="punct">=&gt;</span> <span class="symbol">:int</span> <span class="comment"># an integer --num-thumbs &lt;i&gt;, defaulting to nil</span>
70 <span class="keyword">end</span>
71
72 <span class="ident">p</span> <span class="ident">opts</span> <span class="comment"># a hash: { :monkey =&gt; false, :goat =&gt; true, :num_limbs =&gt; 4, :num_thumbs =&gt; nil }</span>
73 </pre>
74
75 <p>
76 <ul>
77 <li><tt>Trollop::options</tt> returns a hash of values. That's all the output you get.</li>
78 <li>Underscores are converted to dashes. <tt>opt :hello_there</tt> corresponds to an option <tt>--hello-there</tt>.
79 <li>All options are taken to be boolean flags, defaulting to false, unless you specify a default or a type. The type will be inferred from the default if given, so no need to specify both.</li>
80 <li>Short (one-character) option names are created automatically. You can set them manually with <tt>:short</tt>.
81 </ul>
82 </p>
83
84 <h3>Medium</h3>
85 <pre class="ruby">
86 <span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
87 <span class="ident">opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
88   <span class="ident">version</span> <span class="punct">&quot;</span><span class="string">test 1.2.3 (c) 2008 William Morgan</span><span class="punct">&quot;</span>
89   <span class="ident">banner</span> <span class="punct">&lt;&lt;-</span><span class="constant">EOS</span><span class="string">
90 Test is an awesome program that does something very, very important.
91
92 Usage:
93        test [options] &lt;filenames&gt;+
94 where [options] are:
95 </span><span class="constant">EOS</span>
96
97   <span class="ident">opt</span> <span class="symbol">:ignore</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Ignore incorrect values</span><span class="punct">&quot;</span>
98   <span class="ident">opt</span> <span class="symbol">:file</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Extra data filename to read in, with a very long option description like this one</span><span class="punct">&quot;,</span> <span class="symbol">:type</span> <span class="punct">=&gt;</span> <span class="constant">String</span>
99   <span class="ident">opt</span> <span class="symbol">:volume</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Volume level</span><span class="punct">&quot;,</span> <span class="symbol">:default</span> <span class="punct">=&gt;</span> <span class="number">3.0</span>
100   <span class="ident">opt</span> <span class="symbol">:iters</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Number of iterations</span><span class="punct">&quot;,</span> <span class="symbol">:default</span> <span class="punct">=&gt;</span> <span class="number">5</span>
101 <span class="keyword">end</span>
102 <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="symbol">:volume</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">must be non-negative</span><span class="punct">&quot;</span> <span class="keyword">if</span> <span class="ident">opts</span><span class="punct">[</span><span class="symbol">:volume</span><span class="punct">]</span> <span class="punct">&lt;</span> <span class="number">0</span>
103 <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="symbol">:file</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">must exist</span><span class="punct">&quot;</span> <span class="keyword">unless</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">exist?</span><span class="punct">(</span><span class="ident">opts</span><span class="punct">[</span><span class="symbol">:file</span><span class="punct">])</span> <span class="keyword">if</span> <span class="ident">opts</span><span class="punct">[</span><span class="symbol">:file</span><span class="punct">]</span>
104 </pre>
105
106 <p>
107 <ul>
108 <li>You can specify a version and some banner text for the help message. <tt>--help</tt> and <tt>--version</tt> are defined for you.</li>
109 <li>Any special error-checking is done by you, on the option hash. You can call <tt>Trollop::die</tt> to die with a nice message.</li>
110 </ul>
111 </p>
112
113 <h3>Sub-commands, a la SVN and Git</h3>
114 <pre class="ruby"><span class="ident">require</span> <span class="punct">'</span><span class="string">trollop</span><span class="punct">'</span>
115
116 <span class="comment">## Here's a program called &quot;magic&quot;. We want this behavior:</span>
117 <span class="comment">##</span>
118 <span class="comment">##   magic delete &lt;fn&gt; =&gt; deletes a file</span>
119 <span class="comment">##   magic copy &lt;fn&gt;   =&gt; copies a file</span>
120 <span class="comment">##</span>
121 <span class="comment">## So 'delete' and 'copy' are subcommands.</span>
122 <span class="comment">##</span>
123 <span class="comment">## There are some global options, which appear to the left of the subcommand.</span>
124 <span class="comment">## There are some subcommand options, which appear to the right.</span>
125 <span class="comment">##</span>
126 <span class="comment">## Subcommand options can be specific to the subcommand. 'delete' might take</span>
127 <span class="comment">## different options from 'copy'.</span>
128 <span class="comment">##</span>
129 <span class="comment">## We do this by calling Trollop twice; one for the global options and once for</span>
130 <span class="comment">## the subcommand options. We need to tell Trollop what the subcommands are, so</span>
131 <span class="comment">## that it stops processing the global options when it encounters one of them.</span>
132
133 <span class="constant">SUB_COMMANDS</span> <span class="punct">=</span> <span class="punct">%w(</span><span class="string">delete copy</span><span class="punct">)</span>
134 <span class="ident">global_opts</span> <span class="punct">=</span> <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
135   <span class="ident">banner</span> <span class="punct">&quot;</span><span class="string">magic file deleting and copying utility</span><span class="punct">&quot;</span>
136   <span class="ident">opt</span> <span class="symbol">:dry_run</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Don't actually do anything</span><span class="punct">&quot;,</span> <span class="symbol">:short</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">-n</span><span class="punct">&quot;</span>
137   <span class="ident">stop_on</span> <span class="constant">SUB_COMMANDS</span>
138 <span class="keyword">end</span>
139
140 <span class="ident">cmd</span> <span class="punct">=</span> <span class="constant">ARGV</span><span class="punct">.</span><span class="ident">shift</span> <span class="comment"># get the subcommand</span>
141 <span class="ident">cmd_opts</span> <span class="punct">=</span> <span class="keyword">case</span> <span class="ident">cmd</span>
142   <span class="keyword">when</span> <span class="punct">&quot;</span><span class="string">delete</span><span class="punct">&quot;</span> <span class="comment"># parse delete options</span>
143     <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
144       <span class="ident">opt</span> <span class="symbol">:force</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Force deletion</span><span class="punct">&quot;</span>
145     <span class="keyword">end</span>
146   <span class="keyword">when</span> <span class="punct">&quot;</span><span class="string">copy</span><span class="punct">&quot;</span>  <span class="comment"># parse copy options</span>
147     <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">options</span> <span class="keyword">do</span>
148       <span class="ident">opt</span> <span class="symbol">:double</span><span class="punct">,</span> <span class="punct">&quot;</span><span class="string">Copy twice for safety's sake</span><span class="punct">&quot;</span>
149     <span class="keyword">end</span>
150   <span class="keyword">else</span>
151     <span class="constant">Trollop</span><span class="punct">::</span><span class="ident">die</span> <span class="punct">&quot;</span><span class="string">unknown subcommand <span class="expr">#{cmd.inspect}</span></span><span class="punct">&quot;</span>
152   <span class="keyword">end</span>
153
154 <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Global options: <span class="expr">#{global_opts.inspect}</span></span><span class="punct">&quot;</span>
155 <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Subcommand: <span class="expr">#{cmd.inspect}</span></span><span class="punct">&quot;</span>
156 <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Subcommand options: <span class="expr">#{cmd_opts.inspect}</span></span><span class="punct">&quot;</span>
157 <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Remaining arguments: <span class="expr">#{ARGV.inspect}</span></span><span class="punct">&quot;</span>
158
159 </pre>
160
161 <h2> Contributors </h2>
162
163 <p>Trollop is brought to you by <a href="http://cs.stanford.edu/~ruby/">William Morgan</a> and by:
164 <ul>
165 <li>Tuomas Kareinen &lt;tkareine at the gmail dot coms&gt;</li>
166 <li>Ohad Lutzky &lt;ohad at the lutzky dot nets&gt;</li>
167 <li>Erik Ostrom &lt;erik at the echographia dot coms&gt;</li>
168 </ul>
169 </p>
170
171 </body>
172 </html>