vmm: Alphabetically sort CLI options in --help output#6988
vmm: Alphabetically sort CLI options in --help output#6988rbradford merged 1 commit intocloud-hypervisor:mainfrom
Conversation
cc5408c to
b6b10de
Compare
b6b10de to
264b12c
Compare
b3cbe31 to
cc1bc1d
Compare
src/main.rs
Outdated
| // Debug-only check. | ||
| let is_sorted = args.is_sorted_by(|a, b| a.get_id().cmp(b.get_id()) != Ordering::Greater); | ||
| debug_assert!(is_sorted); |
There was a problem hiding this comment.
IMHO we can just used assert! here, because if someone forgets to keep the order, our CI will complain about it 🤔
There was a problem hiding this comment.
It feels a bit weird, as we misuse a runtime check to verify a property of compile time data. But I don't see a better solution, and I think keeping this kind of check in any form is useful
I tried something like const fn get_cli_args_sorted() but this fails for various reasons:
diff --git a/src/main.rs b/src/main.rs
index 09126502..694c5c5f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -177,17 +177,7 @@ fn default_rng() -> String {
format!("src={}", vm_config::DEFAULT_RNG_SOURCE)
}
-/// Creates the CLI definition of Cloud Hypervisor.
-fn create_app(default_vcpus: String, default_memory: String, default_rng: String) -> Command {
- let groups = [
- ArgGroup::new("vm-config")
- .multiple(true)
- .requires("vm-payload"),
- ArgGroup::new("vmm-config").multiple(true),
- ArgGroup::new("logging").multiple(true),
- ArgGroup::new("vm-payload").multiple(true),
- ];
-
+const fn get_cli_args_sorted(default_vcpus: String, default_memory: String, default_rng: String) -> [Arg; 37] {
// All CLI arguments in alphabetical order. PLEASE keep the list of commands alphabetically
// sorted as this order is taken for the `--help` output.
let args = [
@@ -462,9 +452,24 @@ fn create_app(default_vcpus: String, default_memory: String, default_rng: String
.group("vm-config"),
];
- // Debug-only check.
let is_sorted = args.is_sorted_by(|a, b| a.get_id().cmp(b.get_id()) != Ordering::Greater);
- debug_assert!(is_sorted);
+ // Compile time check: const function
+ assert!(is_sorted);
+ args
+}
+
+/// Creates the CLI definition of Cloud Hypervisor.
+fn create_app(default_vcpus: String, default_memory: String, default_rng: String) -> Command {
+ let groups = [
+ ArgGroup::new("vm-config")
+ .multiple(true)
+ .requires("vm-payload"),
+ ArgGroup::new("vmm-config").multiple(true),
+ ArgGroup::new("logging").multiple(true),
+ ArgGroup::new("vm-payload").multiple(true),
+ ];
+
+ let args = get_cli_args_sorted(default_vcpus, default_memory, default_rng);
Command::new("cloud-hypervisor")
// 'BUILD_VERSION' is set by the build script 'build.rs' at
There was a problem hiding this comment.
Yep, IMO the idea here is to have a alphabetically sorted help output and args layout in our code.
We could put this sorted check into our unit test then, we separate the args section and test it in unit tests
There was a problem hiding this comment.
yes, probably the best solution. Thanks! Please re-check
cc1bc1d to
5f4bf55
Compare
b5c8fef to
5afef9e
Compare
RuoqingHe
left a comment
There was a problem hiding this comment.
I'm not sure this is the best way of organizing the commits, but the code looks good to me, thanks for your patience ❤️
|
I'd rather not have the commit to reorder the |
The CLI has grown to a big variety of options. clap prints them in the help message (--help) in the order they were defined. We now are at a point where grouping things logically together doesn't work well. Further, there is no support by clap for logical grouping and the current code base wasn't consistent. Therefore, this commit introduces two changes: - a new structure to define arguments (all in an array) - an alphabetical ordering of the arguments No other changes have been made. No options have been altered. This significantly improves: - code maintainability and extensibility - readability of the --help output A unit test ensures they stay sorted. A better approach to check if the list of arguments (known at build time) is sorted would be a compile time check (`const`), but this currently isn't possible in stable Rust. Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
5afef9e to
c37c639
Compare
I updated the PR. Are we good to go now? |
This enables to sort them alphabetically in a next step, similar to cloud-hypervisor#6988 / c37c639. [0] cloud-hypervisor#6988 Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
This enables to sort them alphabetically in a next step, similar to cloud-hypervisor#6988 / c37c639. [0] cloud-hypervisor#6988 Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
This enables to sort them alphabetically in a next step, similar to cloud-hypervisor#6988 / c37c639. [0] cloud-hypervisor#6988 Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
This enables to sort them alphabetically in a next step, similar to cloud-hypervisor#6988 / c37c639. [0] cloud-hypervisor#6988 Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
The CLI has grown to a big variety of options. clap prints them in the
help message (--help) in the order they were defined. We now are at a
point where grouping things logically together doesn't work well.
Further, there is no support by clap for logical grouping and the
current code base wasn't consistent. Therefore, this commit introduces
two changes:
No other changes have been made. No options have been altered.
This significantly improves:
Signed-off-by: Philipp Schuster philipp.schuster@cyberus-technology.de
CLI --help Before
Details
CLI --help After
Details