Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Check for more edge cases when parsing options and display help when …
Browse files Browse the repository at this point in the history
…requested or options are invalid
  • Loading branch information
jferris committed Sep 21, 2010
1 parent 82ff62a commit f85d294
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
60 changes: 60 additions & 0 deletions features/help.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Feature: get help on using trout

Scenario Outline: Specify an unknown command
When I run "trout <arguments>"
Then the output should contain:
"""
I don't know how to "<command>."
Run "trout help" for usage information.
"""
Examples:
| arguments | command |
| swim | swim |
| help swim | swim |

Scenario Outline: Run an invalid command
When I run "<invalid command>"
Then the output should contain:
"""
I don't understand the options you provided.
Run "<help command>" for usage information.
"""
Examples:
| invalid command | help command |
| trout checkout | trout help checkout |
| trout checkout file | trout help checkout |
| trout checkout file url extra | trout help checkout |
| trout update | trout help update |
| trout update file extra | trout help update |

Scenario Outline: Ask for help
When I run "<help command>"
Then the output should contain:
"""
trout helps you sync individual files from other git repositories.
"""
And the output should contain:
"""
Commands:
"""
And the output should not contain:
"""
I don't know how
"""
Examples:
| help command |
| trout |
| trout help |
| trout -h |
| trout --help |

Scenario Outline: Ask for usage for a particular command
When I run "trout help <command>"
Then the output should contain:
"""
Usage: trout <command> <usage>
"""
Examples:
| command | usage |
| checkout | filename git_url |
| update | filename |
1 change: 1 addition & 0 deletions features/sync_gemfile.feature
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Feature: sync a Gemfile between two repositories
gem "mysql"
gem "redcloth"
"""

92 changes: 83 additions & 9 deletions lib/trout/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'optparse'
require 'trout/managed_file'

module Trout
Expand All @@ -6,31 +7,104 @@ def self.run(arguments)
new(arguments).run
end

attr_reader :arguments, :git_url, :filename, :file, :command
attr_accessor :command, :option_parser, :arguments, :file

def initialize(arguments)
self.arguments = arguments
self.file = ManagedFile.new(filename)
self.arguments = arguments
self.option_parser = parse_options
end

def run
case command
when 'checkout'
self.git_url = arguments[2]
build_file
git_url = next_argument
end_arguments
file.copy_from(git_url)
when 'update'
build_file
end_arguments
file.update
when 'help', nil
puts option_parser
if arguments_left?
puts
usage_for(next_argument)
end
else
unknown_command(command)
end
end

private

def arguments=(arguments)
@arguments = arguments
self.command = arguments[0]
self.filename = arguments[1]
def parse_options
option_parser = OptionParser.new do |parser|
parser.banner =
"trout helps you sync individual files from other git repositories.\n\n" +
"Usage: trout [options] command arguments"

parser.separator ""
parser.separator "Options:"
parser.separator ""

parser.on("-h", "--help", "View this help document")

parser.separator ""
parser.separator "Commands:"
parser.separator ""
parser.separator " checkout - start tracking a file from another repository"
parser.separator " help - get usage details for a specific command"
parser.separator " update - synchronize changes to a tracked file"
end

option_parser.parse!(arguments)

if arguments_left?
self.command = next_argument
else
self.command = 'help'
end

option_parser
end

def next_argument
arguments.shift or invalid_arguments
end

attr_writer :git_url, :filename, :file, :command
def arguments_left?
!arguments.empty?
end

def invalid_arguments
puts "I don't understand the options you provided."
puts %{Run "trout help #{command}" for usage information.}
exit
end

def end_arguments
invalid_arguments if arguments_left?
end

def build_file
self.file = ManagedFile.new(next_argument)
end

def usage_for(command)
case command
when 'checkout'
puts "Usage: trout checkout filename git_url"
when 'update'
puts "Usage: trout update filename"
else
unknown_command(command)
end
end

def unknown_command(command)
puts %{I don't know how to "#{command}."}
puts %{Run "trout help" for usage information.}
end
end
end

0 comments on commit f85d294

Please sign in to comment.