choosy 0.4.9 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/Gemfile.lock +13 -1
  2. data/README.md +259 -0
  3. data/Rakefile +28 -17
  4. data/lib/VERSION.yml +4 -4
  5. data/lib/choosy.rb +38 -10
  6. data/lib/choosy/argument.rb +0 -2
  7. data/lib/choosy/base_command.rb +9 -8
  8. data/lib/choosy/command.rb +0 -6
  9. data/lib/choosy/converter.rb +0 -5
  10. data/lib/choosy/dsl/argument_builder.rb +0 -5
  11. data/lib/choosy/dsl/base_builder.rb +0 -2
  12. data/lib/choosy/dsl/base_command_builder.rb +54 -42
  13. data/lib/choosy/dsl/command_builder.rb +0 -6
  14. data/lib/choosy/dsl/option_builder.rb +4 -6
  15. data/lib/choosy/dsl/super_command_builder.rb +0 -4
  16. data/lib/choosy/errors.rb +10 -2
  17. data/lib/choosy/option.rb +0 -3
  18. data/lib/choosy/parse_result.rb +0 -2
  19. data/lib/choosy/parser.rb +0 -4
  20. data/lib/choosy/printing/base_printer.rb +0 -3
  21. data/lib/choosy/printing/color.rb +21 -33
  22. data/lib/choosy/printing/erb_printer.rb +0 -2
  23. data/lib/choosy/printing/formatting_element.rb +0 -2
  24. data/lib/choosy/printing/help_printer.rb +0 -3
  25. data/lib/choosy/printing/manpage.rb +5 -2
  26. data/lib/choosy/printing/manpage_printer.rb +1 -6
  27. data/lib/choosy/rake.rb +33 -31
  28. data/lib/choosy/super_command.rb +1 -7
  29. data/lib/choosy/super_parser.rb +0 -6
  30. data/lib/choosy/terminal.rb +0 -3
  31. data/lib/choosy/verifier.rb +0 -3
  32. data/lib/choosy/version.rb +0 -2
  33. data/spec/choosy/argument_spec.rb +0 -3
  34. data/spec/choosy/base_command_spec.rb +0 -3
  35. data/spec/choosy/command_spec.rb +0 -4
  36. data/spec/choosy/converter_spec.rb +0 -4
  37. data/spec/choosy/dsl/argument_builder_spec.rb +0 -3
  38. data/spec/choosy/dsl/base_builder_spec.rb +0 -3
  39. data/spec/choosy/dsl/base_command_builder_spec.rb +18 -5
  40. data/spec/choosy/dsl/commmand_builder_spec.rb +0 -6
  41. data/spec/choosy/dsl/option_builder_spec.rb +0 -3
  42. data/spec/choosy/dsl/super_command_builder_spec.rb +0 -4
  43. data/spec/choosy/option_spec.rb +0 -3
  44. data/spec/choosy/parser_spec.rb +0 -5
  45. data/spec/choosy/printing/base_printer_spec.rb +0 -5
  46. data/spec/choosy/printing/color_spec.rb +1 -4
  47. data/spec/choosy/printing/help_printer_spec.rb +0 -5
  48. data/spec/choosy/printing/manpage_printer_spec.rb +0 -4
  49. data/spec/choosy/printing/manpage_spec.rb +2 -3
  50. data/spec/choosy/super_command_spec.rb +0 -3
  51. data/spec/choosy/super_parser_spec.rb +0 -5
  52. data/spec/choosy/terminal_spec.rb +0 -3
  53. data/spec/choosy/verifier_spec.rb +0 -7
  54. data/spec/choosy/version_spec.rb +0 -2
  55. metadata +127 -121
  56. data/README.markdown +0 -463
  57. data/lib/choosy/printing.rb +0 -1
  58. data/spec/integration/command-A_spec.rb +0 -37
@@ -1,9 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/base_command'
3
- require 'choosy/dsl/command_builder'
4
- require 'choosy/parser'
5
- require 'choosy/verifier'
6
-
7
1
  module Choosy
8
2
  class Command < BaseCommand
9
3
  attr_accessor :executor, :arguments
@@ -1,8 +1,3 @@
1
- require 'choosy/errors'
2
- require 'time'
3
- require 'date'
4
- require 'yaml'
5
-
6
1
  module Choosy
7
2
  class Converter
