Skip to content
Browse files

Create binary and parse options

  • Loading branch information...
1 parent 302db94 commit 547de2315deff80bec8a44c11a7b8634e946e2cf @eparreno eparreno committed Aug 6, 2012
Showing with 27 additions and 12 deletions.
  1. +5 −2 bin/apiary
  2. +1 −1 lib/apiary.rb
  3. +21 −9 lib/apiary/cli.rb
View
7 bin/apiary
@@ -1,5 +1,8 @@
#!/usr/bin/env ruby
+# encoding: UTF-8
-require File.expand_path('../../lib/apiary', __FILE__)
+lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
+$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
+require File.dirname(__FILE__) + '/../lib/apiary'
-Apiary::Cli.run(ARGV.dup)
+Apiary::CLI.run(ARGV.dup)
View
2 lib/apiary.rb
@@ -1,5 +1,5 @@
require "apiary/version"
+require "apiary/cli"
module Apiary
- # Your code goes here...
end
View
30 lib/apiary/cli.rb
@@ -1,4 +1,9 @@
-class Apiary::Cli
+require 'optparse'
+
+class Apiary::CLI
+
+ attr_reader :args
+ attr_reader :options
def initialize(args=[])
@args = args
@@ -9,14 +14,21 @@ def self.run(args)
new(args).run
end
+ def run
+ @args << '-h' if @args.empty?
+ parse_options!(@args)
+ end
+
def parse_options!(args)
- options_parser = OptionsParser.new do |opts|
- opts.banner = "\nAvailable options:\n\n"
-
- opts.on("--preview") {}
- opts.on("--server") { @options[:port] = 8080 }
- end
- @args = options_parser.parse!(@args)
- puts("ARGS: #{@args.inspect}")
+ options_parser = OptionParser.new do |opts|
+ opts.banner = "\nUsage: apiary command [options]\n\n"
+
+ opts.on("--preview", 'Show API documentation in a browser') {}
+ opts.on("--server", 'Start a web server on port 8080') { @options[:port] = 8080 }
+ opts.on('-v', '--version', 'Display the version') { puts Apiary::VERSION }
+ opts.on( '-h', '--help', 'Display this screen' ) { puts opts; exit }
+ end
+ @args = options_parser.parse!(@args)
end
+
end

0 comments on commit 547de23

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