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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'0.3.1'

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



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

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

.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



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

def self.confirm(message, input: $stdin, output: $stdout)
  Prompt.confirm(message, input: input, output: output)
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



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

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



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

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



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

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



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

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



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

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



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

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