class

OptionBuilder::Command

Inherits Reference < Object

A CLI command with flags, subcommands, positional args, and hooks.

cmd = OptionBuilder::Command.new("serve", "Start server") do |ctx|
  ctx.flag('p', "port", Int32, default: 3000) { |prt| @port = prt }
  ctx.run { |args| puts "Server starting on port #{@port}" }
end

Constructors

new(name : String, short_description : String = "", aliases : Array(String) = [] of String, hidden : Bool = false, &)
Source

Class methods

piped?

Returns true if STDIN is piped (not a TTY).

Source

Instance methods

after

Runs after the command.

Source
aliases
Source
all_persistent_flags

All persistent flags including from parent chain.

Source
args

Sets a custom argument validator (block form).

Source
args=(validator : Proc(Array(String), Nil))

Sets a custom argument validator (proc form).

Source
array_flag(short : Char | Nil, long : String, type : T.class, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, choices : Array(T) | Nil = nil, required : Bool = false, &block : Array(T) -> ) forall T

Array flag that collects repeated values (e.g. --input a --input b).

Source
before

Runs before the command (after validation).

Source
config_flag(short : Char | Nil, long : String, description : String = "Path to config file", env : String | Nil = nil)

Adds a config file flag. When the user provides a config file path, it is loaded before any other flags are processed. Precedence: programmatic default < config file < env var < CLI flag.

c.config_flag 'c', "config", description: "Path to config file"
Source
count_flag(short : Char | Nil, long : String, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, &block : Int32 -> )

Count flag that increments on each use (e.g. -v -v -v).

Source
custom_flag(short : Char | Nil, long : String, type : T.class, default : T | Nil = nil, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, required : Bool = false, parser : Proc(String, T) = Proc(String, T).new do |s| raise("no parser") end, &block : T -> ) forall T

Custom flag with a user-provided parser block.

Source
default_subcommand(name : String)

When no subcommand is given, run this one instead of showing help.

Source
description
Source
description=(description : String | Nil)
Source
disable_suggestions=(disable_suggestions : Bool)
Source
disable_suggestions?
Source
enum_flag(short : Char | Nil, long : String, type : T.class, default : T | Nil = nil, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, required : Bool = false, &block : T -> ) forall T

Enum flag that maps string values to Crystal enum members.

Source
example(command : String, description : String)
Source
examples
Source
examples=(examples : Array(Example))
Source
execute(args = ARGV)

Executes the command (defaults to ARGV). Prints errors to STDERR and exits 1.

Source
flag(short : Char | Nil, long : String, type : T.class, default : T | Nil = nil, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, choices : Array(T) | Nil = nil, required : Bool = false, &block : T -> ) forall T
Source
flag_group(title : String, *flag_names : String)

Group flags under a custom heading in help output.

Source
flags
Source
flags_exclusive(*flag_names : String)

Only one flag from the group may be set.

Source
generate_completion(shell : Shell) : String

Shell completion script. Accepts Shell enum or string.

Source
generate_completion(shell : String) : String

Shell completion script. Accepts Shell enum or string.

Source
help_string

Formatted help string for this command.

Source
hidden?
Source
long_description
Source
long_description=(long_description : String | Nil)
Source
name
Source
on_error

Custom error handler instead of the default STDERR + exit(1).

Source
parent
Source
parse_and_execute(args : Array(String))

Like execute but raises instead of printing/exiting.

Source
persistent_array_flag(short : Char | Nil, long : String, type : T.class, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, choices : Array(T) | Nil = nil, required : Bool = false, &block : Array(T) -> ) forall T

Persistent array flag inherited by all subcommands.

Source
persistent_count_flag(short : Char | Nil, long : String, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, &block : Int32 -> )

Persistent count flag inherited by all subcommands.

Source
persistent_enum_flag(short : Char | Nil, long : String, type : T.class, default : T | Nil = nil, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, required : Bool = false, &block : T -> ) forall T

Persistent enum flag inherited by all subcommands.

Source
persistent_flag(short : Char | Nil, long : String, type : T.class, default : T | Nil = nil, description : String = "", env : String | Nil = nil, aliases : Array(String) = [] of String, hidden : Bool = false, deprecated : String | Nil = nil, choices : Array(T) | Nil = nil, required : Bool = false, &block : T -> ) forall T
Source
persistent_flags
Source
persistent_post_run

Persistent post-run hook inherited by all subcommands.

Source
persistent_pre_run

Persistent pre-run hook inherited by all subcommands.

Source
positional(name : String, type : T.class, required : Bool = true, description : String = "", &block : T -> ) forall T
Source
positionals
Source
post_run

Runs after the run block. Not inherited by subcommands.

Source
pre_run

Runs before the run block. Not inherited by subcommands.

Source
require_flags_together(*flag_names : String)

If any flag in the group is set, all must be set.

Source
require_one_of(*flag_names : String)

At least one flag from the group must be set.

Source
run

Remaining args not consumed by flags/positionals are passed to the block.

Source
short_description
Source
subcommand(name : String, short_desc : String, aliases : Array(String) = [] of String, hidden : Bool = false, &block : Command -> )
Source
subcommand_group(title : String, *command_names : String)

Group subcommands under a custom heading in help output.

Source
subcommands
Source
suggestions_minimum_distance
Source
suggestions_minimum_distance=(suggestions_minimum_distance : Int32)
Source
validate

Runs after parsing, before execution. Raise CommandError to abort.

Source
version(v : String)
Source
version_string
Source
version_string=(version_string : String | Nil)
Source

Nested types