lib/ctioga2/commands/commands.rb in ctioga2-0.4 vs lib/ctioga2/commands/commands.rb in ctioga2-0.5
- old
+ new
@@ -15,11 +15,11 @@
require 'ctioga2/commands/arguments'
require 'ctioga2/commands/groups'
module CTioga2
- Version::register_svn_info('$Revision: 355 $', '$Date: 2012-12-26 00:19:04 +0100 (Wed, 26 Dec 2012) $')
+ Version::register_svn_info('$Revision: 430 $', '$Date: 2013-08-23 14:23:52 +0200 (Fri, 23 Aug 2013) $')
module Commands
# An exception raised when the command is called with an insufficient
# number of arguments.
@@ -88,10 +88,18 @@
attr_accessor :group
# The context of definition [file, line]
attr_accessor :context
+ # The context of the documentation
+ attr_accessor :documentation_context
+
+ def self.get_calling_context(id=2)
+ caller[id].gsub(/.*\/ctioga2\//, 'lib/ctioga2/') =~ /(.*):(\d+)/
+ return [$1, $2.to_i]
+ end
+
# Creates a Command, with all attributes set up. The code can be
# set using #set_code.
#
# Single and double dashes are stripped from the beginning of the
# short and long options respectively.
@@ -107,12 +115,11 @@
raise "A long option must always be present if a short one is"
end
@code = code
self.describe(d_short, d_long, group)
- caller[1].gsub(/.*\/ctioga2\//, 'lib/ctioga2/') =~ /(.*):(\d+)/
- @context = [$1, $2.to_i]
+ @context = Command.get_calling_context
# Registers automatically the command
if register
Commands::Interpreter.register_command(self)
end
@@ -130,18 +137,28 @@
end
# Sets the descriptions of the command. If the long description
# is ommitted, the short is reused.
def describe(short, long = nil, group = nil)
+ @documentation_context = Command.get_calling_context(1)
@short_description = short
@long_description = long || short
if(group)
@group = group
group.commands << self
end
end
+ # Sets the long documentation of the given command
+ def self.document_command(cmd, desc)
+ tg = Commands::Interpreter.command(cmd)
+ if tg
+ tg.documentation_context = Command.get_calling_context(1)
+ tg.long_description = desc
+ end
+ end
+
# Returns a list of three strings:
# * the short option
# * the long option with arguments
# * the description string
#
@@ -208,40 +225,55 @@
#
# Any object which is not a String is left as is (useful for
# instance for the OptionParser with boolean options)
def convert_options(options)
target_options = {}
+ conv = target_option_names()
for k,v in options
- if ! @optional_arguments.key? k
+ kn = normalize_option_name(k)
+ if ! conv.key? kn
raise CommandOptionUnkown, "Unkown option #{k} for command #{@name}"
end
- opt = @optional_arguments[k]
+ opt = @optional_arguments[conv[kn]]
if v.is_a? String
v = opt.type.string_to_type(v)
end
- target = opt.option_target || k
+ target = opt.option_target || conv[kn]
if opt.option_deprecated
expl = ""
if opt.option_target
expl = " -- please use #{opt.option_target} instead"
+ elsif opt.option_deprecated != true # Ie more than plain
+ # true/false
+ expl = " -- #{opt.option_deprecated}"
end
Log::warn { "Deprecated option #{k}#{expl}" }
end
target_options[target] = v
end
return target_options
end
+ # Returns a hash "normalized option names" => 'real option name'
+ def target_option_names
+ return @tg_op_names if @tg_op_names
+
+ @tg_op_names = {}
+ for k in @optional_arguments.keys
+ @tg_op_names[normalize_option_name(k)] = k
+ end
+ return @tg_op_names
+ end
+
+ # Returns a lowercase
+ def normalize_option_name(opt)
+ return opt.gsub(/_/,"-").downcase
+ end
+
# Whether the Command accepts the named _option_.
- #
- # \todo Several conversions could be used, to facilitate the
- # writing of options:
- #
- # * convert everything to lowercase .
- # * ignore the difference between _ and - (a bit delicate).
def has_option?(option)
- return @optional_arguments.key? option
+ return target_option_names.key?(normalize_option_name(option))
end
# Whether the Command accepts any option at all ?
def has_options?
return !(@optional_arguments.empty?)