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.



134
135
136
# File 'lib/arg-parser/dsl.rb', line 134

def self.included(base)
    base.extend(ClassMethods)
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.



140
141
142
# File 'lib/arg-parser/dsl.rb', line 140

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.



147
148
149
# File 'lib/arg-parser/dsl.rb', line 147

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.



154
155
156
# File 'lib/arg-parser/dsl.rb', line 154

def parse_errors
    args_def.errors
end

#show_help(*args) ⇒ Object

Outputs detailed help about available arguments.



178
179
180
# File 'lib/arg-parser/dsl.rb', line 178

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

#show_help?Boolean

Whether help should be displayed.

Returns:

  • (Boolean)


166
167
168
# File 'lib/arg-parser/dsl.rb', line 166

def show_help?
    args_def.show_help?
end

#show_usage(*args) ⇒ Object

Outputs brief usgae details.



172
173
174
# File 'lib/arg-parser/dsl.rb', line 172

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

#show_usage?Boolean

Whether usage information should be displayed.

Returns:

  • (Boolean)


160
161
162
# File 'lib/arg-parser/dsl.rb', line 160

def show_usage?
    args_def.show_usage?
end