Merge pull request #22 from dbck/using_thor_as_cli

Using thor as cli
This commit is contained in:
Tomasz Gieniusz 2013-08-20 12:28:03 -07:00
commit 181701546e
4 changed files with 49 additions and 24 deletions

View file

@ -20,8 +20,43 @@ It browses the repository and outputs html page with statistics.
### Generator ### Generator
#### Print help
$ git_stats $ git_stats
<follow instructions on the screen> Commands:
git_stats generate # Generates the statistics of a repository
git_stats help [COMMAND] # Describe available commands or one specific command
#### Print help of the generate command
$ git_stats help generate
Usage:
git_stats generate
Options:
p, [--path=PATH] # Path to repository from which statistics should be generated.
# Default: .
o, [--output=OUTPUT] # Output path where statistics should be written.
# Default: ./git_stats
l, [--language=LANGUAGE] # Language of written statistics.
# Default: en
f, [--from=FROM] # Commit from where statistics should start.
t, [--to=TO] # Commit where statistics should stop.
# Default: HEAD
#### Start generator with default settings
$ git_stats generate
git rev-list --pretty=format:'%h|%at|%ai|%aE' HEAD | grep -v commit
git shortlog -se HEAD
...
#### Start generator with some parameters in long and short form.
$ git_stats generate -o stats --langugage de
git rev-list --pretty=format:'%h|%at|%ai|%aE' HEAD | grep -v commit
git shortlog -se HEAD
...
### API usage example ### API usage example

View file

@ -7,5 +7,4 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
# start up the CLI # start up the CLI
require "git_stats/cli" require "git_stats/cli"
cli = GitStats::CLI.new GitStats::CLI.start(ARGV)
cli.start

View file

@ -25,5 +25,5 @@ Gem::Specification.new do |gem|
gem.add_dependency('haml') gem.add_dependency('haml')
gem.add_dependency('lazy_high_charts') gem.add_dependency('lazy_high_charts')
gem.add_dependency('i18n') gem.add_dependency('i18n')
gem.add_dependency('highline') gem.add_dependency('thor')
end end

View file

@ -1,27 +1,18 @@
# -*- encoding : utf-8 -*- # -*- encoding : utf-8 -*-
require "git_stats" require "git_stats"
require "highline/import" require "thor"
class GitStats::CLI class GitStats::CLI < Thor
option :path, :aliases => :p, :default => '.', :desc => 'Path to repository from which statistics should be generated.'
option :output, :aliases => :o, :default => './git_stats', :desc => 'Output path where statistics should be written.'
option :language, :aliases => :l, :default => 'en', :desc => 'Language of written statistics.'
option :from, :aliases => :f, :desc => 'Commit from where statistics should start.'
option :to, :aliases => :t, :default => 'HEAD', :desc => 'Commit where statistics should stop.'
def start desc 'generate', 'Generates the statistics of a repository'
def generate
repo_path = ask("Repo path? ") { |q| I18n.locale = options[:language]
q.default = "." GitStats::Generator.new(options[:path], options[:output], options[:from], options[:to]) { |g|
q.validate = lambda { |p| GitStats::Validator.new.valid_repo_path?(p) }
q.responses[:not_valid] = "Given path is not a git repository, try again"
}
out_path = ask("Output dir? ") { |q| q.default = "./git_stats" }
I18n.locale = ask("Language? ") { |q| q.default = "en"; q.answer_type = Symbol }
specify_range = agree("Want to specify commits range? ") { |q| q.default = "no" }
if specify_range
first_commit_sha = ask("Starting commit sha? ") { |q| q.default = nil }
last_commit_sha = ask("Ending commit sha? ") { |q| q.default = "HEAD" }
end
GitStats::Generator.new(repo_path, out_path, first_commit_sha, last_commit_sha) { |g|
g.add_command_observer { |command, result| puts "#{command}" } g.add_command_observer { |command, result| puts "#{command}" }
}.render_all }.render_all
end end