davetron5000-gli 0.1.6 → 0.2.0

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.
data/README.rdoc CHANGED
@@ -1,6 +1,16 @@
1
- This is a command line parser for a git-like command line client.
1
+ = Git-Like Interface Command Line Parser
2
2
 
3
- = Use
3
+ Author:: Dave Copeland (mailto:davetron5000 at g mail dot com)
4
+ Copyright:: Copyright (c) 2009 by Dave Copeland
5
+ License:: Distributes under the Apache License, see LICENSE.txt in the source distro
6
+
7
+ This is a DSL you can use to create a command line interface like git, gem or svn, in that the first argument is a command, and there are global and command specific flags.
8
+
9
+ == Use
10
+
11
+ Install if you need to:
12
+
13
+ sudo gem install gli
4
14
 
5
15
  The simplest way to get started is to create a scaffold project
6
16
 
@@ -10,7 +20,7 @@ This will create a (very) basic scaffold project in <tt>./my_proj</tt>, with a b
10
20
  main file in <tt>./my_proj/bin/my_proj</tt>. This file demonstrates most of what you need
11
21
  to describe your command line interface
12
22
 
13
- == More Detail
23
+ === More Detail
14
24
 
15
25
  This sets you up to use the DSL that GLI defines:
16
26
 
@@ -59,7 +69,7 @@ line arguments
59
69
 
60
70
  c.action do |global_options,options,args|
61
71
  if args.length < 1
62
- raise(MissingArgumentException,'You must specify the name of your project')
72
+ raise 'You must specify the name of your project'
63
73
  end
64
74
  Scaffold.create_scaffold(g[:r],!o[:notest],o[:e],args[0],args[1..-1],o[:force],g[:n])
65
75
  end
@@ -100,7 +110,7 @@ What this doesn't give you:
100
110
  * A way to indicate a require argument or required number of arguments
101
111
  * A way to do default switches to 'true' and therefore accept things like <tt>--no-force</tt>
102
112
 
103
- = Interface Generated
113
+ == Interface Generated
104
114
 
105
115
  *executable* <i>global options and flags</i> *command* <i>command specific options and flags</i> `arguments`
106
116
 
@@ -109,7 +119,7 @@ What this doesn't give you:
109
119
  [command] the command to execute. The <tt>rebase</tt> in <tt>git rebase</tt>
110
120
  [arguments] Anything that's not a switch, flag, or command. The <tt>main.c</tt> in <tt>git add main.c</tt>
111
121
 
112
- == Switches
122
+ === Switches
113
123
 
114
124
  Switches can be specified one at a time in either a long or short format:
115
125
 
@@ -121,15 +131,20 @@ Switches can also be combined in their short form:
121
131
  ls -l -a
122
132
  ls -la
123
133
 
124
- == Flags
134
+ === Flags
125
135
 
126
136
  Flags can be specified in long or short form, and with or without an equals:
127
137
 
128
138
  git merge -s resolve
129
139
  git merge --strategy=resolve
130
140
 
131
- == Stop Switch
141
+ === Stop Switch
132
142
 
133
143
  A <tt>--</tt> at any time stops processing and sends the rest of the argument to the command as arguments, even if
134
144
  they start with a "--"
135
145
 
