class Argy::Command

Overview

A Command represents a single verb in the CLI hierarchy.

Example:

root = Argy::Command.new( use: "mytool", short: "A fictional dev tool", long: "mytool is a fictional dev tool used to illustrate argy." )

serve = Argy::Command.new( use: "serve [flags]", short: "Start the HTTP server" ) serve.on_run do |cmd, _args| port = cmd.int_flag("port") puts "Serving on port #{port}" end serve.flags.int("port", 'p', 8080, "port to listen on")

root.add_command(serve) root.execute

Defined in:

argy/command.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(use : String, short : String = "", long : String = "") #

[View source]

Instance Method Detail

def add_command(*cmds : Command) : Nil #

[View source]
def bool_flag(name : String) : Bool #

[View source]
def execute(argv : Array(String) = ARGV.to_a) : Nil #

Call this on the root command to parse ARGV and dispatch. If the first token equals this command's own name (e.g. the user passed the program name explicitly), it is stripped before routing so that myapp myapp subcommand and myapp subcommand behave identically.


[View source]
def flags : FlagSet #

Local flags — only available to this command


[View source]
def float_flag(name : String) : Float64 #

[View source]
def int_flag(name : String) : Int32 #

[View source]
def long : String #

Long description shown at the top of this command's help page


[View source]
def long=(long : String) #

Long description shown at the top of this command's help page


[View source]
def name : String #

The first word of use — the canonical command name used for routing


[View source]
def on_persistent_pre_run(&block : Command, Array(String) -> ) : self #

Register a callback run from root to current command on every path.


[View source]
def on_pre_run(&block : Command, Array(String) -> ) : self #

Register a callback run before this command's body.


[View source]
def on_run(&block : Command, Array(String) -> ) : self #

Register the command body callback.


[View source]
def persistent_flags : FlagSet #

Persistent flags — inherited by all subcommands


[View source]
def print_help(io : IO = STDOUT) : Nil #

[View source]
def short : String #

One-line description shown in parent's help listing


[View source]
def short=(short : String) #

One-line description shown in parent's help listing


[View source]
def string_flag(name : String) : String #

[View source]
def subcommands : Hash(String, Command) #

[View source]
def use : String #

The usage line – first word is treated as the command name. e.g. "serve [flags]" or just "serve"


[View source]