1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
CommandLine - Application and OptionParser
==========================================
Author: Jim Freeze
Copyright 2005 Jim Freeze
DOCS: http://rubyforge.org/docman/view.php/632/232/posted-docs.index.html
Welcome to CommandLine
CommandLine is a library that greatly simplifies the repetitive process of building a command line user interface for your applications. It's 'ruby-like' usage style streamlines application development so that even applications with numerous configuration options can be quickly put together. CommandLine automatically builds friendly usage and help screens that are nicely formatted for the user. No longer is starting an application a pain where you have to copy boiler plate code (or a previous application) and retype repetitive code to get an application started.
CommandLine smartly handles the arguments passed on the commandline. For example, if your application accepts arguments, and none are given, it prints a usage statement. But, if your application accepts no arguments, CommandLine will happily run your application. CommandLine also handles a complex set of options through the OptionParser library, which is described below.
OptionParser is designed to be a flexible command line parser with a Ruby look and feel to it. OptionParser got its birth from the need for a parser that is standards compliant, yet flexible. OptionParser supports the standard command line styles of Unix, Gnu and X Toolkit, but also lets you break those rules.
OptionParser is not a port of a traditional command line parser, but it is written to meet the feature requirements of traditional command line parsers. When using it as a library, you should notice that it is expressive, supports Ruby’s blocks and lambda’s, and is sprinkled with a little bit of magic.
While the library can be used by itself, it is also designed to work with the CommandLine::Application class. These tools work together to facilitate the generation of a sophisticated (batch oriented) application user interface in a matter of minutes.
If you need a refresher on the traditional option parsing schemes, see "Traditional Option Parsing Schemes" below.
EXAMPLES
========
Probably the best way to describe how the tool works is
with some examples:
% cat app.rb
#---------------------------------------------------
#!/usr/bin/env ruby
require 'rubygems'
require 'commandline'
#
# A minimum application
#
class App < CommandLine::Application
def main
end
end#class App
#---------------------------------------------------
% app.rb
Usage: app.rb
% cat app5.rb
#---------------------------------------------------
#!/usr/bin/env ruby
begin
require 'commandline'
rescue LoadError
require 'rubygems'
retry
end
class App < CommandLine::Application
def initialize
version "0.0.1"
author "Author Name"
copyright "2005, Jim Freeze"
synopsis "[-dhV] param_file out_file"
short_description "A simple app example that takes two arguments."
long_description "app5 is a simple application example that supports "+
"three options and two commandline arguments."
option :version
option :debug
option :help
expected_args :param_file, :out_file
end
def main
puts "main called"
puts "@param_file = #{@param_file}"
puts "@out_file = #{@out_file}"
end
end#class App
#---------------------------------------------------
% app5.rb
Usage: app5.rb [-dhV] param_file out_file
% app5.rb -h
NAME
app5.rb - A simple app example that takes two arguments.
DESCRIPTION
app5.rb is a simple application example that supports three options
and two commandline arguments.
OPTIONS
--version,-V
Displays application version.
--debug,-d
Sets debug to true.
--help,-h
Displays help page.
AUTHOR: Author Name
Copyright (c) 2005, Jim Freeze
% app5.rb f1 f2
main called
@param_file = f1
@out_file = f2
% cat app6.rb
#---------------------------------------------------
#!/usr/bin/env ruby
begin
require 'commandline'
rescue LoadError
require 'rubygems'
retry
end
#
# An application demonstrating customizing of canonical options
#
class App < CommandLine::Application
def initialize
version "0.0.1"
author "Author Name"
copyright "2005, Jim Freeze"
short_description "A simple app example that takes two arguments."
long_description "This app is a simple application example that supports "+
"three options and two commandline arguments."
option :version, :names => %w(--version -v --notice-the-change-from-app5)
option :debug, :arity => [0,1], :arg_description => "debug_level",
:opt_description => "Set debug level from 0 to 9."
option :help
expected_args :param_file, :out_file
end
def main
puts "main called"
puts "@param_file = #{@param_file}"
puts "@out_file = #{@out_file}"
end
end#class App
#---------------------------------------------------
% app6.rb -h
NAME
app6.rb - A simple app example that takes two arguments.
DESCRIPTION
This app is a simple application example that supports three
options and two commandline arguments.
OPTIONS
--version,-v,--notice-the-change-from-app5
Displays application version.
--debug,-d debug_level
Set debug level from 0 to 9.
--help,-h
Displays help page.
AUTHOR: Author Name
Copyright (c) 2005, Jim Freeze
% cat app7.rb
#---------------------------------------------------
#!/usr/bin/env ruby
begin
require 'commandline'
rescue LoadError
require 'rubygems'
retry
end
#
# An application demonstrating customizing of canonical options
#
class App < CommandLine::Application
def initialize
version "0.0.1"
author "Author Name"
copyright "2005, Jim Freeze"
short_description "A simple app example that takes two arguments."
long_description "This app is a simple application example that supports "+
"three options and two commandline arguments."
option :version, :names => %w(--version -v --notice-the-change-from-app5)
option :debug, :arity => [0,1], :arg_description => "debug_level",
:opt_description => "Set debug level from 0 to 9."
option :help
expected_args :param_file, :out_file
end
def main
puts "main called"
puts "@param_file = #{@param_file}"
puts "@out_file = #{@out_file}"
end
end#class App
#---------------------------------------------------
% app7.rb -h
NAME
app7.rb - A simple app example that takes two arguments.
DESCRIPTION
This app is a simple application example that supports three
options and two commandline arguments.
OPTIONS
--version,-v,--notice-the-change-from-app5
Displays application version.
--debug,-d debug_level
Set debug level from 0 to 9.
--help,-h
Displays help page.
AUTHOR: Author Name
Copyright (c) 2005, Jim Freeze
TESTS
=====
Tests: 81
Assertions: 310
Download & Installation
=======================
Homepage: http://rubyforge.org/projects/optionparser/
Documentation: http://optionparser.rubyforge.org/
Download: http://rubyforge.org/frs/?group_id=632&release_id=2345
Dependencies:
* None
Currently optionparser is only available as a rubygem.
Via RubyGems
$ gem install -r commandline
All feedback is appreciated!
Installations not yet available
===============================
# not in RPA yet
Via RPA
$ rpa install commandline
# this either
The do-it-yourself way
$ ruby setup.rb config
$ ruby setup.rb setup
$ ruby setup.rb install
# nor this
The simplified do-it-yourself way
$ rake install
RELEASE NOTES
=============
== 0.7.9 11/05/2005
=== Additions
- Renamed gem to lowercase commandline
- Added replay command options
- Added CommandLine::Application_wo_AutoRun - no auto run set thru at_exit
- Added documentation for CommandLine::Application - instead of just README
- Changed :arg_arity to :arity in Option
- Add :required for use with :opt_found
- Added args accessor for @args - suggested by Esteban Manchado Velázquez
- Added opt() accessor for @option_data[]
HISTORY
=======
After poking around in a few corporations, it was evident that
option parsing was not well understood. Therefore, many inhouse
tools were built that did not conform to any of the POSIX, Gnu or XTools
option styles. CommandLine::OptionParser was developed so that
new applications could be written that conformed to accepted standards,
but non-standard option configurations could be handled as well
to support legacy interfaces.
Once the option parsing was written, there was a need to streamline
the repetitive tasks in setting up an application. The original
boilerplate was simple, but after taking a few cues from
rails, a significant amount of functionality was added to
Application that make it a very useful tool yet simple to use.
More information and usage scenarios on OptionParser can be found at:
http://rubyforge.org/projects/optionparser/
ACKNOWLEDGEMENTS
================
This library contains code from:
* Austin Ziegler - Text::Format
* Ara - open4.rb - obtained from codeforthepeople
|