libdot: lint: add shortcuts to narrow down selection

While we want the default behavior to lint every file in the tree,
when iterating on a subsection of the codebase, this can slow things
down a bit.  Add some shortcuts to help narrow down the behavior so
people can ignore unrelated file types.

Change-Id: I1958f9a2fc0bbe2eb6ec4e9d82fba8567ef446a1
Reviewed-on: https://chromium-review.googlesource.com/c/apps/libapps/+/2205459
Reviewed-by: Joel Hockey <joelhockey@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/libdot/bin/lint b/libdot/bin/lint
index fbeb13c..92b0d54 100755
--- a/libdot/bin/lint
+++ b/libdot/bin/lint
@@ -145,11 +145,25 @@
                         help='Run linters with --fix setting if possible.')
     parser.add_argument('--gerrit-comments-file',
                         help='Save errors for posting files to Gerrit.')
-    parser.add_argument('paths', nargs='*',
-                        help='Paths to lint.')
     parser.add_argument('--skip-mkdeps', dest='run_mkdeps',
                         action='store_false', default=True,
                         help='Skip (re)building of dependencies.')
+
+    group = parser.add_argument_group(
+        'File selection/filtering',
+        description='All files are linted by default')
+    group.add_argument('--cpp', action='store_true',
+                       help='Lint C/C++ files.')
+    group.add_argument('--js', action='store_true',
+                       help='Lint JavaScript files.')
+    group.add_argument('--py', action='store_true',
+                       help='Lint Python files.')
+    group.add_argument('--txt', action='store_true',
+                       help='Lint text files.')
+
+    parser.add_argument('paths', nargs='*',
+                        help='Paths to lint.')
+
     return parser
 
 
@@ -190,6 +204,11 @@
         else:
             logging.info('Skipping building dependencies due to --skip-mkdeps')
 
+    lint_groups = {'cpp', 'js', 'py', 'txt'}
+    lint_all = all(not getattr(opts, x) for x in lint_groups)
+    for group in lint_groups:
+        setattr(opts, group, lint_all or getattr(opts, group))
+
     tolint_paths = set(opts.paths)
     kwargs = {
         'fix': opts.fix,
@@ -198,8 +217,8 @@
     checks = []
 
     js_files = [x for x in opts.paths if x.endswith('.js')]
-    if js_files:
-        tolint_paths -= set(js_files)
+    tolint_paths -= set(js_files)
+    if js_files and opts.js:
         js_kwargs = {'paths': js_files, **kwargs}
         checks += [
             libdot.eslint.perform(**js_kwargs),
@@ -207,21 +226,21 @@
         ]
 
     py_files = libdot.pylint.filter_known_files(opts.paths)
-    if py_files:
-        tolint_paths -= set(py_files)
+    tolint_paths -= set(py_files)
+    if py_files and opts.py:
         py_kwargs = {'paths': py_files, **kwargs}
         checks += [libdot.pylint.perform(**py_kwargs)]
 
     cpp_files = libdot.cpplint.filter_known_files(opts.paths)
-    if cpp_files:
-        tolint_paths -= set(cpp_files)
+    tolint_paths -= set(cpp_files)
+    if cpp_files and opts.cpp:
         cpp_kwargs = {'paths': cpp_files, **kwargs}
         checks += [libdot.cpplint.perform(**cpp_kwargs)]
 
     html_files = [x for x in opts.paths
                   if x.endswith('.html') or x.endswith('.html.in')]
-    if html_files:
-        tolint_paths -= set(html_files)
+    tolint_paths -= set(html_files)
+    if html_files and opts.txt:
         checks += [lint_html_files(html_files)]
 
     # TODO(vapier): Add smarter JSON & markdown linters.
@@ -235,8 +254,8 @@
     text_files = [x for x in opts.paths
                   if (os.path.basename(x) in TEXT_FILENAMES or
                       os.path.splitext(x)[1] in TEXT_EXTENSIONS)]
-    if text_files:
-        tolint_paths -= set(text_files)
+    tolint_paths -= set(text_files)
+    if text_files and opts.txt:
         checks += [lint_text_files(text_files)]
 
     if tolint_paths: