class
Argy::Command
- Argy::Command
- Reference
- Object
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.crConstructors
Instance Method Summary
- #add_command(*cmds : Command) : Nil
-
#aliases : Array(String)
Alternative names that route to this command
- #bool_flag(name : String) : Bool
-
#execute(argv : Array(String) = ARGV.to_a) : Nil
Call this on the root command to parse ARGV and dispatch.
-
#flags : FlagSet
Local flags — only available to this command
- #float_flag(name : String) : Float64
-
#hidden : Bool
When true, this command is excluded from help listings but still callable
-
#hidden=(hidden : Bool)
When true, this command is excluded from help listings but still callable
- #int_flag(name : String) : Int32
-
#long : String
Long description shown at the top of this command's help page
-
#long=(long : String)
Long description shown at the top of this command's help page
-
#name : String
The first word of use — the canonical command name used for routing
-
#on_persistent_pre_run(&block : Command, Array(String) -> ) : self
Register a callback run from root to current command on every path.
-
#on_pre_run(&block : Command, Array(String) -> ) : self
Register a callback run before this command's body.
-
#on_run(&block : Command, Array(String) -> ) : self
Register the command body callback.
-
#persistent_flags : FlagSet
Persistent flags — inherited by all subcommands
- #print_error(error : Exception, io : IO = stderr) : Nil
- #print_help(io : IO = stdout) : Nil
-
#short : String
One-line description shown in parent's help listing
-
#short=(short : String)
One-line description shown in parent's help listing
-
#stderr : IO
The stream this command writes errors to (own override, else inherited, else the process
STDERR). -
#stderr=(io : IO) : IO
Override this command's (and, by inheritance, its descendants') stderr.
-
#stdout : IO
The stream this command prints to (own override, else inherited, else the process
STDOUT). -
#stdout=(io : IO) : IO
Override this command's (and, by inheritance, its descendants') stdout.
- #string_flag(name : String) : String
- #subcommands : Hash(String, Command)
-
#use : String
The usage line – first word is treated as the command name.
Constructor Detail
Instance Method Detail
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.
Register a callback run from root to current command on every path.
Register a callback run before this command's body.
Register the command body callback.
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.
The stream this command writes errors to (own override, else inherited,
else the process STDERR).
The stream this command prints to (own override, else inherited, else
the process STDOUT).
The usage line – first word is treated as the command name. e.g. "serve [flags]" or just "serve"