Skip to content

Commit

Permalink
Switch from slop to GLI for command parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismcg committed Nov 17, 2011
1 parent 442700b commit 94c9602
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 67 deletions.
91 changes: 36 additions & 55 deletions bin/localeapp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))

require 'slop'
require 'localeapp'

# Don't connect to the net if we're running under cucumber for testing
Expand All @@ -22,84 +21,71 @@ def include_config_file
end
end

# Ugly global for now till we figure out a better way
$recognized_command = false
require 'gli'
include GLI

slop = Slop.new(ARGV, :help => true, :strict => true) do
banner "Usage: localeapp COMMAND [options]"
summary <<-COMMANDS
COMMAND:
install <api_key> - Creates new configuration files and confirms key works
pull - Pulls all translations from localeapp.com
push <file> - Pushes a translation file to localeapp.com
add <key> <locale>:<content> (<locale>:<content> ...)
- Sends the key and content to localeapp.com
update - Gets any changes since the last poll and updates the yml
daemon - Simple daemon (checks every 5 seconds for new translations)
COMMANDS
version Localeapp::VERSION

on_empty { puts self.to_s }
end

slop.command :install do
execute do |opts, args|
$recognized_command = true
desc "Creates new configuration files and confirms key works"
arg_name "<api_key>"
command :install do |c|
c.action do |global_options, options, args|
key = args.first
installer = Localeapp::CLI::Install.new
if installer.execute(key)
exit 0
else
exit 1
unless installer.execute(key)
exit_now! "", 1
end
end
end

slop.command :add do
execute do |opts, args|
$recognized_command = true
desc "Sends the key and content to localeapp.com"
arg_name "<key> <locale:content> (<locale:content> ...)"
command :add do |c|
c.action do |global_options, options, args|
key = args.shift
if key.nil? || args.size.zero?
puts "localeapp add requires a key name and at least one translation"
exit 1
exit_now! "localeapp add requires a key name and at least one translation", 1
else
include_config_file
Localeapp::CLI::Add.new.execute(key, *args)
end
end
end

slop.command :pull do
execute do |opts, args|
$recognized_command = true
desc "Pulls all translations from localeapp.com"
command :pull do |c|
c.action do |global_options, options, args|
include_config_file
Localeapp::CLI::Pull.new.execute
end
end

slop.command :push do
on_empty { puts "localeapp push requires an file to push" }

execute do |opts, args|
$recognized_command = true
file = args.first
exit 1 if file.nil?
include_config_file
pusher = Localeapp::CLI::Push.new
pusher.execute(file)
desc "Pushes a translation file to localeapp.com"
arg_name "<file>"
command :push do |c|
c.action do |global_options, options, args|
if args.empty?
exit_now! "localeapp push requires an file to push", 1
else
file = args.first
include_config_file
pusher = Localeapp::CLI::Push.new
pusher.execute(file)
end
end
end

slop.command :update do
execute do |opts, args|
$recognized_command = true
desc "Gets any changes since the last poll and updates the yml"
command :update do |c|
c.action do |global_options, options, args|
include_config_file
Localeapp::CLI::Update.new.execute
end
end

slop.command :daemon do
execute do |opts, args|
$recognized_command = true
desc "Simple daemon (checks every 5 seconds for new translations)"
command :daemon do |c|
c.action do |global_options, options, args|
include_config_file
while true do
Localeapp::CLI::Update.new.execute
Expand All @@ -108,9 +94,4 @@ slop.command :daemon do
end
end

begin
slop.parse!
rescue Slop::InvalidOptionError
puts slop
end
puts slop unless $recognized_command
exit GLI.run(ARGV)
14 changes: 3 additions & 11 deletions features/localeapp_binary.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@ Feature: localeapp executable

Scenario: Viewing help
In order to see what options I have
When I run `localeapp --help`
When I run `localeapp help`
Then the output should contain:
"""
Usage: localeapp COMMAND [options]
COMMAND:
install <api_key> - Creates new configuration files and confirms key works
pull - Pulls all translations from localeapp.com
usage: localeapp command [command options]
"""

Scenario: Running a command that doesn't exist
In order to warn of a bad command
When I run `localeapp foo`
Then the output should contain:
"""
Usage: localeapp COMMAND [options]
COMMAND:
install <api_key> - Creates new configuration files and confirms key works
pull - Pulls all translations from localeapp.com
error: Unknown command 'foo'. Use 'localeapp help' for a list of commands
"""

Scenario: Running install
Expand Down
2 changes: 1 addition & 1 deletion localeapp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency('json')
s.add_dependency('rest-client')
s.add_dependency('ya2yaml')
s.add_dependency('slop')
s.add_dependency('gli')

s.add_development_dependency('rake')
s.add_development_dependency('rspec', '2.5.0')
Expand Down

0 comments on commit 94c9602

Please sign in to comment.