abstract class Admiral::Command

Defined in:

admiral/command.cr
admiral/command/argument.cr
admiral/command/flag.cr
admiral/command/help.cr
admiral/command/rescue.cr
admiral/command/runner.cr
admiral/command/sub_command.cr
admiral/command/version.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(string : String, program_name = PROGRAM_NAME, input = STDIN, output = STDOUT, error = STDERR, parent : Admiral::Command | Nil = nil) #

Initializes a command with a String, which will be split into arguments.


[View source]
def self.new(argv : Array(String) = ::ARGV.clone, program_name = PROGRAM_NAME, input = STDIN, output = STDOUT, error = STDERR, parent : Admiral::Command | Nil = nil) #

Initializes a command with an Array(String) of arguments.


[View source]
def self.new(argv, program_name, input = nil, output = nil, error = nil, parent : Admiral::Command | Nil = nil) #

Initializes a command with an Admiral::ArgumentList.


[View source]

Class Method Detail

def self.expand_short_flags(argv : Admiral::ArgumentList) #

[View source]

Instance Method Detail

abstract def arguments #

Returns the commands Arguments object.

You can access names arguments by name. You can also access the remaning arguments using .arguments[index].


[View source]
def error(*args) #

Puts to the command's error IO.


[View source]
abstract def flags #

Returns the commands Flags object.

You can access names flags by name.


[View source]
def gets(*args) #

Gets from the command's input IO.


[View source]
def panic(*args) #

[View source]
def parent #

Returns the parent command if one is specified, or returns an error.


[View source]
def print(*args) #

Prints to the command's output IO.


[View source]
def print_error(*args) #

Prints to the command's error IO.


[View source]
def program_name : String #

Returns the commands program name.


[View source]
def puts(*args) #

Puts to the command's output IO.


[View source]
abstract def run #

The run command.


[View source]
abstract def sub(command, *args, **params) #

Invokes a sub command by name, passing self as the parent.


[View source]

Macro Detail

macro define_argument(attr, description = "", default = nil, required = false) #

Defines a named command line argument.

Simple Arguments

Simple arguments are denoted only by a name and will compile to returning a String | Nil.

# hello.cr
class Hello < Admiral::Command
  define_argument planet

  def run
    puts "Hello #{arguments.planet || "World"}"
  end
end

HelloWorld.run
$ crystal build ./world.cr
$ ./hello
Hello World
$ ./hello Alderaan
Hello Alderaan

Typed Arguments

Arguments can also be assigned a type. This will result in a properly typed value when calling arguments.arg_name. By default arguments are not required and will return a Union including the type and Nil.

# hello_world.cr
class HelloWorld < Admiral::Command
  define_argument number_of_greetings : UInt32, default: 1_u32

  def run
    arguments.number_of_greetings.times do
      puts "Hello World"
    end
  end
end

HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world  3
Hello World
Hello World
Hello World

Built in argument types

The following classes are assignable as arguments by default:

  • String
  • Bool
  • Float32
  • Float64
  • Int8
  • Int16
  • Int32
  • Int64
  • UInt8
  • UInt16
  • UInt32
  • UInt64

Pro Tip: To make any Class or Struct assignable as a argument, define a .new(value : ::Admiral::StringValue) or #initialize(value : ::Admiral::StringValue).

Additional Argument Options

# hello_world.cr
class HelloWorld < Admiral::Command
  define_argument number_of_greetings : UInt32,
    description: "The number of times to greet the world", # The description of the argument to be used in auto generated help.
    default: 1_u32,                                        # The default value of the argument.
    required: true                                         # Denotes if a argument is required. Required arguments without a default value will raise an error when not specified at command invocation.

  def run
    arguments.number_of_greetings.times do
      puts "Hello World"
    end
  end
end

HelloWorld.run

Note: Required arguments cannot be defined after optional arguments.


[View source]
macro define_flag(flag, description = "", default = nil, short = nil, long = nil, required = false) #

Defines a command line flag.