8
3
  CONVERSIONS = {
@@ -1,8 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/argument'
3
- require 'choosy/converter'
4
- require 'choosy/dsl/base_builder'
5
-
6
1
  module Choosy::DSL
7
2
  class ArgumentBuilder
8
3
  include BaseBuilder
@@ -1,5 +1,3 @@
1
- require 'choosy/errors'
2
-
3
1
  module Choosy::DSL
4
2
  # Must have entity attribute
5
3
  module BaseBuilder
@@ -1,10 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/dsl/option_builder'
3
- require 'choosy/dsl/base_builder'
4
- require 'choosy/printing/manpage_printer'
5
- require 'choosy/printing/erb_printer'
6
- require 'choosy/printing/formatting_element'
7
-
8
1
  module Choosy::DSL
9
2
  class BaseCommandBuilder
10
3
  include BaseBuilder
@@ -76,31 +69,28 @@ module Choosy::DSL
76
69
  evaluate_option_builder!(builder, &block)
77
70
  end
78
71
 
79
- def self.create_conversions
80
- Choosy::Converter::CONVERSIONS.keys.each do |method|
81
- next if method == :boolean || method == :bool
72
+ Choosy::Converter::CONVERSIONS.keys.each do |method|
73
+ next if method == :boolean || method == :bool
82
74
 
83
- self.class_eval <<-EOF
84
- def #{method}(sym, desc, config=nil, &block)
85
- simple_option(sym, desc, true, :one, :#{method}, nil, config, &block)
86
- end
75
+ self.class_eval <<-EOF, __FILE__, __LINE__
76
+ def #{method}(sym, desc, config=nil, &block)
77
+ simple_option(sym, desc, true, :one, :#{method}, nil, config, &block)
78
+ end
87
79
 
88
- def #{method}s(sym, desc, config=nil, &block)
89
- simple_option(sym, desc, true, :many, :#{method}, nil, config, &block)
90
- end
80
+ def #{method}s(sym, desc, config=nil, &block)
81
+ simple_option(sym, desc, true, :many, :#{method}, nil, config, &block)
82
+ end
91
83
 
92
- def #{method}_(sym, desc, config=nil, &block)
93
- simple_option(sym, desc, false, :one, :#{method}, nil, config, &block)
94
- end
84
+ def #{method}_(sym, desc, config=nil, &block)
85
+ simple_option(sym, desc, false, :one, :#{method}, nil, config, &block)
86
+ end
95
87
 
96
- def #{method}s_(sym, desc, config=nil, &block)
97
- simple_option(sym, desc, false, :many, :#{method}, nil, config, &block)
98
- end
99
- EOF
100
- end
88
+ def #{method}s_(sym, desc, config=nil, &block)
89
+ simple_option(sym, desc, false, :many, :#{method}, nil, config, &block)
90
+ end
91
+ EOF
101
92
  end
102
93
 
103
- create_conversions
104
94
  alias :single :string
105
95
  alias :single_ :string_
106
96
 
@@ -127,34 +117,56 @@ module Choosy::DSL
127
117
  # Additional helpers
128
118
 
129
119
  def help(msg=nil, &block)
130
- h = OptionBuilder.new(OptionBuilder::HELP)
131
- h.short '-h'
132
- h.long '--help'
133
120
  msg ||= "Show this help message"
134
- h.desc msg
135
121
 
136
- h.validate do
137
- raise Choosy::HelpCalled.new(:help_option)
138
- end
122
+ h = OptionBuilder.new(OptionBuilder::HELP) do
123
+ short '-h'
124
+ long '--help'
125
+ desc msg
126
+ validate do
127
+ raise Choosy::HelpCalled.new(:help)
128
+ end
129
+ end
139
130
 
140
131
  evaluate_option_builder!(h, &block)
141
132
  end
142
133
 
143
134
  def version(msg, &block)
144
- v = OptionBuilder.new(OptionBuilder::VERSION)
145
- v.long '--version'
146
- v.desc "The version number"
147
- # TODO: research how this should be refactored, used with manpage
148
- # v.default msg
149
- v.cast :boolean
150
-
151
- v.validate do
152
- raise Choosy::VersionCalled.new(msg)
135
+ v = OptionBuilder.new(OptionBuilder::VERSION) do
136
+ long '--version'
137
+ desc "The version number"
138
+ cast :boolean
139
+ validate do
140
+ raise Choosy::VersionCalled.new(msg)
141
+ end
142
+ end
143
+
144
+ option_eigenclass = class << v.entity; self; end
145
+ option_eigenclass.send :define_method, :version do
146
+ msg
153
147
  end
154
148
 
155
149
  evaluate_option_builder!(v, &block)
156
150
  end
157
151
 
152
+ def no_color(msg, &block)
153
+ msg ||= "Disable the color"
154
+ cmd = entity
155
+
156
+ n = OptionBuilder.new(:no_color) do
157
+ long "--no-color"
158
+ desc msg
159
+ cast :boolean
160
+ validate do |args, options|
161
+ if cmd.printer.respond_to?(:color)
162
+ cmd.printer.color.disable!
163
+ end
164
+ end
165
+ end
166
+
167
+ evaluate_option_builder!(n, &block)
168
+ end
169
+
158
170
  protected
159
171
  def evaluate_option_builder!(builder, &block)
160
172
  builder.evaluate!(&block)
@@ -1,9 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/dsl/base_command_builder'
3
- require 'choosy/dsl/option_builder'
4
- require 'choosy/dsl/argument_builder'
5
- require 'choosy/printing/help_printer'
6
-
7
1
  module Choosy::DSL
8
2
  class CommandBuilder < BaseCommandBuilder
9
3
  def executor(exec=nil, &block)
@@ -1,16 +1,14 @@
1
- require 'choosy/option'
2
- require 'choosy/errors'
3
- require 'choosy/converter'
4
- require 'choosy/dsl/argument_builder'
5
-
6
1
  module Choosy::DSL
7
2
  class OptionBuilder < ArgumentBuilder
8
3
  HELP = :__help__
9
4
  VERSION = :__version__
10
5
 
11
- def initialize(name)
6
+ def initialize(name, &block)
12
7
  super()
13
8
  @name = name
9
+ if block_given?
10
+ self.instance_eval(&block)
11
+ end
14
12
  end
15
13
 
16
14
  def entity
@@ -1,7 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/dsl/base_command_builder'
3
- require 'choosy/command'
4
-
5
1
  module Choosy::DSL
6
2
  class SuperCommandBuilder < BaseCommandBuilder
7
3
  HELP = :help
@@ -3,11 +3,19 @@ module Choosy
3
3
 
4
4
  ConfigurationError = Class.new(Choosy::Error)
5
5
  ValidationError = Class.new(Choosy::Error)
6
- HelpCalled = Class.new(Choosy::Error)
7
- VersionCalled = Class.new(Choosy::Error)
8
6
  ConversionError = Class.new(Choosy::Error)
9
7
  ParseError = Class.new(Choosy::Error)
10
8
  SuperParseError = Class.new(Choosy::Error)
11
9
  VersionError = Class.new(Choosy::Error)
12
10
  ClientExecutionError = Class.new(Choosy::Error)
11
+
12
+ class HelpCalled < Choosy::Error
13
+ def initialize(m); @m = m; end
14
+ def message; @m; end
15
+ end
16
+
17
+ class VersionCalled < Choosy::Error
18
+ def initialize(m); @m = m; end
19
+ def message; @m; end
20
+ end
13
21
  end
@@ -1,6 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/argument'
3
-
4
1
  module Choosy
5
2
  class Option < Argument
6
3
  attr_accessor :name, :description
@@ -1,5 +1,3 @@
1
- require 'choosy/verifier'
2
-
3
1
  module Choosy
4
2
  class BaseParseResult
5
3
  attr_reader :command, :options, :unparsed
@@ -1,7 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/parse_result'
3
- require 'choosy/dsl/option_builder'
4
-
5
1
  module Choosy
6
2
  class Parser
7
3
  attr_reader :flags, :terminals, :command
@@ -1,6 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/terminal'
3
-
4
1
  module Choosy::Printing
5
2
  class BasePrinter
6
3
  include Choosy::Terminal
@@ -1,5 +1,3 @@
1
- require 'choosy/errors'
2
-
3
1
  module Choosy::Printing
4
2
  class Color
5
3
  # Extrapolated from:
@@ -27,8 +25,10 @@ module Choosy::Printing
27
25
  :normal => 22
28
26
  }
29
27
 
30
- FOREGROUND = 30
31
- BACKGROUND = 40
28
+ KINDS = {
29
+ :foreground => 30,
30
+ :background => 40
31
+ }
32
32
 
33
33
  def initialize
34
34
  begin
@@ -62,7 +62,7 @@ module Choosy::Printing
62
62
  originally_nil = str.nil?
63
63
  styles.each do |style|
64
64
  if color?(style)
65
- str = bedazzle(COLORS[style] + FOREGROUND, str, originally_nil)
65
+ str = bedazzle(COLORS[style] + KINDS[:foreground], str, originally_nil)
66
66
  elsif effect?(style)
67
67
  str = bedazzle(EFFECTS[style], str, originally_nil)
68
68
  end
@@ -70,42 +70,30 @@ module Choosy::Printing
70
70
  str
71
71
  end
72
72
 
73
- def respond_to?(method)
74
- color?(method) || effect?(method)
75
- end
73
+ COLORS.each do |color, number|
74
+ define_method color do |*args|
75
+ raise ArgumentError, "Too many arguments, (max 2)" if args.length > 2
76
76
 
77
- # Dynamically handle colors and effects
78
- def method_missing(method, *args, &block)
79
- if disabled?
80
- return args[0] || ""
81
- end
77
+ return args[0] || "" if disabled?
78
+ kind = args[1]
79
+ if kind && !KINDS.has_key?(kind)
80
+ raise ArgumentError, "Unrecognized format, only :foreground or :background supported"
81
+ end
82
82
 
83
- if color?(method)
84
- raise ArgumentError.new("too many arguments to Color##{method} (max 2)") if args.length > 2
85
- offset = find_state(method, args[1])
86
- bedazzle(COLORS[method] + offset, args[0])
87
- elsif effect?(method)
88
- raise ArgumentError.new("too many arguments to Color##{method} (max 1)") if args.length > 1
89
- bedazzle(EFFECTS[method], args[0])
90
- else
91
- raise NoMethodError.new("undefined method '#{method}' for Color")
83
+ bedazzle(number + KINDS[(kind || :foreground)], args[0])
92
84
  end
93
85
  end
94
86
 
95
- private
96
- def find_state(method, state)
97
- case state
98
- when nil
99
- FOREGROUND
100
- when :foreground
101
- FOREGROUND
102
- when :background
103
- BACKGROUND
104
- else
105
- raise ArgumentError.new("unrecognized state for Color##{method}, :foreground or :background only")
87
+ EFFECTS.each do |color, number|
88
+ define_method color do |*args|
89
+ raise ArgumentError, "Too many arguments, (max 1)" if args.length > 1
90
+
91
+ return args[0] || "" if disabled?
92
+ bedazzle(number, args[0])
106
93
  end
107
94
  end
108
95
 
96
+ private
109
97
  def bedazzle(number, str, keep_open=nil)
110
98
  prefix = "\e[#{number}m"
111
99
  if str.nil?
@@ -1,5 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/printing/help_printer'
3
1
  require 'erb'
4
2
 
5
3
  module Choosy::Printing
@@ -1,5 +1,3 @@
1
- require 'choosy/errors'
2
-
3
1
  module Choosy::Printing
4
2
  class FormattingElement
5
3
  attr_reader :value, :styles, :kind
@@ -1,6 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/printing/base_printer'
3
-
4
1
  module Choosy::Printing
5
2
  class HelpPrinter < BasePrinter
6
3
  attr_reader :buffer, :usage, :line_count
@@ -1,5 +1,3 @@
1
- require 'choosy/errors'
2
-
3
1
  module Choosy::Printing
4
2
  class ManpageFormatter
5
3
  def bold(line=nil)
@@ -34,6 +32,7 @@ module Choosy::Printing
34
32
  class Manpage
35
33
  attr_accessor :name, :section, :date, :version, :manual
36
34
  attr_reader :format, :buffer
35
+ attr_accessor :column_width
37
36
 
38
37
  def initialize
39
38
  @buffer = []
@@ -176,6 +175,10 @@ module Choosy::Printing
176
175
  io << "'\\\" t\n"
177
176
  io << frame_outline << NEWLINE
178
177
  io << PREFACE
178
+ if column_width
179
+ io << ".ll " << column_width.to_s << NEWLINE
180
+ end
181
+
179
182
  @buffer.each do |line|
180
183
  io << line
181
184
  io << NEWLINE
@@ -1,9 +1,3 @@
1
- require 'choosy/errors'
2
- require 'choosy/option'
3
- require 'choosy/dsl/option_builder'
4
- require 'choosy/printing/base_printer'
5
- require 'choosy/printing/manpage'
6
-
7
1
  module Choosy::Printing
8
2
  class ManpagePrinter < BasePrinter
9
3
  attr_reader :manpage, :name, :synopsis
@@ -33,6 +27,7 @@ module Choosy::Printing
33
27
  end
34
28
 
35
29
  def format_prologue(command)
30
+ @manpage.column_width = columns
36
31
  @manpage.name = command_name(command)
37
32
  version_option = command.option_builders[Choosy::DSL::OptionBuilder::VERSION]
38
33
  if version_option && @manpage.version.nil?
@@ -40,48 +40,50 @@ module SH
40
40
  end
41
41
  end
42
42
 
43
- #########################################################################
44
- # Version
45
- require 'choosy/version'
46
-
47
- desc "Shows the current version number."
48
- task :version => ['version:load'] do
49
- puts "Current version: #{$version.to_s}"
50
- end
43
+ namespace :choosy do
44
+ #########################################################################
45
+ # Version
46
+ require 'choosy/version'
47
+
48
+ desc "Shows the current version number."
49
+ task :version => ['version:load'] do
50
+ puts "Current version: #{$version.to_s}"
51
+ end
51
52
 
52
- namespace :version do
53
- task :load do
54
- puts Dir.pwd
55
- if ENV['VERSION_FILE']
56
- $version = Choosy::Version.new(ENV['VERSION_FILE'])
57
- else
58
- $version = Choosy::Version.load_from_lib Dir.pwd
53
+ namespace :version do
54
+ task :load do
55
+ puts Dir.pwd
56
+ if ENV['VERSION_FILE']
57
+ $version = Choosy::Version.new(ENV['VERSION_FILE'])
58
+ else
59
+ $version = Choosy::Version.load_from_lib Dir.pwd
60
+ end
59
61
  end
60
- end
61
62
 
62
- [:tiny, :minor, :major].each do |type|
63
- desc "Bumps the #{type} revision number."
64
- task type => :load do
65
- old = $version.to_s
66
- $version.version!(type)
67
- puts "Bumped version: #{old} -> #{$version}"
63
+ [:tiny, :minor, :major].each do |type|
64
+ desc "Bumps the #{type} revision number."
65
+ task type => :load do
66
+ old = $version.to_s
67
+ $version.version!(type)
68
+ puts "Bumped version: #{old} -> #{$version}"
69
+ end
68
70
  end
69
71
  end
70
- end
71
72
 
72
- #########################################################################
73
- # Release
74
- desc "Tags the current release in git, builds the gemspec, and pushes the gem"
75
- task :release => ['gem:clean', 'git:diff', 'gem:push', 'git:tag', 'git:push']
73
+ #########################################################################
74
+ # Release
75
+ desc "Tags the current release in git, builds the gemspec, and pushes the gem"
76
+ task :release => ['gem:clean', 'git:diff', 'gem:push', 'git:tag', 'git:push']
77
+ end
76
78
 
77
79
  #########################################################################
78
80
  # Git
79
81
  namespace :git do
80
- task :diff => ['version:load'] do
82
+ task :diff => ['choosy:version:load'] do
81
83
  SH.attempt "git diff --exit-code", :error => "git diff: Your work doesn't seem to be checked in!", :quiet => true
82
84
  end
83
85
 
84
- task :tag => [:diff, 'version:load'] do
86
+ task :tag => [:diff, 'choosy:version:load'] do
85
87
  puts "Tagging version #{$version}"
86
88
  SH.attempt "git tag -a -m \"Tagging release #{$version}\" v#{$version}" do |code, contents|
87
89
  puts contents
@@ -90,7 +92,7 @@ namespace :git do
90
92
  end
91
93
  end
92
94
 
93
- task :push => [:diff, 'version:load'] do
95
+ task :push => [:diff, 'choosy:version:load'] do
94
96
  SH.attempt "git push"
95
97
  SH.attempt "git push --tags"
96
98
  end
@@ -103,7 +105,7 @@ task :gem => ['gem:build']
103
105
 
104
106
  namespace :gem do
105
107
  desc "Builds the current gemspec."
106
- task :build => ['version:load'] do
108
+ task :build => ['choosy:version:load'] do
107
109
  SH.files('gemspec') do |gemspec|
108
110
  puts " Building gemspec: #{gemspec}"
109
111
  SH.attempt "gem build #{gemspec}"