Module: ArgParser::DSL

Defined in:
lib/arg-parser/dsl.rb

Overview

Namespace for DSL methods that can be imported into a class for defining command-line argument handling.

Examples:

class MyClass
    include ArgParser::DSL

    # These class methods are added by the DSL, and allow us to define
    # the command-line arguments we want our class to handle.
    positional_arg :command, 'The name of the sub-command to run',
        validation: ['process', 'list'] do |arg, val, hsh|
            # On parse, return the argument value as a symbol
            val.intern
        end
    rest_arg :files, 'The file(s) to process'

    def run
        # Parse the command-line arguments, and call the appropriate command
        args = parse_arguments
        send(args.command, *args.files)
    end

    def process(*files)
        ...
    end

    def list(*files)
        ...
    end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Hook used to extend the including class with class methods defined in the DSL ClassMethods module.



169
170
171
172
# File 'lib/arg-parser/dsl.rb', line 169

def self.included(base)
    base.extend(ClassMethods)
    base.title base.class.name
end

Instance Method Details

#args_defDefinition

Returns The arguments Definition object defined on this class.

Returns:

  • (Definition)

    The arguments Definition object defined on this class.



176
177
178
# File 'lib/arg-parser/dsl.rb', line 176

def args_def
    self.class.args_def
end

#parse_arguments(args = ARGV) ⇒ Object

Defines a parse_arguments instance method to be added to classes that include this module. Uses the args_def argument definition stored on on the class to define the arguments to parse.



183
184
185
# File 'lib/arg-parser/dsl.rb', line 183

def parse_arguments(args = ARGV)
    args_def.parse(args)
end

#parse_errorsObject

Defines a parse_errors instance method to be added to classes that include this module.



190
191
192
# File 'lib/arg-parser/dsl.rb', line 190

def parse_errors
    args_def.errors
end

#show_help(*args) ⇒ Object

Outputs detailed help about available arguments.



214
215
216
# File 'lib/arg-parser/dsl.rb', line 214

def show_help(*args)
    args_def.show_help(*args)
end

#show_help?Boolean

Whether help should be displayed.

Returns:

  • (Boolean)


202
203
204
# File 'lib/arg-parser/dsl.rb', line 202

def show_help?
    args_def.show_help?
end

#show_usage(*args) ⇒ Object

Outputs brief usgae details.



208
209
210
# File 'lib/arg-parser/dsl.rb', line 208

def show_usage(*args)
    args_def.show_usage(*args)
end

#show_usage?Boolean

Whether usage information should be displayed.

Returns:

  • (Boolean)


196
197
198
# File 'lib/arg-parser/dsl.rb', line 196

def show_usage?
    args_def.show_usage?
end