Class: MCLI::Command
- Inherits:
-
Object
- Object
- MCLI::Command
- Defined in:
- lib/mcli/command.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Option
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
- .call(args) ⇒ Object
- .capture_all! ⇒ Object
- .capture_all? ⇒ Boolean
- .command_name ⇒ Object
- .default_command_group ⇒ Object
- .description(desc = nil) ⇒ Object
- .option(option_name, opts = {}) ⇒ Object
- .options ⇒ Object
- .register_as(command_name, to: default_command_group) ⇒ Object
- .register_as_root(to: default_command_group) ⇒ Object
Instance Method Summary collapse
- #create_parser ⇒ Object
-
#initialize(args = []) ⇒ Command
constructor
A new instance of Command.
- #options ⇒ Object
- #parse ⇒ Object
- #parser ⇒ Object
Constructor Details
#initialize(args = []) ⇒ Command
Returns a new instance of Command.
4 5 6 |
# File 'lib/mcli/command.rb', line 4 def initialize(args=[]) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
2 3 4 |
# File 'lib/mcli/command.rb', line 2 def args @args end |
Class Method Details
.call(args) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/mcli/command.rb', line 87 def call(args) new(args).tap do |command| begin command.parse unless capture_all? command.run rescue MCLI::HelpError command.help end end end |
.capture_all! ⇒ Object
71 72 73 |
# File 'lib/mcli/command.rb', line 71 def capture_all! @capture_all = true end |
.capture_all? ⇒ Boolean
75 76 77 |
# File 'lib/mcli/command.rb', line 75 def capture_all? @capture_all end |
.command_name ⇒ Object
79 80 81 |
# File 'lib/mcli/command.rb', line 79 def command_name @command_name end |
.default_command_group ⇒ Object
83 84 85 |
# File 'lib/mcli/command.rb', line 83 def default_command_group MCLI::CommandGroup end |
.description(desc = nil) ⇒ Object
49 50 51 52 |
# File 'lib/mcli/command.rb', line 49 def description(desc=nil) @description = desc unless desc.nil? @description ||= '' end |
.option(option_name, opts = {}) ⇒ Object
54 55 56 |
# File 'lib/mcli/command.rb', line 54 def option(option_name, opts={}) << Option.new(option_name, opts) end |
.options ⇒ Object
58 59 60 |
# File 'lib/mcli/command.rb', line 58 def @options ||= [] end |
.register_as(command_name, to: default_command_group) ⇒ Object
62 63 64 65 |
# File 'lib/mcli/command.rb', line 62 def register_as(command_name, to: default_command_group) @command_name = command_name to.register(command_name, self) end |
.register_as_root(to: default_command_group) ⇒ Object
67 68 69 |
# File 'lib/mcli/command.rb', line 67 def register_as_root(to: default_command_group) to.register_root(self) end |
Instance Method Details
#create_parser ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/mcli/command.rb', line 38 def create_parser OptionParser.new.tap do |parser| parser.program_name = self.class.command_name parser.on("-h", "--help", "Help") do raise MCLI::HelpError.new end end end |
#options ⇒ Object
30 31 32 |
# File 'lib/mcli/command.rb', line 30 def @options ||= {} end |
#parse ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mcli/command.rb', line 8 def parse @options = {} self.class..each do |option| @options[option.name] = option.default if option.default parser.on(*option.to_args) do |o| @options[option.name] = o end end parser.parse!(@args) self.class. .select { |option| option.required == true } .map do |required_option| if @options[required_option.name].nil? raise OptionParser::MissingArgument end end end |
#parser ⇒ Object
34 35 36 |
# File 'lib/mcli/command.rb', line 34 def parser @parser ||= create_parser end |