PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/main.go

https://github.com/boyter/scc
Go | 358 lines | 342 code | 10 blank | 6 comment | 10 complexity | 0110504149d257ceaddc70bcd7a5b214 MD5 | raw file
Possible License(s): MIT, Unlicense
  1. // SPDX-License-Identifier: MIT OR Unlicense
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/boyter/scc/v3/processor"
  8. "github.com/spf13/cobra"
  9. )
  10. //go:generate go run scripts/include.go
  11. func main() {
  12. //f, _ := os.Create("scc.pprof")
  13. //pprof.StartCPUProfile(f)
  14. //defer pprof.StopCPUProfile()
  15. if len(os.Args) == 2 && strings.HasPrefix(os.Args[1], "@") {
  16. // handle "scc @flags.txt" syntax
  17. filepath := strings.TrimPrefix(os.Args[1], "@")
  18. b, err := os.ReadFile(filepath)
  19. if err != nil {
  20. fmt.Printf("Error reading flags from a file: %s\n", err)
  21. os.Exit(1)
  22. }
  23. args := strings.Split(string(b), "\n")
  24. var newArgs []string
  25. for _, x := range args {
  26. newArgs = append(newArgs, strings.TrimSpace(x))
  27. }
  28. os.Args = append([]string{os.Args[0]}, newArgs...)
  29. }
  30. rootCmd := &cobra.Command{
  31. Use: "scc [flags] [files or directories]",
  32. Short: "scc [files or directories]",
  33. Long: fmt.Sprintf("Sloc, Cloc and Code. Count lines of code in a directory with complexity estimation.\nVersion %s\nBen Boyter <ben@boyter.org> + Contributors", processor.Version),
  34. Version: processor.Version,
  35. Run: func(cmd *cobra.Command, args []string) {
  36. processor.DirFilePaths = args
  37. if processor.ConfigureLimits != nil {
  38. processor.ConfigureLimits()
  39. }
  40. processor.ConfigureGc()
  41. processor.ConfigureLazy(true)
  42. processor.Process()
  43. },
  44. }
  45. flags := rootCmd.PersistentFlags()
  46. flags.BoolVar(
  47. &processor.DisableCheckBinary,
  48. "binary",
  49. false,
  50. "disable binary file detection",
  51. )
  52. flags.BoolVar(
  53. &processor.Files,
  54. "by-file",
  55. false,
  56. "display output for every file",
  57. )
  58. flags.BoolVar(
  59. &processor.Ci,
  60. "ci",
  61. false,
  62. "enable CI output settings where stdout is ASCII",
  63. )
  64. flags.BoolVar(
  65. &processor.Ignore,
  66. "no-ignore",
  67. false,
  68. "disables .ignore file logic",
  69. )
  70. flags.BoolVar(
  71. &processor.GitIgnore,
  72. "no-gitignore",
  73. false,
  74. "disables .gitignore file logic",
  75. )
  76. flags.BoolVar(
  77. &processor.Debug,
  78. "debug",
  79. false,
  80. "enable debug output",
  81. )
  82. flags.StringSliceVar(
  83. &processor.PathDenyList,
  84. "exclude-dir",
  85. []string{".git", ".hg", ".svn"},
  86. "directories to exclude",
  87. )
  88. flags.IntVar(
  89. &processor.GcFileCount,
  90. "file-gc-count",
  91. 10000,
  92. "number of files to parse before turning the GC on",
  93. )
  94. flags.StringVarP(
  95. &processor.Format,
  96. "format",
  97. "f",
  98. "tabular",
  99. "set output format [tabular, wide, json, csv, csv-stream, cloc-yaml, html, html-table, sql, sql-insert, openmetrics]",
  100. )
  101. flags.StringSliceVarP(
  102. &processor.AllowListExtensions,
  103. "include-ext",
  104. "i",
  105. []string{},
  106. "limit to file extensions [comma separated list: e.g. go,java,js]",
  107. )
  108. flags.StringSliceVarP(
  109. &processor.ExcludeListExtensions,
  110. "exclude-ext",
  111. "x",
  112. []string{},
  113. "ignore file extensions (overrides include-ext) [comma separated list: e.g. go,java,js]",
  114. )
  115. flags.StringSliceVarP(
  116. &processor.ExcludeFilename,
  117. "exclude-file",
  118. "n",
  119. []string{},
  120. "ignore files with matching names [comma separated list: e.g. main.go,_test.go]",
  121. )
  122. flags.BoolVarP(
  123. &processor.Languages,
  124. "languages",
  125. "l",
  126. false,
  127. "print supported languages and extensions",
  128. )
  129. flags.Int64Var(
  130. &processor.AverageWage,
  131. "avg-wage",
  132. 56286,
  133. "average wage value used for basic COCOMO calculation",
  134. )
  135. flags.Float64Var(
  136. &processor.Overhead,
  137. "overhead",
  138. 2.4,
  139. "set the overhead multiplier for corporate overhead (facilities, equipment, accounting, etc.)",
  140. )
  141. flags.Float64Var(
  142. &processor.EAF,
  143. "eaf",
  144. 1.0,
  145. "the effort adjustment factor derived from the cost drivers (1.0 if rated nominal)",
  146. )
  147. flags.BoolVar(
  148. &processor.SLOCCountFormat,
  149. "sloccount-format",
  150. false,
  151. "print a more SLOCCount like COCOMO calculation",
  152. )
  153. flags.BoolVar(
  154. &processor.Cocomo,
  155. "no-cocomo",
  156. false,
  157. "remove COCOMO calculation output",
  158. )
  159. flags.StringVar(
  160. &processor.CocomoProjectType,
  161. "cocomo-project-type",
  162. "organic",
  163. "change COCOMO model type [organic, semi-detached, embedded, \"custom,1,1,1,1\"]",
  164. )
  165. flags.BoolVar(
  166. &processor.Size,
  167. "no-size",
  168. false,
  169. "remove size calculation output",
  170. )
  171. flags.StringVar(
  172. &processor.SizeUnit,
  173. "size-unit",
  174. "si",
  175. "set size unit [si, binary, mixed, xkcd-kb, xkcd-kelly, xkcd-imaginary, xkcd-intel, xkcd-drive, xkcd-bakers]",
  176. )
  177. flags.BoolVarP(
  178. &processor.Complexity,
  179. "no-complexity",
  180. "c",
  181. false,
  182. "skip calculation of code complexity",
  183. )
  184. flags.BoolVarP(
  185. &processor.Duplicates,
  186. "no-duplicates",
  187. "d",
  188. false,
  189. "remove duplicate files from stats and output",
  190. )
  191. flags.BoolVarP(
  192. &processor.MinifiedGenerated,
  193. "min-gen",
  194. "z",
  195. false,
  196. "identify minified or generated files",
  197. )
  198. flags.BoolVarP(
  199. &processor.Minified,
  200. "min",
  201. "",
  202. false,
  203. "identify minified files",
  204. )
  205. flags.BoolVarP(
  206. &processor.Generated,
  207. "gen",
  208. "",
  209. false,
  210. "identify generated files",
  211. )
  212. flags.StringSliceVarP(
  213. &processor.GeneratedMarkers,
  214. "generated-markers",
  215. "",
  216. []string{"do not edit", "<auto-generated />"},
  217. "string markers in head of generated files",
  218. )
  219. flags.BoolVar(
  220. &processor.IgnoreMinifiedGenerate,
  221. "no-min-gen",
  222. false,
  223. "ignore minified or generated files in output (implies --min-gen)",
  224. )
  225. flags.BoolVar(
  226. &processor.IgnoreMinified,
  227. "no-min",
  228. false,
  229. "ignore minified files in output (implies --min)",
  230. )
  231. flags.BoolVar(
  232. &processor.IgnoreGenerated,
  233. "no-gen",
  234. false,
  235. "ignore generated files in output (implies --gen)",
  236. )
  237. flags.IntVar(
  238. &processor.MinifiedGeneratedLineByteLength,
  239. "min-gen-line-length",
  240. 255,
  241. "number of bytes per average line for file to be considered minified or generated",
  242. )
  243. flags.StringArrayVarP(
  244. &processor.Exclude,
  245. "not-match",
  246. "M",
  247. []string{},
  248. "ignore files and directories matching regular expression",
  249. )
  250. flags.StringVarP(
  251. &processor.FileOutput,
  252. "output",
  253. "o",
  254. "",
  255. "output filename (default stdout)",
  256. )
  257. flags.StringVarP(
  258. &processor.SortBy,
  259. "sort",
  260. "s",
  261. "files",
  262. "column to sort by [files, name, lines, blanks, code, comments, complexity]",
  263. )
  264. flags.BoolVarP(
  265. &processor.Trace,
  266. "trace",
  267. "t",
  268. false,
  269. "enable trace output (not recommended when processing multiple files)",
  270. )
  271. flags.BoolVarP(
  272. &processor.Verbose,
  273. "verbose",
  274. "v",
  275. false,
  276. "verbose output",
  277. )
  278. flags.BoolVarP(
  279. &processor.More,
  280. "wide",
  281. "w",
  282. false,
  283. "wider output with additional statistics (implies --complexity)",
  284. )
  285. flags.BoolVar(
  286. &processor.NoLarge,
  287. "no-large",
  288. false,
  289. "ignore files over certain byte and line size set by max-line-count and max-byte-count",
  290. )
  291. flags.BoolVar(
  292. &processor.IncludeSymLinks,
  293. "include-symlinks",
  294. false,
  295. "if set will count symlink files",
  296. )
  297. flags.Int64Var(
  298. &processor.LargeLineCount,
  299. "large-line-count",
  300. 40000,
  301. "number of lines a file can contain before being removed from output",
  302. )
  303. flags.Int64Var(
  304. &processor.LargeByteCount,
  305. "large-byte-count",
  306. 1000000,
  307. "number of bytes a file can contain before being removed from output",
  308. )
  309. flags.StringVar(
  310. &processor.CountAs,
  311. "count-as",
  312. "",
  313. "count extension as language [e.g. jsp:htm,chead:\"C Header\" maps extension jsp to html and chead to C Header]",
  314. )
  315. flags.StringVar(
  316. &processor.FormatMulti,
  317. "format-multi",
  318. "",
  319. "have multiple format output overriding --format [e.g. tabular:stdout,csv:file.csv,json:file.json]",
  320. )
  321. flags.StringVar(
  322. &processor.SQLProject,
  323. "sql-project",
  324. "",
  325. "use supplied name as the project identifier for the current run. Only valid with the --format sql or sql-insert option",
  326. )
  327. flags.StringVar(
  328. &processor.RemapUnknown,
  329. "remap-unknown",
  330. "",
  331. "inspect files of unknown type and remap by checking for a string and remapping the language [e.g. \"-*- C++ -*-\":\"C Header\"]",
  332. )
  333. flags.StringVar(
  334. &processor.RemapAll,
  335. "remap-all",
  336. "",
  337. "inspect every file and remap by checking for a string and remapping the language [e.g. \"-*- C++ -*-\":\"C Header\"]",
  338. )
  339. flags.StringVar(
  340. &processor.CurrencySymbol,
  341. "currency-symbol",
  342. "$",
  343. "set currency symbol",
  344. )
  345. if err := rootCmd.Execute(); err != nil {
  346. os.Exit(1)
  347. }
  348. }