Class: CommandKit::Command

Inherits:
Object
  • Object
show all
Includes:
Arguments, Description, Env, Examples, ExceptionHandler, FileUtils, Help, Main, Options, Printing, Stdio, Usage
Defined in:
lib/command_kit/command.rb

Overview

Note:

Command classes are not required to inherit from Command. If you do not wish to inherit from the Command base class, you can instead include the individual modules. This class only exists as a convenience.

The command class base-class.

Examples

class MyCmd < CommandKit::Command

  usage '[OPTIONS] [-o OUTPUT] FILE'

  option :count, short: '-c',
                 value: {
                   type: Integer,
                   default: 1
                 },
                 desc: "Number of times"

  option :output, short: '-o',
                  value: {
                    type: String,
                    usage: 'FILE'
                  },
                  desc: "Optional output file"

  argument :file, required: true,
                  usage: 'FILE',
                  desc: "Input file"

  examples [
    '-o path/to/output.txt path/to/input.txt',
    '-v -c 2 -o path/to/output.txt path/to/input.txt',
  ]

  description 'Example command'

  def run(*files)
    # ...
  end
end

initialize and using instance variables

option :verbose, short: '-v', desc: "Increase verbose level" do
  @verbose += 1
end

def initialize(**kwargs)
  super(**kwargs)

  @verbose = 0
end

Constant Summary

Constants included from Printing

Printing::EOL

Instance Attribute Summary

Attributes included from CommandName

#command_name

Attributes included from Options

#options

Attributes included from Options::Parser

#option_parser

Attributes included from Env

#env

Method Summary

Methods included from FileUtils

#erb

Methods included from ExceptionHandler

#main, #on_exception

Methods included from Printing

#print_error, #print_exception

Methods included from Stdio

#abort, #gets, #initialize, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout

Methods included from Description

#description, #help, #help_description

Methods included from Description::ModuleMethods

#included

Methods included from Help

#help

Methods included from Help::ModuleMethods

#included

Methods included from Examples

#examples, #help, #help_examples

Methods included from Examples::ModuleMethods

#included

Methods included from CommandName

#initialize

Methods included from CommandKit::CommandName::ModuleMethods

#included

Methods included from Options

#help, #initialize

Methods included from Options::ModuleMethods

#included

Methods included from Options::Parser

#help, #help_options, #initialize, #main, #on_ambiguous_argument, #on_ambiguous_option, #on_invalid_argument, #on_invalid_option, #on_missing_argument, #on_needless_argument, #on_parse_error, #parse_options

Methods included from Options::Parser::ModuleMethods

#included

Methods included from Main

#main, #run

Methods included from Main::ModuleMethods

#included

Methods included from Usage

#help, #help_usage, #usage

Methods included from Usage::ModuleMethods

#included

Methods included from Arguments

#help, #help_arguments, #main

Methods included from Arguments::ModuleMethods

#included

Methods included from Env

#initialize