Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Create binary and parse options

  • Loading branch information...
commit 547de2315deff80bec8a44c11a7b8634e946e2cf 1 parent 302db94
Emili Parreño eparreno authored

Showing 3 changed files with 27 additions and 12 deletions. Show diff stats Hide diff stats

  1. +5 2 bin/apiary
  2. +1 1  lib/apiary.rb
  3. +21 9 lib/apiary/cli.rb
7 bin/apiary
... ... @@ -1,5 +1,8 @@
1 1 #!/usr/bin/env ruby
  2 +# encoding: UTF-8
2 3
3   -require File.expand_path('../../lib/apiary', __FILE__)
  4 +lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
  5 +$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
  6 +require File.dirname(__FILE__) + '/../lib/apiary'
4 7
5   -Apiary::Cli.run(ARGV.dup)
  8 +Apiary::CLI.run(ARGV.dup)
2  lib/apiary.rb
... ... @@ -1,5 +1,5 @@
1 1 require "apiary/version"
  2 +require "apiary/cli"
2 3
3 4 module Apiary
4   - # Your code goes here...
5 5 end
30 lib/apiary/cli.rb
... ... @@ -1,4 +1,9 @@
1   -class Apiary::Cli
  1 +require 'optparse'
  2 +
  3 +class Apiary::CLI
  4 +
  5 + attr_reader :args
  6 + attr_reader :options
2 7
3 8 def initialize(args=[])
4 9 @args = args
@@ -9,14 +14,21 @@ def self.run(args)
9 14 new(args).run
10 15 end
11 16
  17 + def run
  18 + @args << '-h' if @args.empty?
  19 + parse_options!(@args)
  20 + end
  21 +
12 22 def parse_options!(args)
13   - options_parser = OptionsParser.new do |opts|
14   - opts.banner = "\nAvailable options:\n\n"
15   -
16   - opts.on("--preview") {}
17   - opts.on("--server") { @options[:port] = 8080 }
18   - end
19   - @args = options_parser.parse!(@args)
20   - puts("ARGS: #{@args.inspect}")
  23 + options_parser = OptionParser.new do |opts|
  24 + opts.banner = "\nUsage: apiary command [options]\n\n"
  25 +
  26 + opts.on("--preview", 'Show API documentation in a browser') {}
  27 + opts.on("--server", 'Start a web server on port 8080') { @options[:port] = 8080 }
  28 + opts.on('-v', '--version', 'Display the version') { puts Apiary::VERSION }
  29 + opts.on( '-h', '--help', 'Display this screen' ) { puts opts; exit }
  30 + end
  31 + @args = options_parser.parse!(@args)
21 32 end
  33 +
22 34 end

0 comments on commit 547de23

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