clive 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +63 -11
  2. data/lib/clive/output.rb +47 -13
  3. data/lib/clive/version.rb +3 -0
  4. metadata +4 -3
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # clive
2
2
 
3
- Clive is a DSL for creating a command line interface. It is for people who, like me, love [OptionParser's](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html) syntax and love [GLI's](http://github.com/davetron5000/gli) commands.
3
+ Clive is a DSL for creating a command line interface. It is for people who, like me,
4
+ love [OptionParser's](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html) syntax and love [GLI's](http://github.com/davetron5000/gli) commands.
4
5
 
5
6
  ## Install
6
7
 
@@ -22,7 +23,8 @@ A simple example to start:
22
23
  c.parse(ARGV)
23
24
  p opts
24
25
 
25
- This creates a very simple interface which can have one switch, you can then use the long or short form to call the block.
26
+ This creates a very simple interface which can have one switch, you can then use the
27
+ long or short form to call the block.
26
28
 
27
29
  my_file -v
28
30
  #=> {:verbose => true}
@@ -32,11 +34,14 @@ This creates a very simple interface which can have one switch, you can then use
32
34
 
33
35
  ### Switches
34
36
 
35
- As we've seen above switches are created using #switch. You can provide as little information as you want. `switch(:v) {}` creates a switch that responds only to `-v`, or `switch(:verbose) {}` creates a switch that only responds to `--verbose`.
37
+ As we've seen above switches are created using #switch. You can provide as little
38
+ information as you want. `switch(:v) {}` creates a switch that responds only to
39
+ `-v`, or `switch(:verbose) {}` creates a switch that only responds to `--verbose`.
36
40
 
37
41
  ### Boolean
38
42
 
39
- Boolean switches allow you to accept arguments like `--no-verbose` and `--verbose`, and deal with both situations in the same block.
43
+ Boolean switches allow you to accept arguments like `--no-verbose` and `--verbose`,
44
+ and deal with both situations in the same block.
40
45
 
41
46
  c = Clive.new do
42
47
  bool(:v, :verbose) {|i| p i}
@@ -52,7 +57,9 @@ Boolean switches allow you to accept arguments like `--no-verbose` and `--verbos
52
57
  my_file --no-verbose
53
58
  #=> false
54
59
 
55
- As you can see the true case can be triggered with the short or long form, the false case can be triggered by appending "no-" to the long form, and it can't be triggered with a short form.
60
+ As you can see the true case can be triggered with the short or long form, the false
61
+ case can be triggered by appending "no-" to the long form, and it can't be triggered
62
+ with a short form.
56
63
 
57
64
  ### Flags
58
65
 
@@ -74,8 +81,11 @@ Flags are like switches but also take an argument:
74
81
  my_file -p short
75
82
  #=> "short"
76
83
 
77
- The argument is then passed into the block. As you can see you can use short, long, equals, or no equals to call flags. As with switches you can call `flag(:p) {|i| ...}` which responds to `-p ...`, `flag(:print) {|i| ...}` which responds to `--print ...` or `--print=...`.
78
- Flags can have default values, for that situation put square brackets round the argument name.
84
+ The argument is then passed into the block. As you can see you can use short, long,
85
+ equals, or no equals to call flags. As with switches you can call `flag(:p) {|i| ...}`
86
+ which responds to `-p ...`, `flag(:print) {|i| ...}` which responds to `--print ...`
87
+ or `--print=...`. Flags can have default values, for that situation put square brackets
88
+ round the argument name.
79
89
 
80
90
  flag(:p, :print, "[ARG]", "Print ARG or "hey" by default) do |i|
81
91
  i ||= "hey"
@@ -101,12 +111,15 @@ Commands work like in git, here's an example:
101
111
  my_file add -r Clive
102
112
  #=> {:add => {:lib => "Clive"}}
103
113
 
104
- Commands make it easy to group flags, switches and even other commands. The block for the command is executed on finding the command, this allows you to put other code within the block specific for the command, as shown above.
114
+ Commands make it easy to group flags, switches and even other commands. The block for
115
+ the command is executed on finding the command, this allows you to put other code within
116
+ the block specific for the command, as shown above.
105
117
 
106
118
 
107
119
  ### Arguments
108
120
 
109
- Anything that isn't a command, switch or flag is taken as an argument. These are returned by #parse as an array.
121
+ Anything that isn't a command, switch or flag is taken as an argument. These are returned
122
+ by #parse as an array.
110
123
 
111
124
  opts = {}
112
125
  c = Clive.new do
@@ -123,7 +136,8 @@ Anything that isn't a command, switch or flag is taken as an argument. These are
123
136
 
124
137
  ### Error Handling
125
138
 
126
- You are able to intercept errors when an option does not exist in a similar way to `method_missing`.
139
+ You are able to intercept errors when an option does not exist in a similar way to
140
+ `method_missing`.
127
141
 
128
142
  c = Clive.new do
129
143
  option_missing do |name|
@@ -133,7 +147,9 @@ You are able to intercept errors when an option does not exist in a similar way
133
147
  c.parse("--hey")
134
148
  #=> hey was used but not defined
135
149
 
136
- I was hoping to provide a similar way of intercepting commands as well but these could also be arguments which means it could result in unexpected results. For this reason I will not be implementing `command_missing`.
150
+ I was hoping to provide a similar way of intercepting commands as well but these could
151
+ also be arguments which means it could result in unexpected results. For this reason I
152
+ will not be implementing `command_missing`.
137
153
 
138
154
  ### Putting It All Together
139
155
 
@@ -176,6 +192,42 @@ I was hoping to provide a similar way of intercepting commands as well but these
176
192
  #=> {:verbose => true, :add => {:framework => ["blueprint"], :min => true, :width => 200}}
177
193
  #=> ["~/Desktop/new_thing", "~/Desktop/another_thing"]
178
194
 
195
+
196
+ ## Clive::Output
197
+
198
+ This is a new bit that allows you to colorize output from the command line, by patching a
199
+ few methods onto String.
200
+
201
+ require 'clive/output'
202
+ # or require 'clive'
203
+
204
+ puts "I'm blue".blue # will print blue text
205
+ puts "I'm red".red # will print red text
206
+ puts "I'm green and bold".green.bold # will print green and bold text
207
+ puts "Crazy".blue.l_yellow_bg.underline
208
+ # etc
209
+
210
+ Methods available are:
211
+ - bold
212
+ - underline
213
+ - blink
214
+ - reverse
215
+
216
+ - white
217
+ - green
218
+ - red
219
+ - magenta
220
+ - yellow
221
+ - blue
222
+ - cyan
223
+ - black (light version called grey not l_black)
224
+
225
+ - + light versions of colours using l_colour)
226
+ - + background setters using colour_bg
227
+ - + light background using l_colour_bg
228
+
229
+
230
+
179
231
  ## Note on Patches/Pull Requests
180
232
 
181
233
  * Fork the project.
@@ -35,22 +35,56 @@ class String
35
35
  "#{code}#{self}\e[0m"
36
36
  end
37
37
 
38
- {
39
- "bold" => "1",
40
- "underline" => "4",
41
- "blink" => "5",
42
- "white" => "37",
43
- "green" => "32",
44
- "red" => "31",
45
- "magenta" => "35",
46
- "yellow" => "33",
47
- "blue" => "34",
48
- "grey" => "90"
49
- }.each do |name, code|
50
- define_method(name) do
38
+ COLOURS = {
39
+ "black" => 0,
40
+ "red" => 1,
41
+ "green" => 2,
42
+ "yellow" => 3,
43
+ "blue" => 4,
44
+ "magenta" => 5,
45
+ "cyan" => 6,
46
+ "white" => 7
47
+ }
48
+
49
+ ATTRIBUTES = {
50
+ "bold" => 1,
51
+ "underline" => 4,
52
+ "blink" => 5,
53
+ "reverse" => 7
54
+ }
55
+
56
+ ATTRIBUTES.each do |name, code|
57
+ define_method name do
51
58
  colour("\e[#{code}m")
52
59
  end
53
60
  end
61
+
62
+ COLOURS.each do |name, code|
63
+ define_method name do
64
+ colour("\e[3#{code}m")
65
+ end
66
+
67
+ define_method "#{name}_bg" do
68
+ colour("\e[4#{code}m")
69
+ end
70
+
71
+ # Light white doesn't exist
72
+ unless name == "white"
73
+ # Change name to grey instead of l_black
74
+ _name = "l_#{name}"
75
+ if name == "black"
76
+ _name = "grey"
77
+ end
78
+
79
+ define_method "#{_name}" do
80
+ colour("\e[9#{code}m")
81
+ end
82
+
83
+ define_method "#{_name}_bg" do
84
+ colour("\e[10#{code}m")
85
+ end
86
+ end
87
+ end
54
88
 
55
89
  end
56
90
 
@@ -0,0 +1,3 @@
1
+ class Clive
2
+ VERSION = '0.6.2'
3
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 1
9
- version: 0.6.1
8
+ - 2
9
+ version: 0.6.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Hawxwell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-20 00:00:00 +00:00
17
+ date: 2010-12-21 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -40,6 +40,7 @@ files:
40
40
  - lib/clive/parser.rb
41
41
  - lib/clive/switch.rb
42
42
  - lib/clive/tokens.rb
43
+ - lib/clive/version.rb
43
44
  - lib/clive.rb
44
45
  has_rdoc: false
45
46
  homepage: http://github.com/hawx/clive