Method: Thor.subcommand
- Defined in:
- lib/thor.rb
.subcommand(ui_name, subcommand_class, desc: nil) ⇒ nil
Declare a subcommand by providing an UI name and subcommand class.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/thor.rb', line 395 def self.subcommand ui_name, subcommand_class, desc: nil ui_name = ui_name.to_s method_name = ui_name.underscore subcommands << method_name subcommand_class.subcommand_help ui_name subcommand_classes[method_name] = subcommand_class if desc self.desc "#{ ui_name } SUBCOMMAND...", desc end define_method method_name do |*args| args, opts = Thor::Arguments.split(args) invoke_args = [ args, opts, {:invoked_via_subcommand => true, :class_options => } ] invoke_args.unshift "help" if opts.delete("--help") || opts.delete("-h") invoke subcommand_class, *invoke_args end # Sigh... this used to just be `subcommand_class.commands...`, but that # fails when: # # 1. A Thor class is created with commands defined, then # 2. That Thor class is subclassed, then # 3. The *subclass* as added as a {.subcommand}. # # This is because then the commands that need {Command#ancestor_name} # set are not part of {.commands}, only {.all_commands}. # # At the moment, this *seems* like an architectural problem, since # the commands being mutated via {Thor::Command#ancestor_name=} do # not *belong* to `self` - they are simply part of superclass, which # could logically be re-used across through additional sub-classes # by multiple {Thor} *unless* this is not party of the paradigm... # but the paradigm fundametals and constraints are never really laid # out anywhere. # # Anyways, switching to {subcommand_classes.all_commands...} hacks # the problem away for the moment. # subcommand_class.all_commands.each do |_meth, command| command.ancestor_name ||= ui_name end nil end |