146
+ == Links
147
+
148
+ * [http://davetron5000.github.com/gli] - RubyDoc
149
+ * [http://www.github.com/davetron5000/gli] - Source on GitHub
150
+
data/bin/gli CHANGED
@@ -33,7 +33,7 @@ command [:init,:scaffold] do |c|
33
33
 
34
34
  c.action do |g,o,args|
35
35
  if args.length < 1
36
- raise(MissingArgumentException,'You must specify the name of your project')
36
+ raise 'You must specify the name of your project'
37
37
  end
38
38
  Scaffold.create_scaffold(g[:r],!o[:notest],o[:e],args[0],args[1..-1],o[:force],g[:n])
39
39
  end
@@ -53,9 +53,4 @@ post do |global,command,options,args|
53
53
  puts "Executed #{command.name}" if global[:v]
54
54
  end
55
55
 
56
- #on_error do |global,command,options,args|
57
- # puts "Got an error" if global[:v]
58
- # true
59
- #end
60
-
61
56
  run(ARGV)
data/lib/gli.rb CHANGED
@@ -72,8 +72,9 @@ module GLI
72
72
  end
73
73
 
74
74
  # Define a block to run if an error occurs.
75
- # The block will receive the exception that was caught.
76
- # It should return false to avoid the built-in error handling
75
+ # The block will receive any Exception that was caught.
76
+ # It should return false to avoid the built-in error handling (which basically just
77
+ # prints out a message)
77
78
  def on_error(&a_proc)
78
79
  @@error_block = a_proc
79
80
  end
@@ -90,15 +91,12 @@ module GLI
90
91
  command.execute(global_options,options,arguments)
91
92
  @@post_block.call(global_options,command,options,arguments) if @@post_block
92
93
  end
93
- rescue UnknownCommandException, UnknownArgumentException, MissingArgumentException => ex
94
+ rescue Exception => ex
94
95
  regular_error_handling = true
95
96
  regular_error_handling = @@error_block.call(ex) if @@error_block
96
97
 
97
98
  if regular_error_handling
98
- puts "error: #{ex}"
99
- puts
100
- help = commands[:help]
101
- help.execute({},{},[])
99
+ puts "error: #{ex.message}"
102
100
  end
103
101
  end
104
102
  end
@@ -175,7 +173,7 @@ module GLI
175
173
  if !command
176
174
  command_name = args.shift
177
175
  command = find_command(command_name)
178
- raise(UnknownCommandException,"Unknown command '#{command_name}'") if !command
176
+ raise "Unknown command '#{command_name}'" if !command
179
177
  return parse_options_helper(args,global_options,command,command_options,arguments)
180
178
  else
181
179
  return global_options,command,command_options,arguments | args
@@ -225,16 +223,16 @@ module GLI
225
223
  try_me.delete arg
226
224
  break
227
225
  end
228
- raise(UnknownArgumentException,"Unknown argument #{arg}") if arg =~ /^\-/
226
+ raise "Unknown argument #{arg}" if arg =~ /^\-/
229
227
  end
230
228
  return [global_options,command,command_options,try_me | rest]
231
229
  else
232
230
  # Now we have our command name
233
231
  command_name = try_me.shift
234
- raise(UnknownArgumentException,"Unknown argument #{command_name}") if command_name =~ /^\-/
232
+ raise "Unknown argument #{command_name}" if command_name =~ /^\-/
235
233
 
236
234
  command = find_command(command_name)
237
- raise(UnknownCommandException,"Unknown command '#{command_name}'") if !command
235
+ raise "Unknown command '#{command_name}'" if !command
238
236
 
239
237
  return parse_options_helper(rest,global_options,command,command_options,arguments)
240
238
  end
@@ -252,14 +250,4 @@ module GLI
252
250
  nil
253
251
  end
254
252
 
255
- # Raise this if you get an argument you were not expecting
256
- class UnknownArgumentException < Exception
257
- end
258
-
259
- class UnknownCommandException < Exception
260
- end
261
-
262
- # Raise this if your command doesn't get the number of arguments you were expecting
263
- class MissingArgumentException < Exception
264
- end
265
253
  end
data/lib/gli/flag.rb CHANGED
@@ -24,7 +24,7 @@ module GLI
24
24
  args.delete_at index
25
25
  return value
26
26
  else
27
- raise(MissingArgumentException,"#{matched} requires an argument")
27
+ raise "#{matched} requires an argument"
28
28
  end
29
29
  else
30
30
  return value
@@ -38,7 +38,7 @@ module GLI
38
38
  if @names[arg]
39
39
  return [true,arg,nil] if arg.length == 2
40
40
  # This means we matched the long-form, but there's no argument
41
- raise(MissingArgumentException,"#{arg} requires an argument via #{arg}=argument")
41
+ raise "#{arg} requires an argument via #{arg}=argument"
42
42
  end
43
43
  @names.keys.each() do |name|
44
44
  match_string = "^#{name}=(.*)$"
@@ -53,7 +53,11 @@ command :#{command} do |c|
53
53
  c.default_value 'default'
54
54
  c.flag :s
55
55
  c.action do |global_options,options,args|
56
+
56
57
  # Your command logic here
58
+
59
+ # If you have any errors, just raise them
60
+ # raise "that command made no sense"
57
61
  end
58
62
  end
59
63
  EOS
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: davetron5000-gli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Copeland
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-08 00:00:00 -07:00
12
+ date: 2009-05-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15