Class: BunBun::CLI
- Inherits:
-
Object
show all
- Defined in:
- lib/bunbun/cli.rb
Defined Under Namespace
Classes: Command, Countries, Keys, Purge, Regions, StorageCommand, StorageDelete, StorageDownload, StorageEdit, StorageFiles, StorageUpload, StorageZone, StorageZoneCreate, StorageZones, Zone, ZoneCreate, ZonePurge, ZoneRulesDelete, ZoneRulesDisenable, ZoneRulesEnable, ZoneRulesPost, Zones
Constant Summary
collapse
- Error =
Class.new(StandardError)
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.command(key) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/bunbun/cli.rb', line 15
def self.command(key)
autoload_path = 'bunbun/cli/' + key.join('_')
autoload command_class_name(key), autoload_path
commands << key
end
|
.command_class_name(key) ⇒ Object
23
24
25
|
# File 'lib/bunbun/cli.rb', line 23
def self.command_class_name(key)
key.map(&:capitalize).join.to_sym
end
|
.commands ⇒ Object
11
12
13
|
# File 'lib/bunbun/cli.rb', line 11
def self.commands
@commands ||= []
end
|
Instance Method Details
#call(args) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/bunbun/cli.rb', line 48
def call(args)
if args.empty? || args.size == 1 && args.first == '--help'
print_usage
return
end
option_args, command_args = args.partition { _1.start_with?('-') }
key = self.class.commands.find { command_args.first(_1.length) == _1 }
if key.nil?
error "#{command_args.join(' ')} is not a valid command"
end
command_class = get_command_class(key)
command = command_class.new
if option_parser = command.option_parser
command.options = {}
option_parser.parse(option_args, into: command.options)
end
command_args = command_args.drop(key.length)
if command_args.length < command_class.argument_count
error "not enough arguments (expected #{command_class.argument_count})"
elsif command_args.length > command_class.argument_count
error "too many arguments (expected #{command_class.argument_count})"
end
command.call(*command_args)
rescue OptionParser::InvalidOption
error 'invalid option'
rescue OptionParser::MissingArgument
error 'missing option argument'
rescue BunBun::Error, BunBun::CLI::Error => exception
error exception.message
end
|