1
# introspection.rb: get informations about what is known to ctioga2
2
# copyright (c) 2009 by Vincent Fourmond
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details (in the COPYING file).
14
require 'ctioga2/utils'
15
require 'ctioga2/commands/commands'
19
Version::register_svn_info('$Revision: 238 $', '$Date: 2011-01-23 21:58:08 +0100 (Sun, 23 Jan 2011) $')
25
# This class provides facilities to display information
28
# Display all known commands, along with their definition place
29
def list_commands(format = :pretty)
30
cmds = Interpreter::commands
31
names = cmds.keys.sort
44
command['line'] = l.to_i
45
command['long_option'] = cmd.long_option
46
command['short_option'] = cmd.short_option
47
command['short_description'] = cmd.short_description
48
command['long_description'] = cmd.long_description
51
puts YAML.dump(commands)
53
puts "Known commands:"
54
max = names.inject(0) {|m,x| [m,x.size].max}
55
max2 = names.inject(0) {|m,x| [m,cmds[x].long_option.size].max}
58
puts "\t%-#{max}s\t--%-#{max2}s\t(#{f}: #{l})" %
59
[n, cmds[n].long_option ]
65
def list_groups(raw = false)
66
puts "Known groups:" unless raw
67
groups = Interpreter::groups
68
names = groups.keys.sort
73
f,l = groups[n].context
74
puts "\t#{n}\t(#{f}: #{l})"
80
def list_types(raw = false)
81
puts "Known types:" unless raw
82
types = Interpreter::types
83
names = types.keys.sort
88
f,l = types[n].context
89
puts "\t#{n}\t(#{f}: #{l})"
94
# Lauches an editor to edit the given command:
96
cmd = Interpreter::command(cmd)
98
edit_file(*cmd.context)
102
# Lauches an editor to edit the given command:
103
def edit_group(group)
104
group = Interpreter::group(group)
106
edit_file(*group.context)
110
# Lauches an editor to edit the given command:
112
type = Interpreter::type(type)
114
edit_file(*type.context)
121
# Launches an editor to edit the given file at the given place.
122
def edit_file(file, line)
123
editor = ENV['EDITOR'] || 'emacs'
124
if ENV['CT2_DEV_HOME']
125
file = "#{ENV['CT2_DEV_HOME']}/#{file}"
127
system("#{editor} +#{line} #{file} &")
133
/list|raw/i => :list,
134
/default|pretty/i => :pretty,
139
InternalFormatType = CmdType.new('internal-format',
141
:list => InternalFormatRE}, <<EOD)
142
Output format for internals.
147
CmdGroup.new('introspection', "Introspection",
149
Commands displaying information about the internals of ctioga2, such
150
as known types/commands/backends...
153
TypeOption = {'format' => CmdArg.new('internal-format')}
154
RawOption = {'raw' => CmdArg.new('boolean')}
157
Cmd.new('list-commands', nil, '--list-commands',
158
[], RawOption.dup.update(TypeOption)) do |p, opts|
159
opts['format'] = :list if opts['raw']
161
Introspection.new.list_commands(opts['format'])
164
ListCommandsCmd.describe("List known commands",
165
<<EOH, IntrospectionGroup)
166
List all commands known to ctioga2
170
Cmd.new('list-groups', nil, '--list-groups',
171
[], RawOption) do |p, opts|
172
Introspection.new.list_groups(opts['raw'])
175
ListGroupsCmd.describe("List known groups",
176
<<EOH, IntrospectionGroup)
177
List all command groups known to ctioga2
181
Cmd.new('list-types', nil, '--list-types',
182
[], RawOption) do |p, opts|
183
Introspection.new.list_types(opts['raw'])
186
ListTypesCmd.describe("List known types",
187
<<EOH, IntrospectionGroup)
188
List all types known to ctioga2
192
Cmd.new('edit-command', nil, '--edit-command',
193
[ CmdArg.new('text')]) do |plotmaker, cmd|
194
Introspection.new.edit_command(cmd)
197
EditCommandCmd.describe("Edit the command",
198
<<EOH, IntrospectionGroup)
199
Edit the given command in an editor. It will only work from the
200
top directory of a ctioga2 source tree.
204
Cmd.new('edit-group', nil, '--edit-group',
205
[ CmdArg.new('text')]) do |plotmaker, cmd|
206
Introspection.new.edit_group(cmd)
209
EditGroupCmd.describe("Edit the group",
210
<<EOH, IntrospectionGroup)
211
Edit the given group in an editor. It will only work from the
212
top directory of a ctioga2 source tree.
216
Cmd.new('edit-type', nil, '--edit-type',
217
[ CmdArg.new('text')]) do |plotmaker, cmd|
218
Introspection.new.edit_type(cmd)
221
EditTypeCmd.describe("Edit the type",
222
<<EOH, IntrospectionGroup)
223
Edit the given type in an editor. It will only work from the
224
top directory of a ctioga2 source tree.
228
Cmd.new('version-raw', nil, '--version-raw',
230
print Version::version
233
VersionRawCmd.describe("Raw version",
234
<<EOH, IntrospectionGroup)
235
Prints the raw version number, without any other decoration and