-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add TOON usage output #3021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,117 @@ | ||
| import Commander | ||
| import Foundation | ||
|
|
||
| struct ResolvedOutputFormat { | ||
| let format: OutputFormat | ||
| let toonRequested: Bool | ||
| } | ||
|
|
||
| struct CLIOutputPreferences { | ||
| let format: OutputFormat | ||
| let jsonOnly: Bool | ||
| let pretty: Bool | ||
| /// Set only by `usage --format toon`. TOON piggybacks on the JSON fetch/render pipeline (`format` | ||
| /// stays `.json` so credits/color/account behavior matches), but error/exit payloads must still | ||
| /// render as TOON rather than JSON so callers parsing `--format toon` see a consistent format. | ||
| var toonRequested: Bool = false | ||
|
|
||
| var usesJSONOutput: Bool { | ||
| self.jsonOnly || self.format == .json | ||
| } | ||
|
|
||
| static func from(values: ParsedValues) -> CLIOutputPreferences { | ||
| /// TOON is a `usage`-only contract. Every other command's `--format` help promises `text | json`, | ||
| /// so they must keep the legacy decoder, where an unrecognized value falls through to the | ||
| /// text/JSON default instead of silently selecting JSON. | ||
| static func from(values: ParsedValues, allowsToon: Bool = false) -> CLIOutputPreferences { | ||
| let jsonOnly = values.flags.contains("jsonOnly") | ||
| let format = CodexBarCLI.decodeFormat(from: values) | ||
| let pretty = values.flags.contains("pretty") | ||
| return CLIOutputPreferences(format: format, jsonOnly: jsonOnly, pretty: pretty) | ||
| let resolved = Self.resolveOutputFormat(from: values, allowsToon: allowsToon) | ||
| return CLIOutputPreferences( | ||
| format: resolved.format, | ||
| jsonOnly: jsonOnly, | ||
| pretty: pretty, | ||
| toonRequested: resolved.toonRequested) | ||
| } | ||
|
|
||
| static func from(argv: [String]) -> CLIOutputPreferences { | ||
| var jsonOnly = false | ||
| var pretty = false | ||
| var format: OutputFormat = .text | ||
| var lastExplicitFormat: String? | ||
| var jsonShortcut = false | ||
|
|
||
| var index = 0 | ||
| while index < argv.count { | ||
| let arg = argv[index] | ||
| switch arg { | ||
| case "--json-only": | ||
| jsonOnly = true | ||
| format = .json | ||
| jsonShortcut = true | ||
| case "--json": | ||
| format = .json | ||
| jsonShortcut = true | ||
| case "--pretty": | ||
| pretty = true | ||
| case "--format": | ||
| let next = index + 1 | ||
| if next < argv.count, let parsed = OutputFormat(argument: argv[next]) { | ||
| format = parsed | ||
| if next < argv.count { | ||
| lastExplicitFormat = argv[next] | ||
| index += 1 | ||
| } | ||
| default: | ||
| break | ||
| if arg.hasPrefix("--format="), arg != "--format=" { | ||
| lastExplicitFormat = String(arg.dropFirst("--format=".count)) | ||
| } | ||
| } | ||
| index += 1 | ||
| } | ||
|
|
||
| return CLIOutputPreferences(format: format, jsonOnly: jsonOnly, pretty: pretty) | ||
| let resolved = Self.resolveOutputFormat( | ||
| lastExplicitFormat: lastExplicitFormat, | ||
| jsonShortcut: jsonShortcut || jsonOnly, | ||
| allowsToon: Self.commandSupportsToon(argv: argv)) | ||
| return CLIOutputPreferences( | ||
| format: resolved.format, | ||
| jsonOnly: jsonOnly, | ||
| pretty: pretty, | ||
| toonRequested: resolved.toonRequested) | ||
| } | ||
|
|
||
| /// Mirrors `CodexBarCLI.effectiveArgv`: a bare `codexbar --format toon` runs the implicit `usage` | ||
| /// command, so the argv bootstrap scanner has to reach the same verdict as the post-parse path. | ||
| static func commandSupportsToon(argv: [String]) -> Bool { | ||
| guard let first = argv.first else { return true } | ||
| if first.hasPrefix("-") { return true } | ||
| return first == "usage" | ||
| } | ||
|
|
||
| /// Explicit `--format` wins over `--json` / `--json-only`, matching `decodeFormat(from:)`. | ||
| /// TOON is recognized only via `usage --format toon` and piggybacks on the JSON pipeline; for | ||
| /// every other command `toon` stays an unrecognized value, exactly as before TOON existed. | ||
| static func resolveOutputFormat(from values: ParsedValues, allowsToon: Bool = false) -> ResolvedOutputFormat { | ||
| let jsonShortcut = values.flags.contains("jsonShortcut") | ||
| || values.flags.contains("json") | ||
| || values.flags.contains("jsonOnly") | ||
| return Self.resolveOutputFormat( | ||
| lastExplicitFormat: values.options["format"]?.last, | ||
| jsonShortcut: jsonShortcut, | ||
| allowsToon: allowsToon) | ||
| } | ||
|
|
||
| static func resolveOutputFormat( | ||
| lastExplicitFormat: String?, | ||
| jsonShortcut: Bool, | ||
| allowsToon: Bool = false) -> ResolvedOutputFormat | ||
| { | ||
| if let raw = lastExplicitFormat { | ||
| if allowsToon, raw.lowercased() == "toon" { | ||
| return ResolvedOutputFormat(format: .json, toonRequested: true) | ||
| } | ||
| if let parsed = OutputFormat(argument: raw) { | ||
| return ResolvedOutputFormat(format: parsed, toonRequested: false) | ||
| } | ||
| } | ||
| if jsonShortcut { | ||
| return ResolvedOutputFormat(format: .json, toonRequested: false) | ||
| } | ||
| return ResolvedOutputFormat(format: .text, toonRequested: false) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.