Module: ArgParser::DSL::ClassMethods

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

Overview

Class methods added when DSL module is included into a class.

Instance Method Summary collapse

Instance Method Details

#args_defObject

Accessor to return a Definition object holding the command-line argument definitions.



40
41
42
# File 'lib/arg-parser/dsl.rb', line 40

def args_def
    @args_def ||= ArgParser::Definition.new
end

#args_defined?Boolean

Returns true if any arguments have been defined

Returns:

  • (Boolean)


46
47
48
# File 'lib/arg-parser/dsl.rb', line 46

def args_defined?
    @args_def && @args_def.args.size > 0
end

#command_arg(key, desc, opts = {}, &block) ⇒ Object

Define a new command argument.



76
77
78
79
80
# File 'lib/arg-parser/dsl.rb', line 76

def command_arg(key, desc, opts = {}, &block)
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    @arg_opts = nil
    args_def.command_arg(key, desc, opts, &block)
end

Sets the copyright notice to be displayed on the help screen or on startup.



70
71
72
# File 'lib/arg-parser/dsl.rb', line 70

def copyright(txt)
    args_def.copyright = txt
end

#define_args(label = nil, &block) ⇒ Object

Define a set of pre-defined arguments for later use



116
117
118
119
120
# File 'lib/arg-parser/dsl.rb', line 116

def define_args(label = nil, &block)
    pre_def_arg_scope = ArgumentScope.new(label || "Predefined args")
    pre_def_arg_scope.instance_eval(&block)
    pre_def_arg_scope
end

#flag_arg(key, desc, opts = {}, &block) ⇒ Object

Define a new flag argument.



100
101
102
103
104
# File 'lib/arg-parser/dsl.rb', line 100

def flag_arg(key, desc, opts = {}, &block)
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    @arg_opts = nil
    args_def.flag_arg(key, desc, opts, &block)
end

#keyword_arg(key, desc, opts = {}, &block) ⇒ Object

Define a new positional argument.



92
93
94
95
96
# File 'lib/arg-parser/dsl.rb', line 92

def keyword_arg(key, desc, opts = {}, &block)
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    @arg_opts = nil
    args_def.keyword_arg(key, desc, opts, &block)
end

#positional_arg(key, desc, opts = {}, &block) ⇒ Object

Define a new positional argument.



84
85
86
87
88
# File 'lib/arg-parser/dsl.rb', line 84

def positional_arg(key, desc, opts = {}, &block)
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    @arg_opts = nil
    args_def.positional_arg(key, desc, opts, &block)
end

#predefined_arg(lookup_key, desc = nil, opts = {}) ⇒ Object

Use a pre-defined argument.



136
137
138
139
140
141
142
143
144
145
# File 'lib/arg-parser/dsl.rb', line 136

def predefined_arg(lookup_key, desc = nil, opts = {})
    if desc.is_a?(Hash)
        opts = desc
        desc = nil
    end
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    opts[:description] = desc if desc
    @arg_opts = nil
    args_def.predefined_arg(lookup_key, opts)
end

#purpose(desc) ⇒ Object

Sets the descriptive text that describes the purpose of the job represented by this class.



64
65
66
# File 'lib/arg-parser/dsl.rb', line 64

def purpose(desc)
    args_def.purpose = desc
end

#register_parse_handler(key, &block) ⇒ Object

Registers the supplied block as an on_parse handler that can be attached to an argument using key.



52
53
54
# File 'lib/arg-parser/dsl.rb', line 52

def register_parse_handler(key, &block)
    ArgParser::OnParseHandlers[key] = block
end

#require_any_of(*keys) ⇒ Object

Make one or more of the specified arguments mandatory.



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

def require_any_of(*keys)
    args_def.require_any_of(*keys)
end

#require_one_of(*keys) ⇒ Object

Make exactly one of the specified arguments mandatory.



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

def require_one_of(*keys)
    args_def.require_one_of(*keys)
end

#rest_arg(key, desc, opts = {}, &block) ⇒ Object

Define a rest argument.



108
109
110
111
112
# File 'lib/arg-parser/dsl.rb', line 108

def rest_arg(key, desc, opts = {}, &block)
    opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
    @arg_opts = nil
    args_def.rest_arg(key, desc, opts, &block)
end

#title(val) ⇒ Object

Sets the title that will appear in the Usage output generated from the Definition.



58
59
60
# File 'lib/arg-parser/dsl.rb', line 58

def title(val)
    args_def.title = val
end

#usage_break(label) ⇒ Object

Set a label for a usage break to be applied on the next argument that is defined.



149
150
151
# File 'lib/arg-parser/dsl.rb', line 149

def usage_break(label)
    @arg_opts = {usage_break: label}
end

#with_predefined_args(scope, &blk) ⇒ Object

Set the ArgumentScope to use for looking up pre-defined arguments



123
124
125
126
127
128
129
130
131
132
# File 'lib/arg-parser/dsl.rb', line 123

def with_predefined_args(scope, &blk)
    if block_given?
        pre_defs = args_def.predefined_args
        args_def.predefined_args = scope
        yield
        args_def.predefined_args = pre_defs
    else
        args_def.predefined_args = scope
    end
end