Note: When defining flags, the underscore method name will translate to a hyphen on the command line. This can be overridden with the long: my_name option when defining the flag.

Simple Flags

Simple flags are denoted only by a name and will compile to returning a String | Nil.

# hello_world.cr
class HelloWorld < Admiral::Command
  define_flag planet

  def run
    puts "Hello #{flags.planet || "World"}"
  end
end

HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world
Hello World
$ ./hello_world --planet Alderaan
Hello Alderaan

Typed Flags

Flags can also be assigned a type. This will result in a properly typed value when calling flags.flag_name. By default flags are not required and will return a Union including the type and Nil.

# hello_world.cr
class HelloWorld < Admiral::Command
  define_flag number_of_greetings : UInt32, default: 1_u32, long: times

  def run
    flags.times.times do
      puts "Hello World"
    end
  end
end

HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world  --times 3
Hello World
Hello World
Hello World

Built in flag types

The following classes are assignable as flags by default:

  • String
  • Bool
  • Float32
  • Float64
  • Int8
  • Int16
  • Int32
  • Int64
  • UInt8
  • UInt16
  • UInt32
  • UInt64

Pro Tip: To make any Class or Struct assignable as a flag, define a .new(value : ::Admiral::StringValue) or #initialize(value : ::Admiral::StringValue).

Enumerable Flags

Enumerable flags allow for multiple values to be passed on the command line. For example with a defined flag with Array(String) would return an array of String values when calling the flag.

# hello_world.cr
class HelloWorld < Admiral::Command
  define_flag citizens : Array(String), long: citizen

  def run
    flags.citizen.each do |citizen|
      puts "Hello #{citizen}, citizen of Earth!"
    end
  end
end

HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world  --citizen Jim --citizen Harry
Hello Jim, citizen of Earth!
Hello Harry, citizen of Earth!

Additional Flag Options

# hello_world.cr
class HelloWorld < Admiral::Command
  define_flag number_of_greetings : UInt32,
    description: "The number of times to greet the world", # The description of the flag to be used in auto generated help.
    default: 1_u32,                                        # The default value of the flag.
    long: times,                                           # The long version of the flag ex: `long: times` for `--times`.
    short: t,                                              # The short version of the flag ex: `short: t` for `-t`.
    required: true                                         # Denotes if a flag is required. Required flags without a default value will raise an error when not specified at command invocation.

  def run
    flags.number_of_greetings.times do
      puts "Hello World"
    end
  end
end

HelloWorld.run

[View source]
macro define_help(custom, description = "", flag = help, short = nil) #

[View source]
macro define_help(description = "", flag = help, short = nil) #

Adds a help to the command.

# hello.cr
class Hello < Admiral::Command
  define_help description: "A command that says hello"
  define_argument planet, default: "World"

  def run
    puts "Hello #{arguments.planet}"
  end
end
$ crystal build ./hello.cr
$ ./hello --help
Usage:
  ./hello [flags...] <planet> [arg...]

A command that says hello

Flags:
  --help

Arguments:
  planet (default: World)

Custom Help

You can also generate your own custom help text.

# hello.cr
class Hello < Admiral::Command
  define_help custom: "This is the help for my command"

  def run
  end
end

[View source]
macro define_version(string, flag = version, short = nil) #

[View source]
macro register_sub_command(command, type = nil, *, description = nil, short = nil) #

Registers a subcommand.

# hello.cr
class Hello < Admiral::Command
  class Planetary < Admiral::Command
    def run
      puts "Hello World"
    end
  end

  class Municipality < Admiral::Command
    def run
      puts "Hello Denver"
    end
  end

  register_subcommand planet : Planetary
  register_subcommand city : Municipality

  def run
    puts help
  end
end

HelloWorld.run
$ crystal build ./hello.cr
$ ./hello planet
Hello World
$ ./hello city
Hello Denver

[View source]
macro rescue_from(klass, method) #

[View source]
macro rescue_from(klass, &block) #

[View source]