Module: Linecounter::CLI

Defined in:
lib/linecounter/cli.rb

Constant Summary collapse

DEFAULTS =
{
  top: 20,
  since: nil,
  json: false,
  min_loc: 20,
  show_branch_count: false,
  show_structure_overview: false,
  show_detailed_structure: false,
  repo: nil
}.freeze

Class Method Summary collapse

Class Method Details

.build_parser(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/linecounter/cli.rb', line 35

def build_parser(options)
  OptionParser.new do |o|
    o.banner = "Usage: linecounter [options]"
    o.separator ""
    o.separator "Options:"
    o.on("--top N", Integer, "Show top N rows (default: 20).") { |v| options[:top] = v }
    o.on("--since STR", String, "Limit churn to commits since date. Supports git-parseable dates in YYYY-MM-DD (e.g., '2025-01-01'), other git-parseable strings (e.g., 'last friday'), and relative forms: 'N.days.ago', 'N.weeks.ago', 'N.hours.ago', 'N.months.ago', 'N.years.ago', plus 'today'/'yesterday'.") { |v| options[:since] = v }
    o.on("--min-loc N", Integer, "Exclude files below N non-empty lines (default: 20).") { |v| options[:min_loc] = v }
    o.on("--repo PATH", String, "Path to a git repo to scan (default: current directory).") { |v| options[:repo] = v }
    o.on("--json", "Output JSON instead of text.") { options[:json] = true }
    o.on("--show-branch-count", "Show per-branch keyword breakdown under each file.") { options[:show_branch_count] = true }
    o.on("--show-structure-overview", "Show a summary of class structure counts across all files, including avg_loc_per_item (avg statement lines per item).") { options[:show_structure_overview] = true }
    o.on("--show-interaction-overview", "Alias for --show-structure-overview.") { options[:show_structure_overview] = true }
    o.on("--detailed-structure", "Show overall structure averages (avg lines per item) for each regex item across all files.") { options[:show_detailed_structure] = true }
    o.on("-h", "--help", "Show this help.") { puts o; exit }
    o.separator ""
    o.separator "Examples:"
    o.separator "  linecounter"
    o.separator "  linecounter --repo /path/to/repo"
    o.separator "  linecounter --repo /path/to/repo --top 50"
    o.separator "  linecounter --repo /path/to/repo --since 2025-01-01"
    o.separator "  linecounter --repo /path/to/repo --since 2.weeks.ago"
    o.separator "  linecounter --repo /path/to/repo --min-loc 50"
    o.separator "  linecounter --repo /path/to/repo --show-branch-count"
    o.separator "  linecounter --repo /path/to/repo --show-structure-overview"
    o.separator "  linecounter --repo /path/to/repo --detailed-structure"
    o.separator "  linecounter --repo /path/to/repo --json"
    o.separator "  linecounter --repo /path/to/repo --top 30 --since 3.months.ago"
  end
end

.run(argv) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/linecounter/cli.rb', line 21

def run(argv)
  options = DEFAULTS.dup
  build_parser(options).parse!(argv)

  options[:repo] ||= "."
  options[:since] = Git.parse_since(options[:since])

  repo_path = File.expand_path(options[:repo])
  abort "Not inside a git repository: #{repo_path}" unless Git.repo?(repo_path)

  result = Analyzer.run(repo_path: repo_path, min_loc: options[:min_loc], since: options[:since])
  Report.render(result, options)
end