lib/rubikon/application/dsl_methods.rb in rubikon-0.3.0 vs lib/rubikon/application/dsl_methods.rb in rubikon-0.4.0

- old
+ new

@@ -14,30 +14,18 @@ # @author Sebastian Staudt # @see Application::Base # @since 0.3.0 module DSLMethods - private + # @return [String] The (first) definition file of the application + attr_reader :base_file - # Returns the arguments for the currently executed Command - # - # @return [Array] - # @since 0.2.0 - # - # @example - # command :something do - # puts arguments[0] - # end - def args - unless @current_command.nil? - @current_command.arguments - else - @current_global_option.arguments - end - end - alias_method :arguments, :args + # @return [String] The absolute path of the application + attr_reader :path + private + # Define a new application Command or an alias to an existing one # # @param [String, Hash] name The name of the Command as used in # application parameters. This might also be a Hash where every # key will be an alias to the corresponding value, e.g. <tt>{ @@ -48,11 +36,11 @@ # executed when this Command is called, i.e. when the application # is called with the associated parameter # # @return [Command] # @since 0.2.0 - def command(name, description = nil, &block) + def command(name, arg_count = nil, description = nil, &block) if name.is_a? Hash name.each do |alias_name, command_name| command = @commands[command_name] if command.nil? @commands[alias_name] = command_name @@ -60,11 +48,11 @@ command.aliases << alias_name @commands[alias_name] = command end end else - command = Command.new(self, name, &block) + command = Command.new(self, name, arg_count, &block) command.description = description unless description.nil? @commands.each do |command_alias, command_name| if command_name == command.name @commands[command_alias] = command command.aliases << command_alias @@ -73,11 +61,11 @@ @commands[command.name] = command end unless command.nil? || @parameters.empty? @parameters.each do |parameter| - command << parameter + command.add_param(parameter) end @parameters.clear end command @@ -129,11 +117,11 @@ # end def flag(name, &block) if name.is_a? Hash @parameters << name else - @parameters << Flag.new(name, &block) + @parameters << Flag.new(self, name, &block) end end # Checks whether parameter with the given name has been supplied by the # user on the command-line. @@ -142,19 +130,20 @@ # @since 0.2.0 # # @example # flag :status # command :something do - # print_status if given? :status + # print_status if active? :status # end - def given?(name) + def active?(name) name = name.to_sym parameter = @global_parameters[name] parameter = @current_command.parameters[name] if parameter.nil? return false if parameter.nil? parameter.active? end + alias_method :given?, :active? # Create a new flag with the given name to be used globally # # Global flags are not bound to any command and can therefore be used # throughout the application with the same result. @@ -182,11 +171,11 @@ flag.aliases << alias_name @global_parameters[alias_name] = flag end end else - flag = Flag.new(name, &block) + flag = Flag.new(self, name, &block) @global_parameters.each do |flag_alias, flag_name| if flag_name == flag.name @global_parameters[flag_alias] = flag flag.aliases << flag_alias end @@ -223,11 +212,11 @@ option.aliases << alias_name @global_parameters[alias_name] = option end end else - option = Option.new(name, arg_count, &block) + option = Option.new(self, name, arg_count, &block) @global_parameters.each do |option_alias, option_name| if option_name == option.name @global_parameters[option_alias] = option option.aliases << option_alias end @@ -241,11 +230,11 @@ # @param [String, #to_s] prompt A String or other Object responding to # +to_s+ used for displaying a prompt to the user # @since 0.2.0 # # @example Display a prompt "Please type something: " - # action 'interactive' do + # command 'interactive' do # user_provided_value = input 'Please type something' # # # Do something with the data # ... # end @@ -278,11 +267,11 @@ # end def option(name, arg_count = 0, &block) if name.is_a? Hash @parameters << name else - @parameters << Option.new(name.to_s, arg_count, &block) + @parameters << Option.new(self, name.to_s, arg_count, &block) end end # Convenience method for accessing the user-defined output stream # @@ -295,50 +284,48 @@ # ostream.flush def ostream @settings[:ostream] end - # Returns the parameters for the currently executed command + # Defines a block of code used as a hook that should be executed after + # the command execution has finished # - # @return [Array] The parameters of the currently executed command - # @see Command - # @since 0.2.0 - # - # @example - # option :message, 1 - # command :something do - # puts parameters[:message].args[0] if given? :message - # end - def params - @current_command.parameters + # @param [Proc] The code block to execute after the command execution has + # finished + # @since 0.4.0 + def post_execute(&block) + @hooks[:post_execute] = block end - alias_method :parameters, :params - # Output text using +IO#<<+ of the output stream + # Defines a block of code used as a hook that should be executed after + # the application has been initialized # - # @param [String] text The text to write into the output stream - # @since 0.2.0 - def put(text) - @settings[:ostream] << text - @settings[:ostream].flush + # @param [Proc] The code block to execute after the application has been + # initialized + # @since 0.4.0 + def post_init(&block) + @hooks[:post_init] = block end - # Output a character using +IO#putc+ of the output stream + # Defines a block of code used as a hook that should be executed before + # the command has been started # - # @param [String, Numeric] char The character to write into the output - # stream - # @since 0.2.0 - def putc(char) - @settings[:ostream].putc char + # @param [Proc] The code block to execute before the command has been + # started + # @since 0.4.0 + def pre_execute(&block) + @hooks[:pre_execute] = block end - # Output a line of text using +IO#puts+ of the output stream + # Defines a block of code used as a hook that should be executed before + # the application has been initialized # - # @param [String] text The text to write into the output stream - # @since 0.2.0 - def puts(text) - @settings[:ostream].puts text + # @param [Proc] The code block to execute before the application has been + # initialized + # @since 0.4.0 + def pre_init(&block) + @hooks[:pre_init] = block end # Displays a progress bar while the given block is executed # # Inside the block you have access to a instance of ProgressBar. So you @@ -369,9 +356,35 @@ progress = ProgressBar.new(options) block.call(progress) end + end + + # Output text using +IO#<<+ of the output stream + # + # @param [String] text The text to write into the output stream + # @since 0.2.0 + def put(text) + @settings[:ostream] << text + @settings[:ostream].flush + end + + # Output a character using +IO#putc+ of the output stream + # + # @param [String, Numeric] char The character to write into the output + # stream + # @since 0.2.0 + def putc(char) + @settings[:ostream].putc char + end + + # Output a line of text using +IO#puts+ of the output stream + # + # @param [String] text The text to write into the output stream + # @since 0.2.0 + def puts(text) + @settings[:ostream].puts text end # Sets an application setting # # @param [Symbol, #to_sym] setting The name of the setting to change