cpp_linter/cli/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
68
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! This module holds the Command Line Interface design.
use std::path::PathBuf;

// non-std crates
use clap::builder::{ArgPredicate, BoolishValueParser, FalseyValueParser};
use clap::{value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command};

mod structs;
pub use structs::{ClangParams, Cli, FeedbackInput, LinesChangedOnly, ThreadComments};

/// Builds and returns the Command Line Interface's argument parsing object.
pub fn get_arg_parser() -> Command {
    Command::new("cpp-linter")
        .subcommand(
            Command::new("version")
                .about("Display the cpp-linter version and exit.")
        )
        .arg(
            Arg::new("verbosity")
                .long("verbosity")
                .short('v')
                .default_value("info")
                .value_parser(["debug", "info"])
                .help(
                    "This controls the action's verbosity in the workflow's logs.
This option does not affect the verbosity of resulting
thread comments or file annotations.\n\n",
            ),
        )
        .arg(
            Arg::new("database")
                .long("database")
                .short('p')
                .value_parser(value_parser!(PathBuf))
                .help_heading("clang-tidy options")
                .help(
                    "The path that is used to read a compile command database.
For example, it can be a CMake build directory in which a file named
compile_commands.json exists (set `CMAKE_EXPORT_COMPILE_COMMANDS` to `ON`).
When no build path is specified, a search for compile_commands.json will be
attempted through all parent paths of the first input file. See [LLVM docs about
setup tooling](https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html)
for an example of setting up Clang Tooling on a source tree.",
            )
        )
        .arg(
            Arg::new("style")
                .short('s')
                .long("style")
                .default_value("llvm")
                .help_heading("clang-format options")
                .help(
                    "The style rules to use.

- Set this to `file` to have clang-format use the closest relative
  .clang-format file.
- Set this to a blank string (`''`) to disable using clang-format
  entirely.

> [!note]
> If this is not a blank string, then it is also passed to clang-tidy
> (if [`--tidy_checks`](#-c---tidy-checks) is not `-*`).
> This is done to ensure suggestions from both clang-tidy and
> clang-format are consistent.\n\n",
            ),
        )
        .arg(
            Arg::new("tidy-checks")
                .short('c')
                .long("tidy-checks")
                .default_value(
                    "boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-*,cppcoreguidelines-*",
                )
                .help_heading("clang-tidy options")
                .help(
                    "A comma-separated list of globs with optional `-` prefix.
Globs are processed in order of appearance in the list.
Globs without `-` prefix add checks with matching names to the set,
globs with the `-` prefix remove checks with matching names from the set of
enabled checks. This option's value is appended to the value of the 'Checks'
option in a .clang-tidy file (if any).

- It is possible to disable clang-tidy entirely by setting this option to
  `'-*'`.
- It is also possible to rely solely on a .clang-tidy config file by
  specifying this option as a blank string (`''`).

See also clang-tidy docs for more info.\n\n",
            ),
        )
        .arg(
            Arg::new("version")
                .short('V')
                .long("version")
                .default_missing_value("NO-VERSION")
                .num_args(0..=1)
                .require_equals(true)
                .default_value("")
                .help(
                    "The desired version of the clang tools to use. Accepted options are
strings which can be 8, 9, 10, 11, 12, 13, 14, 15, 16, 17.

- Set this option to a blank string (`''`) to use the
  platform's default installed version.
- This value can also be a path to where the clang tools are
  installed (if using a custom install location). All paths specified
  here are converted to absolute.\n\n",
            ),
        )
        .arg(
            Arg::new("extensions")
                .short('e')
                .long("extensions")
                .value_delimiter(',')
                .default_value("c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx")
                .help_heading("Source options")
                .help("A comma-separated list of file extensions to analyze.\n"),
        )
        .arg(
            Arg::new("repo-root")
                .short('r')
                .long("repo-root")
                .default_value(".")
                .help_heading("Source options")
                .help(
                    "The relative path to the repository root directory. This path is
relative to the runner's `GITHUB_WORKSPACE` environment variable (or
the current working directory if not using a CI runner).\n\n",
            ),
        )
        .arg(
            Arg::new("ignore")
                .short('i')
                .long("ignore")
                .value_delimiter('|')
                .default_value(".github|target")
                .help_heading("Source options")
                .help(
                    "Set this option with path(s) to ignore (or not ignore).

- In the case of multiple paths, you can use `|` to separate each path.
- There is no need to use `./` for each entry; a blank string (`''`)
  represents the repo-root path.
- This can also have files, but the file's path (relative to
  the [`--repo-root`](#-r---repo-root)) has to be specified with the filename.
- Submodules are automatically ignored. Hidden directories (beginning
  with a `.`) are also ignored automatically.
- Prefix a path with `!` to explicitly not ignore it. This can be
  applied to a submodule's path (if desired) but not hidden directories.
- Glob patterns are supported here. Path separators in glob patterns should
  use `/` because `\\` represents an escaped literal.\n\n",
            ),
        )
        .arg(
            Arg::new("ignore-tidy")
                .short('D')
                .long("ignore-tidy")
                .value_delimiter('|')
                .help_heading("clang-tidy options")
                .help(
                    "Similar to [`--ignore`](#-i---ignore) but applied
exclusively to files analyzed by clang-tidy.\n\n",
            ),
        )
        .arg(
            Arg::new("ignore-format")
                .short('M')
                .long("ignore-format")
                .value_delimiter('|')
                .help_heading("clang-format options")
                .help(
                    "Similar to [`--ignore`](#-i---ignore) but applied
exclusively to files analyzed by clang-format.\n\n",
            ),
        )
        .arg(
            Arg::new("lines-changed-only")
                .short('l')
                .long("lines-changed-only")
                .value_parser(["true", "on", "1", "false", "off", "0", "diff"])
                .default_value("true")
                .help_heading("Source options")
                .help(
                    "This controls what part of the files are analyzed.
The following values are accepted:

- `false`: All lines in a file are analyzed.
- `true`: Only lines in the diff that contain additions are analyzed.
- `diff`: All lines in the diff are analyzed (including unchanged
  lines but not subtractions).\n\n",
            ),
        )
        .arg(
            Arg::new("files-changed-only")
                .short('f')
                .long("files-changed-only")
                .default_value_if("lines-changed-only", ArgPredicate::Equals("true".into()), "true")
                .default_value("false")
                .value_parser(FalseyValueParser::new())
                .help_heading("Source options")
                .help(
                    "Set this option to false to analyze any source files in the repo.
This is automatically enabled if
[`--lines-changed-only`](#-l---lines-changed-only) is enabled.

> [!note]
> The `GITHUB_TOKEN` should be supplied when running on a
> private repository with this option enabled, otherwise the runner
> does not not have the privilege to list the changed files for an event.
>
> See [Authenticating with the `GITHUB_TOKEN`](
> https://docs.github.com/en/actions/reference/authentication-in-a-workflow).\n\n",
            ),
        )
        .arg(
            Arg::new("extra-arg")
                .long("extra-arg")
                .short('x')
                .action(ArgAction::Append)
                .help_heading("clang-tidy options")
                .help(
                    r#"A string of extra arguments passed to clang-tidy for use as
compiler arguments. This can be specified more than once for each
additional argument. Recommend using quotes around the value and
avoid using spaces between name and value (use `=` instead):

```shell
cpp-linter --extra-arg="-std=c++17" --extra-arg="-Wall"
```"#,
            ),
        )
        .arg(
            Arg::new("thread-comments")
                .long("thread-comments")
                .short('g')
                .value_parser(["true", "on", "1", "false", "off", "0", "update"])
                .default_value("false")
                .help_heading("feedback options")
                .help(
                    "Set this option to true to enable the use of thread comments as feedback.
Set this to `update` to update an existing comment if one exists;
the value 'true' will always delete an old comment and post a new one if necessary.

> [!note]
> To use thread comments, the `GITHUB_TOKEN` (provided by
> Github to each repository) must be declared as an environment
> variable.
>
> See [Authenticating with the `GITHUB_TOKEN`](
> https://docs.github.com/en/actions/reference/authentication-in-a-workflow).\n\n",
            ),
        )
        .arg(
            Arg::new("no-lgtm")
                .long("no-lgtm")
                .short('t')
                .value_parser(FalseyValueParser::new())
                .default_value("true")
                .help_heading("feedback options")
                .help(
                    "Set this option to true or false to enable or disable the use of a
thread comment that basically says 'Looks Good To Me' (when all checks pass).

> [!important]
> The [`--thread-comments`](#-g---thread-comments)
> option also notes further implications.\n\n",
            ),
        )
        .arg(
            Arg::new("step-summary")
                .long("step-summary")
                .short('w')
                .value_parser(FalseyValueParser::new())
                .default_value("false")
                .help_heading("feedback options")
                .help(
                    "Set this option to true or false to enable or disable the use of
a workflow step summary when the run has concluded.\n\n",
            ),
        )
        .arg(
            Arg::new("file-annotations")
                .long("file-annotations")
                .short('a')
                .value_parser(FalseyValueParser::new())
                .default_value("true")
                .help_heading("feedback options")
                .help(
                    "Set this option to false to disable the use of
file annotations as feedback.\n\n",
            ),
        )
        .arg(
            Arg::new("tidy-review")
                .long("tidy-review")
                .short('d')
                .value_parser(BoolishValueParser::new())
                .default_value("false")
                .help_heading("feedback options")
                .help(
                    "Set to `true` to enable Pull Request reviews from clang-tidy.\n\n",
            ),
        )
        .arg(
            Arg::new("format-review")
                .long("format-review")
                .short('m')
                .value_parser(BoolishValueParser::new())
                .default_value("false")
                .help_heading("feedback options")
                .help(
                    "Set to `true` to enable Pull Request reviews from clang-format.\n\n",
            ),
        )
        .arg(
            Arg::new("passive-reviews")
                .long("passive-reviews")
                .short('R')
                .value_parser(BoolishValueParser::new())
                .default_value("false")
                .help_heading("feedback options")
                .help(
                    "Set to `true` to prevent Pull Request reviews from
approving or requesting changes.\n\n",
            ),
        )
        .arg(
            Arg::new("files")
                .action(ArgAction::Append)
                .help(
                    "An explicit path to a file.
This can be specified zero or more times, resulting in a list of files.
The list of files is appended to the internal list of 'not ignored' files.
Further filtering can still be applied (see [Source options](#source-options)).",
            )
        )
        .groups([
            ArgGroup::new("Clang-tidy options")
                .args(["tidy-checks", "database", "extra-arg", "ignore-tidy"]).multiple(true),
            ArgGroup::new("Clang-format options").args(["style", "ignore-format"]).multiple(true),
            ArgGroup::new("General options").args(["verbosity", "version"]).multiple(true),
            ArgGroup::new("Source options").args(["extensions", "repo-root", "ignore", "lines-changed-only", "files-changed-only"]).multiple(true),
            ArgGroup::new("Feedback options").args([
                "thread-comments", "no-lgtm", "step-summary", "file-annotations", "tidy-review", "format-review", "passive-reviews"
            ]).multiple(true),
        ])
        .next_line_help(true)
}

/// Converts the parsed value of the `--extra-arg` option into an optional vector of strings.
///
/// This is for adapting to 2 scenarios where `--extra-arg` is either
///
/// - specified multiple times
///     - each val is appended to a [`Vec`] (by clap crate)
/// - specified once with multiple space-separated values
///     - resulting [`Vec`] is made from splitting at the spaces between
/// - not specified at all (returns [`None`])
///
/// It is preferred that the values specified in either situation do not contain spaces and are
/// quoted:
/// ```shell
/// --extra-arg="-std=c++17" --extra-arg="-Wall"
/// # or equivalently
/// --extra-arg="-std=c++17 -Wall"
/// ```
/// The cpp-linter-action (for Github CI workflows) can only use 1 `extra-arg` input option, so
/// the value will be split at spaces.
pub fn convert_extra_arg_val(args: &ArgMatches) -> Vec<String> {
    let mut val = args.get_many::<String>("extra-arg").unwrap_or_default();
    if val.len() == 1 {
        // specified once; split and return result
        return val
            .next()
            .unwrap()
            .trim_matches('\'')
            .trim_matches('"')
            .split(' ')
            .map(|i| i.to_string())
            .collect();
    } else {
        // specified multiple times; just return
        val.map(|i| i.to_string()).collect()
    }
}

#[cfg(test)]
mod test {
    use clap::ArgMatches;

    use super::{convert_extra_arg_val, get_arg_parser};

    fn parser_args(input: Vec<&str>) -> ArgMatches {
        let arg_parser = get_arg_parser();
        arg_parser.get_matches_from(input)
    }

    #[test]
    fn extra_arg_0() {
        let args = parser_args(vec!["cpp-linter"]);
        let extras = convert_extra_arg_val(&args);
        assert!(extras.is_empty());
    }

    #[test]
    fn extra_arg_1() {
        let args = parser_args(vec!["cpp-linter", "--extra-arg='-std=c++17 -Wall'"]);
        let extra_args = convert_extra_arg_val(&args);
        assert_eq!(extra_args.len(), 2);
        assert_eq!(extra_args, ["-std=c++17", "-Wall"])
    }

    #[test]
    fn extra_arg_2() {
        let args = parser_args(vec![
            "cpp-linter",
            "--extra-arg=-std=c++17",
            "--extra-arg=-Wall",
        ]);
        let extra_args = convert_extra_arg_val(&args);
        assert_eq!(extra_args.len(), 2);
        assert_eq!(extra_args, ["-std=c++17", "-Wall"])
    }
}