cri 1.0.1 → 2.0a1

Sign up to get free protection for your applications and to get access to all the features.
data/NEWS DELETED
@@ -1,9 +0,0 @@
1
- = Cri News
2
-
3
- == 1.0.1
4
-
5
- * Made gem actually include code. D'oh.
6
-
7
- == 1.0.0
8
-
9
- * Initial release!
data/README DELETED
@@ -1,4 +0,0 @@
1
- Cri
2
- ===
3
-
4
- Cri is a library for building easy-to-use commandline tools.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.0.1
data/lib/cri/base.rb DELETED
@@ -1,153 +0,0 @@
1
- module Cri
2
-
3
- # Cri::Base is the central class representing a commandline tool. It has a
4
- # list of commands.
5
- class Base
6
-
7
- # The CLI's list of commands (should also contain the help command)
8
- attr_reader :commands
9
-
10
- # The CLI's help command (required)
11
- attr_accessor :help_command
12
-
13
- # Creates a new instance of the commandline tool.
14
- def initialize(tool_name)
15
- @tool_name = tool_name
16
-
17
- @commands = []
18
- end
19
-
20
- # Parses the given commandline arguments and executes the requested
21
- # command.
22
- def run(args)
23
- # Check arguments
24
- if args.length == 0
25
- @help_command.run([], [])
26
- exit 1
27
- end
28
-
29
- # Partition options
30
- opts_before_command = []
31
- command_name = nil
32
- opts_and_args_after_command = []
33
- stage = 0
34
- args.each do |arg|
35
- # Update stage if necessary
36
- stage = 1 if stage == 0 && !is_option?(arg)
37
-
38
- # Add
39
- opts_before_command << arg if stage == 0
40
- command_name = arg if stage == 1
41
- opts_and_args_after_command << arg if stage == 2
42
-
43
- # Update stage if necessary
44
- stage = 2 if stage == 1
45
- end
46
-
47
- # Handle options before command
48
- begin
49
- parsed_arguments = Cri::OptionParser.parse(opts_before_command, global_option_definitions)
50
- rescue Cri::OptionParser::IllegalOptionError => e
51
- $stderr.puts "illegal option -- #{e}"
52
- exit 1
53
- end
54
- parsed_arguments[:options].keys.each do |option|
55
- handle_option(option)
56
- end
57
-
58
- # Get command
59
- if command_name.nil?
60
- $stderr.puts "no command given"
61
- exit 1
62
- end
63
- command = command_named(command_name)
64
- if command.nil?
65
- $stderr.puts "no such command: #{command_name}"
66
- exit 1
67
- end
68
-
69
- # Parse arguments
70
- option_definitions = command.option_definitions + global_option_definitions
71
- begin
72
- parsed_arguments = Cri::OptionParser.parse(opts_and_args_after_command, option_definitions)
73
- rescue Cri::OptionParser::IllegalOptionError => e
74
- $stderr.puts "illegal option -- #{e}"
75
- exit 1
76
- rescue Cri::OptionParser::OptionRequiresAnArgumentError => e
77
- $stderr.puts "option requires an argument -- #{e}"
78
- exit 1
79
- end
80
-
81
- # Handle global options
82
- global_options = global_option_definitions.map { |o| o[:long] }
83
- global_options.delete_if { |o| !parsed_arguments[:options].keys.include?(o.to_sym) }
84
- global_options.each { |o| handle_option(o.to_sym) }
85
-
86
- if parsed_arguments[:options].has_key?(:help)
87
- # Show help for this command
88
- show_help(command)
89
- else
90
- # Run command
91
- command.run(parsed_arguments[:options], parsed_arguments[:arguments])
92
- end
93
- end
94
-
95
- # Returns the command with the given name.
96
- def command_named(name)
97
- # Find by exact name or alias
98
- command = @commands.find { |c| c.name == name or c.aliases.include?(name) }
99
- return command unless command.nil?
100
-
101
- # Find by approximation
102
- commands = @commands.select { |c| c.name[0, name.length] == name }
103
- if commands.length > 1
104
- $stderr.puts "#{@tool_name}: '#{name}' is ambiguous:"
105
- $stderr.puts " #{commands.map { |c| c.name }.join(' ') }"
106
- exit 1
107
- elsif commands.length == 0
108
- $stderr.puts "#{@tool_name}: unknown command '#{name}'\n"
109
- show_help
110
- exit 1
111
- else
112
- return commands[0]
113
- end
114
- end
115
-
116
- # Shows the help text for the given command, or shows the general help
117
- # text if no command is given.
118
- def show_help(command=nil)
119
- if command.nil?
120
- @help_command.run([], [])
121
- else
122
- @help_command.run([], [ command.name ])
123
- end
124
- end
125
-
126
- # Returns the list of global option definitions.
127
- def global_option_definitions
128
- []
129
- end
130
-
131
- # Adds the given command to the list of commands. Adding a command will
132
- # also cause the command's +base+ to be set to this instance.
133
- def add_command(command)
134
- @commands << command
135
- command.base = self
136
- end
137
-
138
- # Handles the given option.
139
- def handle_option(option)
140
- false
141
- end
142
-
143
- private
144
-
145
- # Returns true if the given string is an option (i.e. -foo or --foo),
146
- # false otherwise.
147
- def is_option?(string)
148
- string =~ /^-/
149
- end
150
-
151
- end
152
-
153
- end