diff options
Diffstat (limited to '')
-rw-r--r-- | src/CheckBasicStyle.lua | 103 |
1 files changed, 101 insertions, 2 deletions
diff --git a/src/CheckBasicStyle.lua b/src/CheckBasicStyle.lua index 19156b537..8cd454e8f 100644 --- a/src/CheckBasicStyle.lua +++ b/src/CheckBasicStyle.lua @@ -266,14 +266,113 @@ end +--- Array of files to process. Filled from cmdline arguments +local ToProcess = {} + + + + + +--- Handlers for the command-line arguments +-- Maps flag => function +local CmdLineHandlers = +{ + -- "-f file" checks the specified file + ["-f"] = function (a_Args, a_Idx) + local fnam = a_Args[a_Idx + 1] + if not(fnam) then + error("Invalid flag: '-f' needs a filename following it.") + end + table.insert(ToProcess, fnam) + return a_Idx + 2 -- skip the filename in param parsing + end, + + -- "-g" checks files reported by git as being committed. + ["-g"] = function (a_Args, a_Idx) + local f = io.popen("git diff --cached --name-only --diff-filter=ACMR") + for fnam in f:lines() do + table.insert(ToProcess, fnam) + end + end, + + -- "-h" prints help and exits + ["-h"] = function (a_Args, a_Idx) + print([[ +Usage:") +"CheckBasicStyle [<options>] + +Available options: +-f <filename> - checks the specified filename +-g - checks files reported by Git as being committed +-h - prints this help and exits +-l <listfile> - checks all files listed in the specified listfile +-- - reads the list of files to check from stdin + +When no options are given, the script checks all files listed in the AllFiles.lst file. + +Only .cpp and .h files are ever checked. +]]) + os.exit(0) + end, + + -- "-l listfile" loads the list of files to check from the specified listfile + ["-l"] = function (a_Args, a_Idx) + local listFile = a_Args[a_Idx + 1] + if not(listFile) then + error("Invalid flag: '-l' needs a filename following it.") + end + for fnam in io.lines(listFile) do + table.insert(ToProcess, fnam) + end + return a_Idx + 2 -- Skip the listfile in param parsing + end, + + -- "--" reads the list of files from stdin + ["--"] = function (a_Args, a_Idx) + for fnam in io.lines() do + table.insert(ToProcess, fnam) + end + end, +} + + + + + -- Remove buffering from stdout, so that the output appears immediately in IDEs: io.stdout:setvbuf("no") --- Process all files in the AllFiles.lst file (generated by cmake): -for fnam in io.lines("AllFiles.lst") do +-- Parse the cmdline arguments to see what files to check: +local idx = 1 +while (arg[idx]) do + local handler = CmdLineHandlers[arg[idx]] + if not(handler) then + error("Unknown command-line argument #" .. idx .. ": " .. arg[idx]) + end + idx = handler(arg, idx) or (idx + 1) -- Call the handler, let it change the next index if it wants +end + + +-- By default process all files in the AllFiles.lst file (generated by cmake): +if not(arg[1]) then + for fnam in io.lines("AllFiles.lst") do + table.insert(ToProcess, fnam) + end +end + + + + + +-- Process the files in the list: +for _, fnam in ipairs(ToProcess) do ProcessItem(fnam) end + + + + -- Report final verdict: print("Number of violations found: " .. g_NumViolations) if (g_NumViolations > 0) then |