Files
2026-09-05 13:19:55 -05:00

1.4 KiB

CliGen::Command Macros

TOC

Overview

This page is dedicated to discussing the macros associated & embedded in the CliGen::Command class.

Macros

argument

This macro used in a Command subclass does one thing and well. It helps define an instance variable with valid annotation keys and helps debug any misconfigurations before you experience them via the compiler and/or during runtime.

require "cligen"

@[CliGen::CommandInfo(description: "Thing")]
class MyClass << CliGen::Command
  argument(myvar : String = "abc",
           description: "This var changes the printed string",
           options: %w[ abc def ghi ]
  )
  
  def main
    puts "Chosen string : %s" % @myvar
  end
end

Example: String Argument

The above essentialy expands out to the following

require "cligen"

@[CliGen::CommandInfo(description: "Thing")]
class MyClass << CliGen::Command

  @[CliGen::Argument(description: "This var changes the printed string", options: %w[ abc def ghi ])]
  @myvar : String = "abc"
  
  def main
    puts "Chosen string : %s" % @myvar
  end
end

It simply provides you a user friendly "DSL" to define a (sometimes) extensive annotation that would be a PITA to do by hand.

However, the use of the macro has much more attributed to it than just doing this.

For a more detailed walkthrough & it's required keys go to the argument macro doc.