# File 'lib/typeprof/cli/cli.rb', line 3
def initialize(argv)
opt = OptionParser.new
opt.banner = "Usage: #{ opt.program_name } [options] files_or_dirs..."
core_options = {}
lsp_options = {}
cli_options = {}
output = nil
rbs_collection_path = nil
initialize_config_file = false
exclude_patterns = []
opt.separator ""
opt.separator "Options:"
opt.on("-o OUTFILE", "Output to OUTFILE instead of stdout") {|v| output = v }
opt.on("-q", "--quiet", "Quiet mode") do
core_options[:display_indicator] = false
end
opt.on("-v", "--verbose", "Verbose mode") do
core_options[:show_errors] = true
end
opt.on("--version", "Display typeprof version") { cli_options[:display_version] = true }
opt.on("--collection PATH", "File path of collection configuration") { |v| rbs_collection_path = v }
opt.on("--no-collection", "Ignore collection configuration") { rbs_collection_path = :no }
opt.on("--exclude PATTERN", "Exclude files matching glob PATTERN (can be specified multiple times)") { |v| exclude_patterns << v }
opt.on("--lsp", "LSP server mode") do |v|
core_options[:display_indicator] = false
cli_options[:lsp] = true
end
opt.separator ""
opt.separator "Analysis output options:"
opt.on("--[no-]show-typeprof-version", "Display TypeProf version in a header") {|v| core_options[:output_typeprof_version] = v }
opt.on("--[no-]show-errors", "Display possible errors found during the analysis") {|v| core_options[:output_diagnostics] = v }
opt.on("--[no-]show-parameter-names", "Display parameter names for methods") {|v| core_options[:output_parameter_names] = v }
opt.on("--[no-]show-source-locations", "Display definition source locations for methods") {|v| core_options[:output_source_locations] = v }
opt.on("--[no-]show-stats", "Display type inference statistics after analysis (for debugging purpose)") {|v| core_options[:output_stats] = v }
opt.separator ""
opt.separator "Advanced options:"
opt.on("--[no-]stackprof MODE", /\Acpu|wall|object\z/, "Enable stackprof (for debugging purpose)") {|v| cli_options[:stackprof] = v.to_sym }
opt.on("--init", 'Generate TypeProf configuration file') {|v| initialize_config_file = true}
opt.separator ""
opt.separator "LSP options:"
opt.on("--port PORT", Integer, "Specify a port number to listen for requests on") {|v| lsp_options[:port] = v }
opt.on("--stdio", "Use stdio for LSP transport") {|v| lsp_options[:stdio] = v }
opt.parse!(argv)
if initialize_config_file
generate_config_file
exit 0
end
if !cli_options[:lsp] && !lsp_options.empty?
raise OptionParser::InvalidOption.new("lsp options with non-lsp mode")
end
@core_options = {
rbs_collection: setup_rbs_collection(rbs_collection_path),
display_indicator: $stderr.tty?,
output_typeprof_version: true,
output_errors: false,
output_parameter_names: false,
output_source_locations: false,
output_stats: false,
exclude_patterns: exclude_patterns,
}.merge(core_options)
@lsp_options = {
port: 0,
stdio: false,
}.merge(lsp_options)
@cli_options = {
argv:,
output: output ? open(output, "w") : $stdout.dup,
display_version: false,
stackprof: nil,
lsp: false,
}.merge(cli_options)
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!
exit 1
end