feat(cli): add --force-color flag to enforce colored output #1030
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.
This addresses issue #1031.
When calling the CDK CLI programmatically (e.g., from a subprocess), colors are automatically disabled because stdout isn't a TTY. This makes it difficult to capture colored output for tools that want to preserve formatting.
I've added a --force-color flag that accepts a color level (1 for 16 colors, 2 for 256 colors, 3 for 16 million colors). If you pass --force-color without a value, it defaults to level 1. The --no-color flag takes priority if both are specified.
I also fixed a subtle bug where the existing --no-color flag wasn't actually being used - the color disabling was happening at module load time before arguments were even parsed. Now both flags work correctly and respect the priority order: --no-color > --force-color > TTY detection.
Tested locally and added unit tests covering all the new behavior.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license