Module: Space::Core::CLI

Defined in:
lib/space_core/cli/new.rb,
lib/space_core/cli/use.rb,
lib/space_core/cli/help.rb,
lib/space_core/cli/init.rb,
lib/space_core/cli/list.rb,
lib/space_core/cli/path.rb,
lib/space_core/cli/repo.rb,
lib/space_core/cli/show.rb,
lib/space_core/cli/shell.rb,
lib/space_core/cli/config.rb,
lib/space_core/cli/status.rb,
lib/space_core/cli/current.rb,
lib/space_core/cli/helpers.rb,
lib/space_core/cli/base_command.rb,
lib/space_core/cli.rb

Defined Under Namespace

Modules: Config, Help, Helpers, Registry, Repo, Shell Classes: BaseCommand, Current, Init, List, New, Outcome, Path, RepoProgress, Show, Status, Use

Constant Summary collapse

CLI =
self
TOP_LEVEL_HELP =
[[], ["--help"], ["-h"], ["help"]].freeze
VERSION_REQUEST =
[["version"], ["--version"]].freeze

Class Method Summary collapse

Class Method Details

.call(argv, out = $stdout, err = $stderr) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/space_core/cli.rb', line 56

def self.call(argv, out = $stdout, err = $stderr)
  Thread.current[:space_core_cli_outcome] = nil
  self.help_pastel = Pastel.new(enabled: help_colors?(argv, out, err))

  if TOP_LEVEL_HELP.include?(argv)
    print_usage(out)
    return 0
  end

  if VERSION_REQUEST.include?(argv)
    print_version(out)
    return 0
  end

  Dry::CLI.new(Registry).call(arguments: normalize_args(argv), out: out, err: err)
  last_outcome&.exit_code || 0
end

.color_mode(argv) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/space_core/cli.rb', line 41

def self.color_mode(argv)
  argv.each_with_index do |arg, i|
    return arg.split("=", 2)[1].to_s.downcase if arg.start_with?("--color=", "--colors=")
    return argv[i + 1].to_s.downcase if %w[--color --colors].include?(arg)
  end
  "auto"
end

.help_colors?(argv, out, err) ⇒ Boolean

Whether the help listing should be colourised: honour an explicit –color/–colors (always/never), otherwise auto-detect from the streams the listing can land on (stdout for top-level help, stderr for bare namespaces).

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/space_core/cli.rb', line 31

def self.help_colors?(argv, out, err)
  case color_mode(argv)
  when "always" then true
  when "never"  then false
  else tty?(out) || tty?(err)
  end
end

.help_pastelObject

Pastel used by the colourful help listing (Help / the Usage reopen). Set per-invocation in .call from the output stream and –color; defaults to a disabled instance so non-CLI callers and tests render plain text.



22
# File 'lib/space_core/cli.rb', line 22

def self.help_pastel = @help_pastel ||= Pastel.new(enabled: false)

.help_pastel=(pastel) ⇒ Object



24
25
26
# File 'lib/space_core/cli.rb', line 24

def self.help_pastel=(pastel)
  @help_pastel = pastel
end

.last_outcomeObject



17
# File 'lib/space_core/cli.rb', line 17

def self.last_outcome = Thread.current[:space_core_cli_outcome]

.normalize_args(argv) ⇒ Object

Move –color/–colors options to the end of the argument list so dry-cli’s command routing is not confused by options before the subcommand name.

Two passes:

1. Leading: extract two-token form (--color VALUE) and =-form from the
   front while args still look like options.
2. Non-leading: extract =-form (--color=VALUE / --colors=VALUE) from any
   position before the -- separator. The bare two-token form is ambiguous
   with a subcommand name in non-leading position and is left in place.


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/space_core/cli.rb', line 83

def self.normalize_args(argv)
  args = argv.dup
  extracted = []

  # Pass 1: leading two-token and =-form (existing behavior, unchanged)
  while (arg = args.first) && arg != "--" && arg.start_with?("-")
    if %w[--color --colors].include?(arg)
      extracted << args.shift
      extracted << args.shift if args.first && !args.first.start_with?("-")
    elsif arg.start_with?("--color=", "--colors=")
      extracted << args.shift
    else
      break
    end
  end

  # Pass 2: =-form from any non-leading position, stop at --
  sep = args.index("--")
  head = sep ? args[0, sep] : args
  tail = sep ? args[sep..] : []
  mid_color, head = head.partition { |a| a.start_with?("--color=", "--colors=") }
  extracted += mid_color
  args = head + tail

  extracted.empty? ? args : args + extracted
end


110
111
112
# File 'lib/space_core/cli.rb', line 110

def self.print_usage(out)
  out.puts Dry::CLI::Usage.call(Registry.get([]))
end


114
115
116
# File 'lib/space_core/cli.rb', line 114

def self.print_version(out)
  out.puts ::Space::Core::VERSION
end

.record_outcome(o) ⇒ Object



16
# File 'lib/space_core/cli.rb', line 16

def self.record_outcome(o) = (Thread.current[:space_core_cli_outcome] = o)

.run(argv, out = $stdout, err = $stderr) ⇒ Object



118
119
120
121
122
123
# File 'lib/space_core/cli.rb', line 118

def self.run(argv, out = $stdout, err = $stderr)
  Kernel.exit(call(argv, out, err))
rescue Interrupt
  err.puts "interrupted"
  Kernel.exit(130)
end

.tty?(io) ⇒ Boolean

Returns:

  • (Boolean)


39
# File 'lib/space_core/cli.rb', line 39

def self.tty?(io) = io.respond_to?(:tty?) && io.tty?