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
Class methods
piped?
Returns true if STDIN is piped (not a TTY).
SourceInstance methods
all_persistent_flags
All persistent flags including from parent chain.
Sourceargs
Sets a custom argument validator (block form).
SourceSets a custom argument validator (proc form).
Sourcearray_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).
Sourcebefore
Runs before the command (after validation).
SourceAdds 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"
SourceCount flag that increments on each use (e.g. -v -v -v).
Sourcecustom_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.
Sourcedefault_subcommand(name :
String)
When no subcommand is given, run this one instead of showing help.
Sourcedisable_suggestions=(disable_suggestions :
Bool)
Sourceenum_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.
Sourceexecute(args = ARGV)
Executes the command (defaults to ARGV). Prints errors to STDERR and exits 1.
Sourceflag(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 SourceGroup flags under a custom heading in help output.
Sourceflags_exclusive(*flag_names :
String)
Only one flag from the group may be set.
Sourcegenerate_completion(shell :
Shell) :
String Shell completion script. Accepts Shell enum or string.
SourceShell completion script. Accepts Shell enum or string.
Sourcehelp_string
Formatted help string for this command.
Sourceon_error
Custom error handler instead of the default STDERR + exit(1).
SourceLike execute but raises instead of printing/exiting.
Sourcepersistent_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.
SourcePersistent count flag inherited by all subcommands.
Sourcepersistent_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.
Sourcepersistent_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 Sourcepersistent_post_run
Persistent post-run hook inherited by all subcommands.
Sourcepersistent_pre_run
Persistent pre-run hook inherited by all subcommands.
Sourcepositional(name :
String, type :
T.class, required :
Bool = true, description :
String = "", &block :
T -> ) forall
T Sourcepost_run
Runs after the run block. Not inherited by subcommands.
Sourcepre_run
Runs before the run block. Not inherited by subcommands.
Sourcerequire_flags_together(*flag_names :
String)
If any flag in the group is set, all must be set.
Sourcerequire_one_of(*flag_names :
String)
At least one flag from the group must be set.
Sourcerun
Remaining args not consumed by flags/positionals are passed to the block.
SourceGroup subcommands under a custom heading in help output.
Sourcesuggestions_minimum_distance
Sourcesuggestions_minimum_distance=(suggestions_minimum_distance :
Int32)
Sourcevalidate
Runs after parsing, before execution. Raise CommandError to abort.
Source