Skip to content

Commit b15dcfe

Browse files
authored
refactor: improve CLI value parsing/docs (#208)
A follow-up to #204 - Allow boolean options to be treated as flags (no value passed) if their default value is `false` (`--file-changes-only`, `--step-summary`, `--*-review`, etc). - Some other CLI options can now accept no value (like a flag) including `--verbosity` - Use `std::str::FromStr` trait to parse the given `--version`: * if no value is given, then behave exactly like the `cpp-linter version` subcommand (print version and exit) * if a path is given look for clang-tools in that path. This only ensures the given path exists, not if the clang- tool is present; that is done later if the tool is needed. * expand support for version specifiers using `semver::VersionReq`. This removes the need for `lenient_semver` dependency and allows the user to specify a range of clang versions. For example, `>=14, <16`, `=12.0.1`, or simply `16` (would be treated as `=16`). See [`semver::VersionReq` docs][ver-req-docs] for more detail. - adjust docs to better describe accepted/possible values. Due to the changes in parsing the user-given `--version`, there is a new enum, `ClangTool` that enforces type-safety about finding the clang tools (when they are needed). * Instead of passing the tool name (as a str), the `ClangTool` enum is used to avoid typos and convey explicit support for only clang-tidy and clang-format. * Getting the clang tool's path and version are now instance methods of the `ClangTool` enum. [ver-req-docs]: https://docs.rs/semver/1.0.27/semver/struct.VersionReq.html
1 parent 106a0e9 commit b15dcfe

File tree

8 files changed

+398
-198
lines changed

8 files changed

+398
-198
lines changed

Cargo.lock

Lines changed: 0 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp-linter/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ colored = "3.0.0"
2121
fast-glob = "1.0.0"
2222
futures = "0.3.31"
2323
git2 = "0.20.2"
24-
lenient_semver = "0.4.2"
2524
log = { version = "0.4.28", features = ["std"] }
2625
quick-xml = { version = "0.38.3", features = ["serialize"] }
2726
regex = "1.12.2"

cpp-linter/src/clang_tools/clang_tidy.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{
99
sync::{Arc, Mutex, MutexGuard},
1010
};
1111

12-
use anyhow::{Context, Result};
1312
// non-std crates
13+
use anyhow::{Context, Result};
1414
use regex::Regex;
1515
use serde::Deserialize;
1616

@@ -346,14 +346,15 @@ mod test {
346346
use std::{
347347
env,
348348
path::PathBuf,
349+
str::FromStr,
349350
sync::{Arc, Mutex},
350351
};
351352

352353
use regex::Regex;
353354

354355
use crate::{
355-
clang_tools::get_clang_tool_exe,
356-
cli::{ClangParams, LinesChangedOnly},
356+
clang_tools::ClangTool,
357+
cli::{ClangParams, LinesChangedOnly, RequestedVersion},
357358
common_fs::FileObj,
358359
};
359360

@@ -421,11 +422,14 @@ mod test {
421422

422423
#[test]
423424
fn use_extra_args() {
424-
let exe_path = get_clang_tool_exe(
425-
"clang-tidy",
426-
env::var("CLANG_VERSION").unwrap_or("".to_string()).as_str(),
427-
)
428-
.unwrap();
425+
let exe_path = ClangTool::ClangTidy
426+
.get_exe_path(
427+
&RequestedVersion::from_str(
428+
env::var("CLANG_VERSION").unwrap_or("".to_string()).as_str(),
429+
)
430+
.unwrap(),
431+
)
432+
.unwrap();
429433
let file = FileObj::new(PathBuf::from("tests/demo/demo.cpp"));
430434
let arc_ref = Arc::new(Mutex::new(file));
431435
let extra_args = vec!["-std=c++17".to_string(), "-Wall".to_string()];

0 commit comments

Comments
 (0)