Module: Philiprehberger::CliKit

Defined in:
lib/philiprehberger/cli_kit.rb,
lib/philiprehberger/cli_kit/menu.rb,
lib/philiprehberger/cli_kit/parser.rb,
lib/philiprehberger/cli_kit/prompt.rb,
lib/philiprehberger/cli_kit/spinner.rb,
lib/philiprehberger/cli_kit/version.rb,
lib/philiprehberger/cli_kit/colorize.rb

Defined Under Namespace

Modules: Colorize, Menu, Prompt, Spinner Classes: Error, Parser

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.ask(message, error: 'Invalid input, please try again.', input: $stdin, output: $stdout) {|answer| ... } ⇒ String

Display a prompt, re-asking until the answer satisfies the given block.

Parameters:

  • message (String)

    the prompt message

  • error (String) (defaults to: 'Invalid input, please try again.')

    error message shown on invalid input

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Yield Parameters:

  • answer (String)

    the stripped user input

Yield Returns:

  • (Boolean)

    whether the answer is acceptable

Returns:

  • (String)

    the accepted user input



72
73
74
# File 'lib/philiprehberger/cli_kit.rb', line 72

def self.ask(message, error: 'Invalid input, please try again.', input: $stdin, output: $stdout, &block)
  Prompt.ask(message, error: error, input: input, output: output, &block)
end

.bold(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


121
122
123
# File 'lib/philiprehberger/cli_kit.rb', line 121

def self.bold(text)
  Colorize.bold(text)
end

.color(text, name) ⇒ String

Wraps text in ANSI color (auto-disabled when not a TTY or NO_COLOR is set).

Parameters:

  • text (String)
  • name (Symbol)

Returns:

  • (String)


115
116
117
# File 'lib/philiprehberger/cli_kit.rb', line 115

def self.color(text, name)
  Colorize.color(text, name)
end

.confirm(message, input: $stdin, output: $stdout) ⇒ Boolean

Display a yes/no confirmation prompt.

Parameters:

  • message (String)

    the confirmation message

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Returns:

  • (Boolean)

    true if user answered yes



49
50
51
# File 'lib/philiprehberger/cli_kit.rb', line 49

def self.confirm(message, input: $stdin, output: $stdout)
  Prompt.confirm(message, input: input, output: output)
end

.dim(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


127
128
129
# File 'lib/philiprehberger/cli_kit.rb', line 127

def self.dim(text)
  Colorize.dim(text)
end

.multi_select(message, choices, defaults: [], input: $stdin, output: $stdout) ⇒ Array<String>

Present a numbered menu allowing multiple selections and return the selected values.

Parameters:

  • message (String)

    the prompt message

  • choices (Array<String>)

    the list of choices

  • defaults (Array<String>) (defaults to: [])

    pre-selected default choices

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Returns:

  • (Array<String>)

    the selected values



106
107
108
# File 'lib/philiprehberger/cli_kit.rb', line 106

def self.multi_select(message, choices, defaults: [], input: $stdin, output: $stdout)
  Menu.multi_select(message, choices, defaults: defaults, input: input, output: output)
end

.parse(args, output: $stdout) {|Parser| ... } ⇒ Parser

Parse command-line arguments using a DSL block.

Parameters:

  • args (Array<String>)

    command-line arguments

  • output (IO) (defaults to: $stdout)

    output stream for help text (default: $stdout)

Yields:

  • (Parser)

    the parser for defining flags, options, and commands

Returns:

  • (Parser)

    the parsed result with flags, options, and arguments



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/philiprehberger/cli_kit.rb', line 20

def self.parse(args, output: $stdout, &)
  parser = Parser.new
  parser.instance_eval(&)
  parser.parse(args)

  if parser.help_requested?
    output.puts parser.help_text
    exit 0 unless output.is_a?(StringIO)
  end

  parser
end

.password(message, input: $stdin, output: $stdout) ⇒ String

Display a prompt and read input without echoing to the terminal.

Parameters:

  • message (String)

    the prompt message

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Returns:

  • (String)

    the user’s input



59
60
61
# File 'lib/philiprehberger/cli_kit.rb', line 59

def self.password(message, input: $stdin, output: $stdout)
  Prompt.password(message, input: input, output: output)
end

.prompt(message, input: $stdin, output: $stdout) ⇒ String

Display a prompt and read user input.

Parameters:

  • message (String)

    the prompt message

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Returns:

  • (String)

    the user’s input



39
40
41
# File 'lib/philiprehberger/cli_kit.rb', line 39

def self.prompt(message, input: $stdin, output: $stdout)
  Prompt.prompt(message, input: input, output: output)
end

.select(message, choices, default: nil, input: $stdin, output: $stdout) ⇒ String

Present a numbered menu and return the selected value.

Parameters:

  • message (String)

    the prompt message

  • choices (Array<String>)

    the list of choices

  • default (String, nil) (defaults to: nil)

    pre-selected default choice

  • input (IO) (defaults to: $stdin)

    input stream

  • output (IO) (defaults to: $stdout)

    output stream

Returns:

  • (String)

    the selected value



94
95
96
# File 'lib/philiprehberger/cli_kit.rb', line 94

def self.select(message, choices, default: nil, input: $stdin, output: $stdout)
  Menu.select(message, choices, default: default, input: input, output: output)
end

.spinner(message, output: $stderr) { ... } ⇒ Object

Display a spinner while executing a block.

Parameters:

  • message (String)

    the spinner message

  • output (IO) (defaults to: $stderr)

    output stream

Yields:

  • the block to execute

Returns:

  • (Object)

    the return value of the block



82
83
84
# File 'lib/philiprehberger/cli_kit.rb', line 82

def self.spinner(message, output: $stderr, &block)
  Spinner.spinner(message, output: output, &block)
end