Class: Calltally::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/calltally/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv) ⇒ Object



8
# File 'lib/calltally/cli.rb', line 8

def self.start(argv) = new.run(argv)

Instance Method Details



30
31
32
33
34
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
65
66
67
# File 'lib/calltally/cli.rb', line 30

def print_help
  puts "    calltally \#{Calltally::VERSION}\n    Usage:\n      calltally [PATH] [options]  # Defaults to scan command\n      calltally scan [PATH] [options]\n\n    Commands:\n      scan    Scan a directory (default PATH='.') and summarize method usage. (default)\n\n    Options (scan):\n      --profile PROFILE        auto(default)|rails|default\n      -d, --dirs x,y           Directories to include\n      -x, --exclude x,y        Path parts to exclude\n      -n, --top N              Show top N (default: 100)\n      -v, --verbose\n      --mode MODE              Output mode:\n                               - pairs (default): receiver-method pairs\n                               - methods: method names only\n                               - receivers: receiver names only\n      --receivers x,y          Filter by receiver constants (e.g. User,Group)\n      --methods x,y            Filter by method names (e.g. where,find)\n      --include-nil-receiver   Count calls without constant receiver (as '(no recv)')\n      --split-variables        Show variable names (e.g. '(var:user)' instead of '(var)')\n      --only-locals            Show only local variable receivers\n      --only-ivars             Show only instance variable receivers\n      --only-cvars             Show only class variable receivers\n      --only-gvars             Show only global variable receivers\n      --only-constants         Show only constant receivers\n      --only-results           Show only method results receivers\n      --[no-]skip-operators    Skip operator methods like +, -, ==, [] (default: true)\n      --plugins x,y            Enable plugins (e.g., erb for calltally-erb)\n      --format F               table(default)|json|csv\n      -o, --output PATH        Write result to file instead of STDOUT\n      --config PATH            Use a specific .calltally.yml\n      -h, --help\n  USAGE\nend\n"

#run(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/calltally/cli.rb', line 10

def run(argv)
  sub = argv.first
  return print_help if %w[-h --help help].include?(sub)

  case sub
  when "scan" then run_scan(argv.drop(1))
  when nil then run_scan(argv)  # Default to scan when no command specified
  else
    # If first arg doesn't look like a command, treat as scan with path
    if sub.start_with?("-") || File.exist?(sub) || sub == "."
      run_scan(argv)
    else
      warn "Unknown command: #{sub}"
      print_help

      exit 1
    end
  end
end

#run_scan(argv) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/calltally/cli.rb', line 69

def run_scan(argv)
  base_dir = "."
  cli_opts = {}
  config_override = nil

  opts = OptionParser.new do |opt|
    opt.on("--profile PROFILE") { |v| cli_opts["profile"] = v }
    opt.on("-d x,y", "--dirs x,y", Array) { |v| cli_opts["dirs"] = v }
    opt.on("-x x,y", "--exclude x,y", Array) { |v| cli_opts["exclude"] = v }
    opt.on("-n N", "--top N", Integer) { |v| cli_opts["top"] = v }
    opt.on("-v", "--verbose") { cli_opts["verbose"] = true }
    opt.on("--mode MODE", [:pairs, :methods, :receivers]) { |v| cli_opts["mode"] = v.to_s }
    opt.on("--receivers x,y", Array) { |v| cli_opts["receivers"] = v }
    opt.on("--methods x,y", Array) { |v| cli_opts["methods"] = v }
    opt.on("--include-nil-receiver") { cli_opts["include_nil_receiver"] = true }
    opt.on("--split-variables") { cli_opts["split_variables"] = true }
    opt.on("--only-locals") { (cli_opts["receiver_types"] ||= []) << "locals" }
    opt.on("--only-ivars") { (cli_opts["receiver_types"] ||= []) << "ivars" }
    opt.on("--only-cvars") { (cli_opts["receiver_types"] ||= []) << "cvars" }
    opt.on("--only-gvars") { (cli_opts["receiver_types"] ||= []) << "gvars" }
    opt.on("--only-constants") { (cli_opts["receiver_types"] ||= []) << "constants" }
    opt.on("--only-results") { (cli_opts["receiver_types"] ||= []) << "results" }
    opt.on("--[no-]skip-operators") { |v| cli_opts["skip_operators"] = v }
    opt.on("--plugins x,y", Array) { |v| cli_opts["plugins"] = v }
    opt.on("--format F", [:table, :json, :csv]) { |v| cli_opts["format"] = v.to_s }
    opt.on("-o PATH", "--output PATH") { |v| cli_opts["output"] = v }
    opt.on("--config PATH") { |v| config_override = v }
    opt.on("-h", "--help") { puts opt; exit }
  end

  if argv.first && !argv.first.start_with?("-")
    base_dir = argv.shift
  end

  opts.parse!(argv)

  if config_override && File.file?(config_override)
    yaml = YAML.load_file(config_override) || {}
    yaml.each { |k, v| cli_opts[k.to_s] = v unless cli_opts.key?(k.to_s) }
  end

  config = Calltally::Config.load(base_dir: base_dir, cli_opts: cli_opts)

  mode, rows = Calltally::Scanner.new(base_dir: base_dir, config: config).scan

  out = config["output"] ? File.open(config["output"], "w") : $stdout
  begin
    case config["format"]
    when "json" then Calltally::Formatter.print_json(mode: mode, data: rows, io: out)
    when "csv"  then Calltally::Formatter.print_csv(mode: mode, data: rows, io: out)
    else             Calltally::Formatter.print_table(mode: mode, data: rows, io: out)
    end
  ensure
    out.close if out.is_a?(File)
  end
end