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 = "", aliases : Array(String) = [] of String, hidden : Bool = false) #

[View source]

Instance Method Detail

def add_command(*cmds : Command) : Nil #

[View source]
def aliases : Array(String) #

Alternative names that route to this command


[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 hidden : Bool #

When true, this command is excluded from help listings but still callable


[View source]
def hidden=(hidden : Bool) #

When true, this command is excluded from help listings but still callable


[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_error(error : Exception, io : IO = stderr) : Nil #

Renders the standard "Error: … / Run '… --help' for usage." diagnostic for a failed #execute to io (this command's #stderr by default). Public so callers that handle their own errors can reuse argy's formatting.


[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 stderr : IO #

The stream this command writes errors to (own override, else inherited, else the process STDERR).


[View source]
def stderr=(io : IO) : IO #

Override this command's (and, by inheritance, its descendants') stderr.


[View source]
def stdout : IO #

The stream this command prints to (own override, else inherited, else the process STDOUT).


[View source]
def stdout=(io : IO) : IO #

Override this command's (and, by inheritance, its descendants') stdout.


[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]