class Jargon::CLI

Defined in:

jargon/cli.cr
jargon/config.cr
jargon/help.cr

Constructors

Instance Method Summary

Constructor Detail

def self.from_file(path : String, program_name : String = "cli") : CLI #

Create a CLI from a JSON or YAML schema file. Auto-detects multi-doc format for subcommands.


[View source]
def self.from_json(json : String, program_name : String = "cli") : CLI #

Create a CLI from a JSON schema string. Auto-detects multi-doc format (relaxed JSONL) for subcommands.


[View source]
def self.from_yaml(yaml : String, program_name : String = "cli") : CLI #

Create a CLI from a YAML schema string. Auto-detects multi-doc format for subcommands.


[View source]
def self.new(schema : Schema, program_name : String = "cli") #

[View source]
def self.new(program_name : String) #

[View source]

Instance Method Detail

def bash_completion(command : String = @program_name) : String #

Generate a shell completion shim. By default it calls back into this program; pass command: to point completion at a separate (e.g. lighter) binary that answers the hidden __complete verb.


[View source]
def completer(path : String, &block : Completion::Context -> Array(String)) #

Register a dynamic completer for a field, identified by its dotted path: the field name for a flat CLI, or subcommand.field (a.b.field when nested). The block receives a Completion::Context and returns the candidate strings. Raises if the path does not match a schema field, so typos and renames fail fast.


[View source]
def completers : Hash(String, Proc(Completion::Context, Array(String))) #

[View source]
def config_paths : Array(String) #

Returns the list of config paths that would be searched


[View source]
def default_subcommand(name : String) #

[View source]
def default_subcommand : String | Nil #

[View source]
def fish_completion(command : String = @program_name) : String #

[View source]
def handle_completion(args : Array(String) = ARGV, *, io : IO = @output, exit_process : Bool = true) : Bool #

If this invocation is a completion request (the hidden __complete verb), print candidates and — unless exit_process: false — exit 0. Returns true when it handled completion, false otherwise, so it can be called early in a program (before heavy initialization) or as the whole body of a dedicated completion helper binary. The developer never types the verb itself; the generated shim supplies it.


[View source]
def help(subcommand : String) : String #

[View source]
def help : String #

[View source]
def json(args : Array(String), input : IO, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : JSON::Any #

[View source]
def json(args : Array(String) = ARGV, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : JSON::Any #

Return just the parsed data as JSON. Raises ParseError on validation errors.


[View source]
def load_config(*, merge : Bool = true) : JSON::Any | Nil #

Load config from standard locations. Supports YAML (.yaml, .yml) and JSON (.json) files.

Paths searched (in order):

  1. ./.config/{program_name}.yaml/.yml/.json (project local, flat)
  2. ./.config/{program_name}/config.yaml/.yml/.json (project local, directory)
  3. $XDG_CONFIG_HOME/{program_name}.yaml/.yml/.json (user global, flat)
  4. $XDG_CONFIG_HOME/{program_name}/config.yaml/.yml/.json (user global, directory)

With merge: true (default), merges all configs found (project wins over user). With merge: false, returns first config found. Returns nil if no config file found.


[View source]
def output : IO #

[View source]
def output=(output : IO) #

[View source]
def parse(args : Array(String), input : IO, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : Result #

[View source]
def parse(args : Array(String) = ARGV, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : Result #

Parse arguments and return full Result with errors array.


[View source]
def program_name : String #

[View source]
def run(args : Array(String), input : IO, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : Result #

[View source]
def run(args : Array(String) = ARGV, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil) : Result #

Run the CLI with automatic --help, --completions, and error handling. Prints help/completions and exits 0, prints errors and exits 1, otherwise returns/yields result.


[View source]
def run(args : Array(String), input : IO, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil, &) : Nil #

[View source]
def run(args : Array(String) = ARGV, *, defaults : JSON::Any | Hash(String, JSON::Any) | Nil = nil, &) : Nil #

[View source]
def schema : Schema | Nil #

[View source]
def subcommand(name : String, schema : Schema) #

[View source]
def subcommand(name : String, cli : CLI) #

[View source]
def subcommand(name : String, *, json : String) #

[View source]
def subcommand(name : String, *, yaml : String) #

[View source]
def subcommand(name : String, *, file : String) #

Load subcommand(s) from a file.

  • Single-doc file: requires name, loads as single subcommand
  • Multi-doc file without name: loads each doc as top-level subcommand
  • Multi-doc file with name: loads docs as nested subcommands under name

[View source]
def subcommand(*, file : String) #

Load subcommands from a multi-document file (no parent name). Each document must have a "name" field.


[View source]
def subcommand_key(key : String) #

[View source]
def subcommand_key : String #

[View source]
def subcommands : Hash(String, Schema | CLI) #

[View source]
def validate(data : Hash(String, JSON::Any), subcommand : String | Nil = nil) : Array(String) #

Validate data against a schema, returning any errors. If no schema is provided, uses the CLI's root schema. For subcommand validation, pass the subcommand name (space-separated for nested).


[View source]
def validate(result : Result) : Array(String) #

[View source]
def zsh_completion(command : String = @program_name) : String #

[View source]