Kebab::Parseable
Mix into a struct to parse it from command-line arguments.
Annotate the struct with Kebab::Command, its fields with Kebab::Option,
Kebab::Argument, or Kebab::Subcommand, then call .parse or .run.
@[Kebab::Command(summary: "Greet someone")]
struct Greet
include Kebab::Parseable
@[Kebab::Argument(description: "Name to greet")]
getter name : String
@[Kebab::Option(short: 'l', description: "Make it loud")]
getter? loud : Bool = false
end
case result = Greet.parse(["-l", "Ada"])
in Greet then puts result.loud? ? "HELLO #{result.name}" : "Hello #{result.name}"
in Kebab::Help then puts result # the user passed --help
in Kebab::Errors then STDERR.puts result # the input was invalid
end
Parsing and dispatch
.parse never raises. It returns the parsed struct, a Kebab::Help when the
user asked for help, or a Kebab::Errors when the input was invalid, so
Crystal's case ... in makes you handle all three.
For a command that owns its behaviour, define def run and call .run, which
parses, invokes run on success, and writes help and errors for you.
.schema returns the command tree as a Kebab::Schema::Command for help,
completion, and your own tooling.
Instance methods
Macros
Walks type's subcommand subtree and fails compilation if a descendant
command reuses a global: true option's long name or short letter. The
declaring command swallows it everywhere in its subtree, so the descendant
could never receive its own copy.
Counts one more occurrence, saturating at the type's maximum so a flood of
flags (-vvv...) can never overflow a narrow int.
One occurrence of a valued option: consume its values, convert them, and
store them on value. Shared by the long and short parsing branches.