Class: Ergane::Command
Direct Known Subclasses
Tool
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
aliases, argument, command, flag, option
#dsl_value
included
included
Constructor Details
#initialize(argv = []) ⇒ Command
Returns a new instance of Command.
105
106
107
108
|
# File 'lib/ergane/command.rb', line 105
def initialize(argv = [])
@options = self.class.build_default_options
@argv = process_arguments(parse_options(argv.dup))
end
|
Instance Attribute Details
Returns the value of attribute options.
99
100
101
|
# File 'lib/ergane/command.rb', line 99
def options
@options
end
|
Class Method Details
.argument_required?(index) ⇒ Boolean
Effective required-ness of the positional argument at index. An explicit DSL ‘required:` (true/false) wins; otherwise it’s derived from the run method’s matching positional parameter: a required parameter (‘run(name)`) means required, while an optional one (`run(name = nil)`) or a splat (`run(*)`) means optional.
34
35
36
37
38
39
40
41
|
# File 'lib/ergane/command.rb', line 34
def argument_required?(index)
defn = argument_definitions[index]
return false unless defn
return defn.required unless defn.required.nil?
param = run_positional_parameters[index]
param ? param.first == :req : false
end
|
.command_name ⇒ Object
17
18
19
|
# File 'lib/ergane/command.rb', line 17
def command_name
@command_name || derive_command_name
end
|
.command_name=(name) ⇒ Object
12
13
14
15
|
# File 'lib/ergane/command.rb', line 12
def command_name=(name)
@command_name = name&.to_sym
register!
end
|
.inherited(subclass) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/ergane/command.rb', line 43
def inherited(subclass)
super
subclass.instance_variable_set(:@option_definitions, option_definitions.dup)
subclass.instance_variable_set(:@argument_definitions, argument_definitions.dup)
subclass.instance_variable_set(:@subcommands, {})
subclass.send(:register!)
end
|
.subcommands ⇒ Object
25
26
27
|
# File 'lib/ergane/command.rb', line 25
def subcommands
@subcommands ||= {}
end
|
21
22
23
|
# File 'lib/ergane/command.rb', line 21
def terms
[command_name, *aliases].compact.uniq
end
|
Instance Method Details
#abbreviate_path(path) ⇒ Object
101
102
103
|
# File 'lib/ergane/command.rb', line 101
def abbreviate_path(path)
Ergane.paths.abbreviate(path)
end
|
110
111
112
|
# File 'lib/ergane/command.rb', line 110
def args
@argv
end
|
#run(*run_args) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/ergane/command.rb', line 114
def run(*run_args)
if self.class.subcommands.any?
$stdout.puts HelpFormatter.new(self.class).format
else
raise AbstractCommand, "#{self.class.name}#run is not implemented"
end
end
|