Skip to content

Commit 6c28a2b

Browse files
authored
feat(cli): support multiple output formats via --formats flag (#130)
* chore: update rubocop todo configuration Update rubocop todo file with latest offense counts and configurations * Apply suggestions from code review * fix(cli): update help text to show default output format Add test case to verify default console format when no --formats option is provided * fix(cli): wrap formats option * fix(cli): reset config before parse * fix(deps): pin path_expander (<2.0) for Ruby 2.7 (endless method defs)
1 parent ff8b0f8 commit 6c28a2b

File tree

8 files changed

+96
-33
lines changed

8 files changed

+96
-33
lines changed

.rubocop_todo.yml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-10-17 22:53:06 UTC using RuboCop version 1.81.1.
3+
# on 2025-11-14 17:50:37 UTC using RuboCop version 1.81.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -24,21 +24,21 @@ Lint/MissingSuper:
2424
- 'lib/skunk/cli/application.rb'
2525
- 'lib/skunk/generators/html/overview.rb'
2626

27-
# Offense count: 4
27+
# Offense count: 5
2828
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
2929
Metrics/AbcSize:
3030
Max: 24
3131

32-
# Offense count: 14
32+
# Offense count: 13
3333
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
3434
# AllowedMethods: refine
3535
Metrics/BlockLength:
3636
Max: 208
3737

38-
# Offense count: 5
38+
# Offense count: 7
3939
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
4040
Metrics/MethodLength:
41-
Max: 18
41+
Max: 20
4242

4343
# Offense count: 1
4444
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
@@ -66,10 +66,3 @@ Style/FrozenStringLiteralComment:
6666
Exclude:
6767
- '**/*.arb'
6868
- 'bin/console'
69-
70-
# Offense count: 2
71-
# This cop supports safe autocorrection (--autocorrect).
72-
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
73-
# URISchemes: http, https
74-
Layout/LineLength:
75-
Max: 124

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## main [(unreleased)](https://github.com/fastruby/skunk/compare/v0.5.4...HEAD)
99

10+
* BUGFIX: Pin path_expander < 2.0 for Ruby 2.7 compatibility
11+
* [FEATURE: Add `--formats` CLI flag to select report formats (json, html, console)](https://github.com/fastruby/skunk/pull/130)
1012
* [REFACTOR: Move Console Report](https://github.com/fastruby/skunk/pull/128)
1113
* [BUGFIX: Set the right content type in the share HTTP request](https://github.com/fastruby/skunk/pull/129)
1214
* [REFACTOR: Centralize Skunk analysis into RubyCritic module](https://github.com/fastruby/skunk/pull/127)

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,33 @@ This should give you an idea if you're moving in the direction of maintaining th
158158

159159
### Setting Output Formats
160160

161-
Skunk provides a simple configuration class to control output formats programmatically. You can use `Skunk::Config` to set which formats should be generated when running Skunk.
161+
Skunk supports multiple output formats and you can select them via CLI or programmatically.
162162

163163
**Supported formats:**
164-
- `:json` - JSON report (default)
164+
- `:json` - JSON report
165165
- `:html` - HTML report with visual charts and tables
166+
- `:console` - Console output (default)
167+
168+
#### CLI flag
169+
170+
You can choose one or more formats from the command line:
171+
172+
```
173+
skunk --formats=json
174+
skunk --f json,html
175+
skunk --formats console,json
176+
```
177+
178+
If omitted, Skunk defaults to `console`.
179+
180+
#### Programmatic configuration
181+
182+
You can also configure formats in code using `Skunk::Config`:
166183

167184
```ruby
168185
require 'skunk/config'
169186

170-
# Set multiple formats
187+
# Set multiple formats (equivalent to `--formats=json,html`)
171188
Skunk::Config.formats = [:json, :html]
172189

173190
# Add a format to the existing list
@@ -177,7 +194,7 @@ Skunk::Config.add_format(:html)
177194
Skunk::Config.remove_format(:json)
178195

179196
# Check supported formats
180-
Skunk::Config.supported_formats # => [:json, :html]
197+
Skunk::Config.supported_formats # => [:json, :html, :console]
181198
Skunk::Config.supported_format?(:json) # => true
182199

183200
# Reset to defaults

lib/skunk/cli/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def initialize(argv)
2525
def execute
2626
warn_coverage_info unless File.exist?(COVERAGE_FILE)
2727

28+
Skunk::Config.reset
2829
# :reek:NilCheck
2930
@parsed_options = @options.parse.to_h
3031
command = Skunk::CommandFactory.create(@parsed_options)

lib/skunk/cli/options/argv.rb

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "rubycritic/cli/options/argv"
4+
require "skunk/config"
45

56
module Skunk
67
module Cli
@@ -15,29 +16,52 @@ class Argv < RubyCritic::Cli::Options::Argv
1516
def parse
1617
parser.new do |opts|
1718
opts.banner = "Usage: skunk [options] [paths]\n"
19+
add_branch_option(opts)
20+
add_output_option(opts)
21+
add_formats_option(opts)
22+
add_tail_options(opts)
23+
end.parse!(@argv)
24+
end
25+
26+
def to_h
27+
super.merge(output_filename: output_filename)
28+
end
1829

19-
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
20-
self.base_branch = String(branch)
21-
set_current_branch
22-
self.mode = :compare_branches
23-
end
30+
private
2431

25-
opts.on("-o", "--out FILE", "Output report to file") do |filename|
26-
self.output_filename = filename
27-
end
32+
def add_branch_option(opts)
33+
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
34+
self.base_branch = String(branch)
35+
set_current_branch
36+
self.mode = :compare_branches
37+
end
38+
end
2839

29-
opts.on_tail("-v", "--version", "Show gem's version") do
30-
self.mode = :version
31-
end
40+
def add_output_option(opts)
41+
opts.on("-o", "--out FILE", "Output report to file") do |filename|
42+
self.output_filename = filename
43+
end
44+
end
3245

33-
opts.on_tail("-h", "--help", "Show this message") do
34-
self.mode = :help
35-
end
36-
end.parse!(@argv)
46+
def add_formats_option(opts)
47+
opts.on(
48+
"-f",
49+
"--formats json,html,console",
50+
Array,
51+
"Output formats: json,html,console (default: console)"
52+
) do |list|
53+
Skunk::Config.formats = Array(list).map(&:to_sym)
54+
end
3755
end
3856

39-
def to_h
40-
super.merge(output_filename: output_filename)
57+
def add_tail_options(opts)
58+
opts.on_tail("-v", "--version", "Show gem's version") do
59+
self.mode = :version
60+
end
61+
62+
opts.on_tail("-h", "--help", "Show this message") do
63+
self.mode = :help
64+
end
4165
end
4266
end
4367
end

skunk.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
3939
spec.require_paths = ["lib"]
4040

41+
spec.add_dependency "path_expander", "< 2.0"
4142
spec.add_dependency "rubycritic", ">= 4.5.2", "< 5.0"
4243
spec.add_dependency "terminal-table", "~> 3.0"
4344

test/lib/skunk/cli/options/argv_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@
2424
end
2525
end
2626
end
27+
28+
describe "#formats" do
29+
after do
30+
Skunk::Config.reset
31+
end
32+
33+
context "passing --formats option" do
34+
let(:argv) { ["--formats=json,html"] }
35+
36+
it "applies formats to Skunk::Config" do
37+
parser = Skunk::Cli::Options::Argv.new(argv)
38+
parser.parse
39+
_(Skunk::Config.formats).must_equal %i[json html]
40+
end
41+
end
42+
43+
context "not passing --formats option" do
44+
it "defaults to console format" do
45+
parser = Skunk::Cli::Options::Argv.new([])
46+
parser.parse
47+
_(Skunk::Config.formats).must_equal [:console]
48+
end
49+
end
50+
end
2751
end

test/lib/skunk/commands/help_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Usage: skunk [options] [paths]
1313
-b, --branch BRANCH Set branch to compare
1414
-o, --out FILE Output report to file
15+
-f, --formats json,html,console Output formats: json,html,console (default: console)
1516
-v, --version Show gem's version
1617
-h, --help Show this message
1718
HELP

0 commit comments

Comments
 (0)