Module: Browserctl::Commands::OutputFormat
- Defined in:
- lib/browserctl/commands/output_format.rb
Overview
Resolves and applies the unified ‘–output json,text,silent` flag.
Resolution order: explicit flag value -> ‘BROWSERCTL_OUTPUT` env var -> `text` (default).
Usage from a command:
fmt = OutputFormat.from(flag, ENV)
fmt.emit(payload_hash) { "human readable text" }
Per the v0.13 contract:
text - prints the human-readable block; this is byte-identical to
today's output. For most commands the human block already IS
the JSON payload (legacy CLI shape) so the two collapse.
json - prints the JSON payload via `to_json` (no pretty printing).
silent - prints nothing on stdout. Exit codes still carry the result.
The current format is also exposed as a process-wide default (‘OutputFormat.current`) so that `CliOutput#print_result` and other legacy helpers can consult it without every callsite threading the value through.
Defined Under Namespace
Classes: Formatter, InvalidFormat
Constant Summary collapse
- VALID =
%w[json text silent].freeze
- DEFAULT =
"text"- ENV_VAR =
"BROWSERCTL_OUTPUT"- FLAG =
"--output"
Class Method Summary collapse
-
.current ⇒ Object
Process-wide current format.
- .current=(formatter) ⇒ Object
-
.extract!(args) ⇒ Object
Strip ‘–output VALUE` (or `–output=VALUE`) from `args` in place and return the extracted value (or nil).
-
.from(flag, env = ENV) ⇒ Object
Build a Formatter from an explicit flag value (or nil) and an env hash.
-
.install!(args, env = ENV) ⇒ Object
Convenience: parse the flag out of ‘args`, build a Formatter, set it as current, and return it.
-
.reset! ⇒ Object
Reset to default — for tests.
Class Method Details
.current ⇒ Object
Process-wide current format. Set once by the CLI entry point after parsing the global flag; consulted by helpers that don’t otherwise have a reference to a Formatter (notably ‘CliOutput#print_result`).
121 122 123 |
# File 'lib/browserctl/commands/output_format.rb', line 121 def current @current ||= Formatter.new(DEFAULT) end |
.current=(formatter) ⇒ Object
125 126 127 |
# File 'lib/browserctl/commands/output_format.rb', line 125 def current=(formatter) @current = formatter end |
.extract!(args) ⇒ Object
Strip ‘–output VALUE` (or `–output=VALUE`) from `args` in place and return the extracted value (or nil). Recognises the long form only —there is intentionally no short alias.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/browserctl/commands/output_format.rb', line 98 def extract!(args) i = 0 while i < args.length arg = args[i] if arg == FLAG args.delete_at(i) value = args.delete_at(i) or raise InvalidFormat, "missing value for #{FLAG}" return value elsif arg.is_a?(String) && arg.start_with?("#{FLAG}=") value = arg.split("=", 2)[1] args.delete_at(i) return value else i += 1 end end nil end |
.from(flag, env = ENV) ⇒ Object
Build a Formatter from an explicit flag value (or nil) and an env hash.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/browserctl/commands/output_format.rb', line 84 def from(flag, env = ENV) raw = flag || env[ENV_VAR] || DEFAULT mode = raw.to_s.strip.downcase mode = DEFAULT if mode.empty? unless VALID.include?(mode) raise InvalidFormat, "invalid --output value '#{raw}' (expected one of: #{VALID.join(', ')})" end Formatter.new(mode) end |
.install!(args, env = ENV) ⇒ Object
Convenience: parse the flag out of ‘args`, build a Formatter, set it as current, and return it.
131 132 133 134 135 136 |
# File 'lib/browserctl/commands/output_format.rb', line 131 def install!(args, env = ENV) flag = extract!(args) fmt = from(flag, env) self.current = fmt fmt end |