Skip to content

CMUX Extension

Jon Kwon edited this page Jun 7, 2017 · 1 revision

How to add or extend commands

Write command class file like $CMUX_HOME/ext/example.rb and store into $CMUX_HOME/ext.

Example 1. Add example command

$CMUX_HOME/ext/example.rb

#!/usr/bin/env ruby

module CMUX
  module Commands
    class Example
      CMD   = 'example'          # command name
      ALIAS = 'exam'             # command alias
      DESC  = 'Example command'  # command description

      extend Commands

      # Regist command.
      reg_cmd(cmd: CMD, alias: ALIAS, desc: DESC)
      
      # Initialize command.
      def initialize(*args)
        @args = *args
        chk_opt
      end

      # Command process.
      def process
        puts "Hello CMUX"
      end

      # Regist and check command option.
      #   See $CMUX_HOME/lib/cmux/utils/opt_parser.rb
      def chk_opt
        opt = Utils::Checker::OptParser.new
        banner = 'Usage: cmux COMMAND [OPTIONS]'
        opt.banner(CMD, ALIAS, banner)
        opt.separator('Options:')
        opt.help_option
        opt.parse
      end
    end
  end
end
$ cmux exam
Hello CMUX

Example 2. Extend example command

$CMUX_HOME/ext/example2.rb

#!/usr/bin/env ruby

module CMUX
  module Commands
    class ExampleForMe < Example
      DESC  = '[For me]Example command'  # command description

      # Regist command.
      reg_cmd(cmd: CMD, alias: ALIAS, desc: DESC)

      # Command process.
      def process
        puts "Hello my CMUX"   # ======> extended
        puts "Hello CMUX"
      end
    end
  end
end
$ cmux exam
Hello my CMUX
Hello CMUX