Skip to content
Browse files

Cleaned up API

Updated example.rb to match API changes
  • Loading branch information...
1 parent 95e8805 commit e1cad5f046b86fd7deb7246baba7aea54ae93ace @shabbyrobe shabbyrobe committed Aug 22, 2012
Showing with 43 additions and 27 deletions.
  1. +8 −4 example.rb
  2. +35 −23 lib/docopt.rb
View
12 example.rb
@@ -20,11 +20,15 @@
--testsuite=dir run regression tests from dir
--doctest run doctest on myself"
-require 'docopt'
+require 'rubygems'
+require 'yaml'
+
+require './lib/docopt'
if __FILE__ == $0
- options = Docopt($DOC, '1.0.0') # parse options based on doc above
- puts options.inspect
- puts ARGV.inspect
+ options = Docopt($DOC, { :version => '1.0.0' }) # parse options based on doc above
+ options.sort.each { |key, value|
+ puts key + ': ' + value.inspect
+ }
end
View
58 lib/docopt.rb
@@ -11,11 +11,11 @@ def self.usage
end
def self.set_usage(usage)
- @@usage = usage
+ @@usage = usage ? usage : ''
end
def initialize(message='')
- super(message + "\n" + @@usage)
+ super((message && message != '' ? (message + "\n") : '') + @@usage)
end
end
@@ -594,10 +594,10 @@ def parse_doc_options(doc)
def printable_usage(doc)
usage_split = doc.split(/([Uu][Ss][Aa][Gg][Ee]:)/)
if usage_split.count < 3
- raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
+ raise DocoptLanguageError, '"usage:" (case-insensitive) not found.'
end
if usage_split.count > 3
- raise DocoptLanguageError('More than one "usage:" (case-insensitive).')
+ raise DocoptLanguageError, 'More than one "usage:" (case-insensitive).'
end
return usage_split[1..-1].join().split(/\n\s*\n/)[0].strip
end
@@ -661,37 +661,49 @@ def extras(help, version, options, doc)
if help and ofound
Exit.set_usage(nil)
- raise Exit(doc.strip)
+ raise Exit, doc.strip
end
if version and vfound
Exit.set_usage(nil)
- raise Exit(version)
+ raise Exit, version
end
end
- def docopt(doc, argv=nil, help=true, version=nil)
- argv = ARGV if !argv
+ def docopt(doc, params={})
+ begin
+ default = { :version => nil, :argv => nil, :help => true }
+ params = default.merge(params)
+ params[:argv] = ARGV if !params[:argv]
- Exit.set_usage(printable_usage(doc))
- options = parse_doc_options(doc)
- pattern = parse_pattern(formal_usage(Exit.usage), options)
- argv = parse_argv(argv, options)
- extras(help, version, argv, doc)
+ Exit.set_usage(printable_usage(doc))
+ options = parse_doc_options(doc)
+ pattern = parse_pattern(formal_usage(Exit.usage), options)
+ argv = parse_argv(params[:argv], options)
+ extras(params[:help], params[:version], argv, doc)
- matched, left, collected = pattern.fix().match(argv)
- collected = [] if !collected
+ matched, left, collected = pattern.fix().match(argv)
+ collected = [] if !collected
- if matched and (!left or left.count == 0)
- ret = {}
- for a in pattern.flat + options + collected
- name = a.name
- if name and name != ''
- ret[name] = a.value
+ if matched and (!left or left.count == 0)
+ ret = {}
+ for a in pattern.flat + options + collected
+ name = a.name
+ if name and name != ''
+ ret[name] = a.value
+ end
end
+ return ret
end
- return ret
+ raise Exit
+
+ rescue Exit => ex
+ puts ex.message.rstrip + "\n"
end
- raise Exit
end
end
end
+
+# Convenience method for Docopt.docopt
+def Docopt *args
+ Docopt.docopt *args
+end

0 comments on commit e1cad5f

Please sign in to comment.
Something went wrong with that request. Please try again.