module

OptionBuilder

Cobra-inspired CLI builder on top of stdlib OptionParser.

cmd = OptionBuilder::Command.new("myapp", "My application") do |c|
  c.version "1.0.0"
  c.persistent_flag('v', "verbose", Bool, description: "Verbose output") { |v| @verbose = v }

  c.subcommand("serve", "Start server") do |sub|
    sub.flag('p', "port", Int32, default: 3000, env: "PORT") { |p| @port = p }
    sub.run { puts "Server starting on port #{@port}" }
  end
end
cmd.execute

Class methods

arbitrary_args

Accepts any number of args.

Source
command(name : String, short_description : String = "", aliases : Array(String) = [] of String, hidden : Bool = false, &block : Command -> )

Shorthand for Command.new.

Source
exact_args(n : Int32)

Requires exactly n positional args.

Source
match_all(*validators : Proc(Array(String), Nil))

All given validators must pass.

Source
maximum_args(n : Int32)

Requires at most n positional args.

Source
minimum_args(n : Int32)

Requires at least n positional args.

Source
no_args

Rejects any positional args.

Source
piped?

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

Source
range_args(min : Int32, max : Int32)

Requires between min and max positional args (inclusive).

Source

Nested types