| 1 | //! Argument parsing via clap. |
| 2 | //! |
| 3 | //! Note that you probably want to keep this as a single file so the build |
| 4 | //! script doesn't trip all over itself. |
| 5 | |
| 6 | // TODO: New sections are misaligned! See if we can get that fixed. |
| 7 | |
| 8 | use std::path::PathBuf; |
| 9 | |
| 10 | use clap::{builder::PossibleValue, *}; |
| 11 | use indoc::indoc; |
| 12 | |
| 13 | const TEMPLATE: &str = indoc! { |
| 14 | "{name} {version} |
| 15 | {author} |
| 16 | |
| 17 | {about} |
| 18 | |
| 19 | {usage-heading} {usage} |
| 20 | |
| 21 | {all-args}" |
| 22 | }; |
| 23 | |
| 24 | const USAGE: &str = "btm [OPTIONS]"; |
| 25 | |
| 26 | // Can't use an unwrap here at the moment since it's const. |
| 27 | const VERSION: &str = match option_env!("NIGHTLY_VERSION") { |
| 28 | Some(nightly_version) => nightly_version, |
| 29 | None => crate_version!(), |
| 30 | }; |
| 31 | |
| 32 | const CHART_WIDGET_POSITIONS: [&str; 9] = [ |
| 33 | "none", |
| 34 | "top-left", |
| 35 | "top", |
| 36 | "top-right", |
| 37 | "left", |
| 38 | "right", |
| 39 | "bottom-left", |
| 40 | "bottom", |
| 41 | "bottom-right", |
| 42 | ]; |
| 43 | |
| 44 | /// Represents the arguments that can be passed in to bottom. |
| 45 | #[derive(Parser, Debug)] |
| 46 | #[command( |
| 47 | name = crate_name!(), |
| 48 | version = VERSION, |
| 49 | author = crate_authors!(), |
| 50 | about = crate_description!(), |
| 51 | disable_help_flag = true, |
| 52 | disable_version_flag = true, |
| 53 | color = ColorChoice::Auto, |
| 54 | help_template = TEMPLATE, |
| 55 | override_usage = USAGE, |
| 56 | )] |
| 57 | pub struct BottomArgs { |
| 58 | #[command(flatten)] |
| 59 | pub general: GeneralArgs, |
| 60 | |
| 61 | #[command(flatten)] |
| 62 | pub process: ProcessArgs, |
| 63 | |
| 64 | #[command(flatten)] |
| 65 | pub temperature: TemperatureArgs, |
| 66 | |
| 67 | #[command(flatten)] |
| 68 | pub cpu: CpuArgs, |
| 69 | |
| 70 | #[command(flatten)] |
| 71 | pub memory: MemoryArgs, |
| 72 | |
| 73 | #[command(flatten)] |
| 74 | pub network: NetworkArgs, |
| 75 | |
| 76 | #[cfg(feature = "battery")] |
| 77 | #[command(flatten)] |
| 78 | pub battery: BatteryArgs, |
| 79 | |
| 80 | #[cfg(feature = "gpu")] |
| 81 | #[command(flatten)] |
| 82 | pub gpu: GpuArgs, |
| 83 | |
| 84 | #[command(flatten)] |
| 85 | pub style: StyleArgs, |
| 86 | |
| 87 | #[command(flatten)] |
| 88 | pub other: OtherArgs, |
| 89 | } |
| 90 | |
| 91 | /// General arguments/config options. |
| 92 | #[derive(Args, Clone, Debug)] |
| 93 | #[command(next_help_heading = "General Options", rename_all = "snake_case")] |
| 94 | pub struct GeneralArgs { |
| 95 | #[arg( |
| 96 | long, |
| 97 | action = ArgAction::SetTrue, |
| 98 | help = "Temporarily shows the time scale in graphs.", |
| 99 | long_help = "Automatically hides the time scale in graphs after being shown for a brief moment when zoomed \ |
| 100 | in/out. If time is disabled using --hide_time then this will have no effect.", |
| 101 | alias = "autohide-time" |
| 102 | )] |
| 103 | pub autohide_time: bool, |
| 104 | |
| 105 | #[arg( |
| 106 | short = 'b', |
| 107 | long, |
| 108 | action = ArgAction::SetTrue, |
| 109 | help = "Hides graphs and uses a more basic look.", |
| 110 | long_help = "Hides graphs and uses a more basic look, largely inspired by htop's design." |
| 111 | )] |
| 112 | pub basic: bool, |
| 113 | |
| 114 | #[arg( |
| 115 | short = 'C', |
| 116 | long, |
| 117 | value_name = "PATH", |
| 118 | value_hint = ValueHint::AnyPath, |
| 119 | help = "Sets the location of the config file.", |
| 120 | long_help = "Sets the location of the config file. Expects a config file in the TOML format. \ |
| 121 | If it doesn't exist, a default config file is created at the path. If no path is provided, \ |
| 122 | the default config location will be used.", |
| 123 | alias = "config-location", |
| 124 | alias = "config", |
| 125 | )] |
| 126 | pub config_location: Option<PathBuf>, |
| 127 | |
| 128 | #[arg( |
| 129 | short = 't', |
| 130 | long, |
| 131 | value_name = "TIME", |
| 132 | help = "Default time value for graphs.", |
| 133 | long_help = "Default time value for graphs. Either a number in milliseconds or a 'human duration' \ |
| 134 | (e.g. 60s, 10m). Defaults to 60s, must be at least 30s.", |
| 135 | alias = "default-time-value" |
| 136 | )] |
| 137 | pub default_time_value: Option<String>, |
| 138 | |
| 139 | // TODO: Charts are broken in the manpage |
| 140 | #[arg( |
| 141 | long, |
| 142 | requires_all = ["default_widget_type"], |
| 143 | value_name = "N", |
| 144 | help = "Sets the N'th selected widget type as the default.", |
| 145 | long_help = indoc! { |
| 146 | "Sets the N'th selected widget type to use as the default widget. Requires 'default_widget_type' to also be \ |
| 147 | set, and defaults to 1. |
| 148 | |
| 149 | This reads from left to right, top to bottom. For example, suppose we have a layout that looks like: |
| 150 | +-------------------+-----------------------+ |
| 151 | | CPU (1) | CPU (2) | |
| 152 | +---------+---------+-------------+---------+ |
| 153 | | Process | CPU (3) | Temperature | CPU (4) | |
| 154 | +---------+---------+-------------+---------+ |
| 155 | |
| 156 | And we set our default widget type to 'CPU'. If we set '--default_widget_count 1', then it would use the \ |
| 157 | CPU (1) as the default widget. If we set '--default_widget_count 3', it would use CPU (3) as the default \ |
| 158 | instead." |
| 159 | }, |
| 160 | alias = "default-widget-count" |
| 161 | )] |
| 162 | pub default_widget_count: Option<u64>, |
| 163 | |
| 164 | #[arg( |
| 165 | long, |
| 166 | value_name = "WIDGET", |
| 167 | help = "Sets the default widget type. Use --help for more info.", |
| 168 | long_help = indoc!{ |
| 169 | "Sets which widget type to use as the default widget. For the default \ |
| 170 | layout, this defaults to the 'process' widget. For a custom layout, it defaults \ |
| 171 | to the first widget it sees. |
| 172 | |
| 173 | For example, suppose we have a layout that looks like: |
| 174 | +-------------------+-----------------------+ |
| 175 | | CPU (1) | CPU (2) | |
| 176 | +---------+---------+-------------+---------+ |
| 177 | | Process | CPU (3) | Temperature | CPU (4) | |
| 178 | +---------+---------+-------------+---------+ |
| 179 | |
| 180 | Then, setting '--default_widget_type temperature' will make the temperature widget selected by default." |
| 181 | }, |
| 182 | value_parser = [ |
| 183 | "cpu", |
| 184 | "mem", |
| 185 | "net", |
| 186 | "network", |
| 187 | "proc", |
| 188 | "process", |
| 189 | "processes", |
| 190 | "temp", |
| 191 | "temperature", |
| 192 | "disk", |
| 193 | #[cfg(feature = "battery")] |
| 194 | "batt", |
| 195 | #[cfg(feature = "battery")] |
| 196 | "battery", |
| 197 | ], |
| 198 | alias = "default-widget-type" |
| 199 | )] |
| 200 | pub default_widget_type: Option<String>, |
| 201 | |
| 202 | #[arg( |
| 203 | long, |
| 204 | action = ArgAction::SetTrue, |
| 205 | help = "Disables mouse clicks.", |
| 206 | long_help = "Disables mouse clicks from interacting with bottom.", |
| 207 | alias = "disable-click" |
| 208 | )] |
| 209 | pub disable_click: bool, |
| 210 | |
| 211 | #[arg( |
| 212 | long, |
| 213 | action = ArgAction::SetTrue, |
| 214 | help = "Disables keyboard shortcuts, INCLUDING the ones that stop bottom.", |
| 215 | long_help = "Disables keyboard shortcuts from interacting with bottom. Note this includes keyboard shortcuts to quit bottom.", |
| 216 | alias = "disable-keys" |
| 217 | )] |
| 218 | pub disable_keys: bool, |
| 219 | |
| 220 | // TODO: Change this to accept a string with the type of marker. |
| 221 | #[arg( |
| 222 | short = 'm', |
| 223 | long, |
| 224 | action = ArgAction::SetTrue, |
| 225 | help = "Uses a dot marker for graphs.", |
| 226 | long_help = "Uses a dot marker for graphs as opposed to the default braille marker.", |
| 227 | alias = "dot-marker" |
| 228 | )] |
| 229 | pub dot_marker: bool, |
| 230 | |
| 231 | #[arg( |
| 232 | short = 'e', |
| 233 | long, |
| 234 | action = ArgAction::SetTrue, |
| 235 | help = "Expand the default widget upon starting the app.", |
| 236 | long_help = "Expand the default widget upon starting the app. This flag has no effect in basic mode (--basic)." |
| 237 | )] |
| 238 | pub expanded: bool, |
| 239 | |
| 240 | #[arg(long, action = ArgAction::SetTrue, help = "Hides the time scale from being shown.", alias = "hide-time")] |
| 241 | pub hide_time: bool, |
| 242 | |
| 243 | #[arg( |
| 244 | short = 'r', |
| 245 | long, |
| 246 | value_name = "TIME", |
| 247 | help = "Sets how often data is refreshed.", |
| 248 | long_help = "Sets how often data is refreshed. Either a number in milliseconds or a 'human duration' \ |
| 249 | (e.g. 1s, 1m). Defaults to 1s, must be at least 250ms. Smaller values may result in \ |
| 250 | higher system resource usage." |
| 251 | )] |
| 252 | pub rate: Option<String>, |
| 253 | |
| 254 | #[arg( |
| 255 | long, |
| 256 | value_name = "TIME", |
| 257 | help = "How far back data will be stored up to.", |
| 258 | long_help = "How far back data will be stored up to. Either a number in milliseconds or a 'human duration' \ |
| 259 | (e.g. 10m, 1h). Defaults to 10 minutes, and must be at least 1 minute. Larger values \ |
| 260 | may result in higher memory usage." |
| 261 | )] |
| 262 | pub retention: Option<String>, |
| 263 | |
| 264 | #[arg( |
| 265 | long, |
| 266 | action = ArgAction::SetTrue, |
| 267 | help = "Shows the list scroll position tracker in the widget title for table widgets.", |
| 268 | alias = "show-table-scroll-position" |
| 269 | )] |
| 270 | pub show_table_scroll_position: bool, |
| 271 | |
| 272 | #[arg( |
| 273 | short = 'd', |
| 274 | long, |
| 275 | value_name = "TIME", |
| 276 | help = "The amount of time changed upon zooming.", |
| 277 | long_help = "The amount of time changed when zooming in/out. Takes a number in \ |
| 278 | milliseconds or a human duration (e.g. 30s). The minimum is 1s, and \ |
| 279 | defaults to 15s.", |
| 280 | alias = "time-delta" |
| 281 | )] |
| 282 | pub time_delta: Option<String>, |
| 283 | } |
| 284 | |
| 285 | /// Process arguments/config options. |
| 286 | #[derive(Args, Clone, Debug, Default)] |
| 287 | #[command(next_help_heading = "Process Options", rename_all = "snake_case")] |
| 288 | pub struct ProcessArgs { |
| 289 | #[arg( |
| 290 | short = 'S', |
| 291 | long, |
| 292 | action = ArgAction::SetTrue, |
| 293 | help = "Enables case sensitivity by default when searching.", |
| 294 | long_help = "Enables case sensitivity by default when searching for a process.", |
| 295 | alias = "case-sensitive" |
| 296 | )] |
| 297 | pub case_sensitive: bool, |
| 298 | |
| 299 | // TODO: Rename this. |
| 300 | #[arg( |
| 301 | short = 'u', |
| 302 | long, |
| 303 | action = ArgAction::SetTrue, |
| 304 | help = "Calculates process CPU usage as a percentage of current usage rather than total usage.", |
| 305 | alias = "current-usage" |
| 306 | )] |
| 307 | pub current_usage: bool, |
| 308 | |
| 309 | #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] |
| 310 | #[arg( |
| 311 | long, |
| 312 | action = ArgAction::SetTrue, |
| 313 | help = "Hides additional stopping options on Unix-like systems.", |
| 314 | long_help = "Hides additional stopping options on Unix-like systems. Signal 15 (TERM) will be sent when \ |
| 315 | stopping a process.", |
| 316 | alias = "disable-advanced-kill" |
| 317 | )] |
| 318 | pub disable_advanced_kill: bool, |
| 319 | |
| 320 | #[arg( |
| 321 | long, |
| 322 | action = ArgAction::SetTrue, |
| 323 | help = "Prevents performing any actions that affect the system.", |
| 324 | long_help = "Prevents performing any actions that affect the system. Disables operations such as stopping or sending signals \ |
| 325 | to processes.", |
| 326 | alias = "read-only" |
| 327 | )] |
| 328 | pub read_only: bool, |
| 329 | |
| 330 | #[cfg(target_os = "linux")] |
| 331 | #[arg( |
| 332 | long, |
| 333 | action = ArgAction::SetTrue, |
| 334 | help = "Hide kernel threads.", |
| 335 | alias = "hide-k-threads" |
| 336 | )] |
| 337 | pub hide_k_threads: bool, |
| 338 | |
| 339 | #[arg( |
| 340 | long, |
| 341 | action = ArgAction::SetTrue, |
| 342 | help = "Also gather process thread information.", |
| 343 | alias = "get-threads", |
| 344 | )] |
| 345 | pub get_threads: bool, |
| 346 | |
| 347 | #[arg( |
| 348 | long, |
| 349 | value_name = "COLUMN", |
| 350 | help = "Sets the default sort column for the process widget.", |
| 351 | long_help = "Sets the default sort column for the process widget. Accepts any of the \ |
| 352 | valid process column names (e.g. \"cpu%\", \"mem\", \"pid\", \"name\"). \ |
| 353 | Overrides the [processes] default_sort setting in the config file.", |
| 354 | alias = "process-default-sort" |
| 355 | )] |
| 356 | pub process_default_sort: Option<String>, |
| 357 | |
| 358 | #[arg( |
| 359 | short = 'g', |
| 360 | long, |
| 361 | action = ArgAction::SetTrue, |
| 362 | help = "Groups processes with the same name by default when searching.", |
| 363 | long_help = "Groups processes with the same name by default when searching. Doesn't do anything if --tree is also set, or \ |
| 364 | tree=true in the config.", |
| 365 | alias = "group-processes" |
| 366 | )] |
| 367 | pub group_processes: bool, |
| 368 | |
| 369 | #[arg( |
| 370 | long, |
| 371 | action = ArgAction::SetTrue, |
| 372 | help = "Defaults to showing process memory usage by value.", |
| 373 | long_help = "Defaults to showing process memory usage by value. Otherwise, it defaults to showing it by percentage.", |
| 374 | alias = "process-memory-as-value" |
| 375 | )] |
| 376 | pub process_memory_as_value: bool, |
| 377 | |
| 378 | #[arg( |
| 379 | long, |
| 380 | action = ArgAction::SetTrue, |
| 381 | help = "Shows the full command name instead of the process name by default.", |
| 382 | alias = "process-command" |
| 383 | )] |
| 384 | pub process_command: bool, |
| 385 | |
| 386 | #[arg(short = 'R', long, action = ArgAction::SetTrue, help = "Enables regex by default while searching.")] |
| 387 | pub regex: bool, |
| 388 | |
| 389 | #[arg( |
| 390 | short = 'T', |
| 391 | long, |
| 392 | action = ArgAction::SetTrue, |
| 393 | help = "Makes the process widget use tree mode by default." |
| 394 | )] |
| 395 | pub tree: bool, |
| 396 | |
| 397 | #[arg( |
| 398 | long, |
| 399 | action = ArgAction::SetTrue, |
| 400 | help = "Collapse process tree by default.", |
| 401 | alias = "tree-collapse" |
| 402 | )] |
| 403 | pub tree_collapse: bool, |
| 404 | |
| 405 | #[arg( |
| 406 | short = 'n', |
| 407 | long, |
| 408 | action = ArgAction::SetTrue, |
| 409 | help = "Show process CPU% usage without averaging over the number of CPU cores.", |
| 410 | alias = "unnormalized-cpu" |
| 411 | )] |
| 412 | pub unnormalized_cpu: bool, |
| 413 | |
| 414 | #[arg( |
| 415 | short = 'W', |
| 416 | long, |
| 417 | action = ArgAction::SetTrue, |
| 418 | help = "Enables whole-word matching by default while searching.", |
| 419 | alias = "whole-word" |
| 420 | )] |
| 421 | pub whole_word: bool, |
| 422 | } |
| 423 | |
| 424 | /// Temperature arguments/config options. |
| 425 | #[derive(Args, Clone, Debug, Default)] |
| 426 | #[command(next_help_heading = "Temperature Options", rename_all = "snake_case")] |
| 427 | #[group(id = "temperature_unit", multiple = false)] |
| 428 | pub struct TemperatureArgs { |
| 429 | #[arg( |
| 430 | short = 'c', |
| 431 | long, |
| 432 | action = ArgAction::SetTrue, |
| 433 | group = "temperature_unit", |
| 434 | help = "Use Celsius as the temperature unit. Default.", |
| 435 | long_help = "Use Celsius as the temperature unit. This is the default option." |
| 436 | )] |
| 437 | pub celsius: bool, |
| 438 | |
| 439 | #[arg( |
| 440 | short = 'f', |
| 441 | long, |
| 442 | action = ArgAction::SetTrue, |
| 443 | group = "temperature_unit", |
| 444 | help = "Use Fahrenheit as the temperature unit." |
| 445 | )] |
| 446 | pub fahrenheit: bool, |
| 447 | |
| 448 | #[arg( |
| 449 | short = 'k', |
| 450 | long, |
| 451 | action = ArgAction::SetTrue, |
| 452 | group = "temperature_unit", |
| 453 | help = "Use Kelvin as the temperature unit." |
| 454 | )] |
| 455 | pub kelvin: bool, |
| 456 | } |
| 457 | |
| 458 | /// The default selection of the CPU widget. If the given selection is invalid, |
| 459 | /// we will fall back to all. |
| 460 | #[derive(Clone, Copy, Debug, Default)] |
| 461 | pub enum CpuDefault { |
| 462 | #[default] |
| 463 | All, |
| 464 | Average, |
| 465 | } |
| 466 | |
| 467 | impl ValueEnum for CpuDefault { |
| 468 | fn value_variants<'a>() -> &'a [Self] { |
| 469 | &[CpuDefault::All, CpuDefault::Average] |
| 470 | } |
| 471 | |
| 472 | fn to_possible_value(&self) -> Option<PossibleValue> { |
| 473 | match self { |
| 474 | CpuDefault::All => Some(PossibleValue::new("all")), |
| 475 | CpuDefault::Average => Some(PossibleValue::new("avg").alias("average")), |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /// CPU arguments/config options. |
| 481 | #[derive(Args, Clone, Debug, Default)] |
| 482 | #[command(next_help_heading = "CPU Options", rename_all = "snake_case")] |
| 483 | pub struct CpuArgs { |
| 484 | // TODO: Maybe rename this or fix this? Should this apply to all "left legends"? |
| 485 | #[arg( |
| 486 | short = 'l', |
| 487 | long, |
| 488 | action = ArgAction::SetTrue, |
| 489 | help = "Puts the CPU chart legend on the left side.", |
| 490 | alias = "cpu-left-legend" |
| 491 | )] |
| 492 | pub cpu_left_legend: bool, |
| 493 | |
| 494 | #[arg( |
| 495 | long, |
| 496 | help = "Sets which CPU entry type is selected by default.", |
| 497 | value_name = "ENTRY", |
| 498 | value_parser = value_parser!(CpuDefault), |
| 499 | alias = "default-cpu-entry" |
| 500 | )] |
| 501 | pub default_cpu_entry: Option<CpuDefault>, |
| 502 | |
| 503 | #[arg( |
| 504 | short = 'a', |
| 505 | long, |
| 506 | action = ArgAction::SetTrue, |
| 507 | help = "Hides the average CPU usage entry.", |
| 508 | alias = "hide-avg-cpu" |
| 509 | )] |
| 510 | pub hide_avg_cpu: bool, |
| 511 | } |
| 512 | |
| 513 | /// Memory argument/config options. |
| 514 | #[derive(Args, Clone, Debug, Default)] |
| 515 | #[command(next_help_heading = "Memory Options", rename_all = "snake_case")] |
| 516 | pub struct MemoryArgs { |
| 517 | #[arg( |
| 518 | long, |
| 519 | value_parser = CHART_WIDGET_POSITIONS, |
| 520 | value_name = "POSITION", |
| 521 | ignore_case = true, |
| 522 | help = "Where to place the legend for the memory chart widget.", |
| 523 | alias = "memory-legend" |
| 524 | )] |
| 525 | pub memory_legend: Option<String>, |
| 526 | |
| 527 | #[cfg(not(target_os = "windows"))] |
| 528 | #[arg( |
| 529 | long, |
| 530 | action = ArgAction::SetTrue, |
| 531 | help = "Enables collecting and displaying cache and buffer memory.", |
| 532 | alias = "enable-cache-memory" |
| 533 | )] |
| 534 | pub enable_cache_memory: bool, |
| 535 | |
| 536 | #[cfg(feature = "zfs")] |
| 537 | #[arg( |
| 538 | long, |
| 539 | action = ArgAction::SetTrue, |
| 540 | help = "Subtract reclaimable ARC from memory.", |
| 541 | alias = "free-arc" |
| 542 | )] |
| 543 | pub free_arc: bool, |
| 544 | |
| 545 | #[cfg(feature = "gpu")] |
| 546 | #[arg( |
| 547 | long, |
| 548 | action = ArgAction::SetTrue, |
| 549 | help = "Use short GPU names (e.g. 'GPU' or 'GPU0', 'GPU1') in the memory widget instead of full GPU names.", |
| 550 | alias = "short-gpu-names" |
| 551 | )] |
| 552 | pub short_gpu_names: bool, |
| 553 | } |
| 554 | |
| 555 | /// Network arguments/config options. |
| 556 | #[derive(Args, Clone, Debug, Default)] |
| 557 | #[command(next_help_heading = "Network Options", rename_all = "snake_case")] |
| 558 | pub struct NetworkArgs { |
| 559 | #[arg( |
| 560 | long, |
| 561 | value_parser = CHART_WIDGET_POSITIONS, |
| 562 | value_name = "POSITION", |
| 563 | ignore_case = true, |
| 564 | help = "Where to place the legend for the network chart widget.", |
| 565 | alias = "network-legend" |
| 566 | )] |
| 567 | pub network_legend: Option<String>, |
| 568 | |
| 569 | // TODO: Rename some of these to remove the network prefix for serde. |
| 570 | #[arg( |
| 571 | long, |
| 572 | action = ArgAction::SetTrue, |
| 573 | help = "Displays the network widget using bytes.", |
| 574 | long_help = "Displays the network widget using bytes. Defaults to bits.", |
| 575 | alias = "network-use-bytes" |
| 576 | )] |
| 577 | pub network_use_bytes: bool, |
| 578 | |
| 579 | #[arg( |
| 580 | long, |
| 581 | action = ArgAction::SetTrue, |
| 582 | help = "Displays the network widget with binary prefixes.", |
| 583 | long_help = "Displays the network widget with binary prefixes (e.g. kibibits, mebibits) rather than a decimal \ |
| 584 | prefixes (e.g. kilobits, megabits). Defaults to decimal prefixes.", |
| 585 | alias = "network-use-binary-prefix" |
| 586 | )] |
| 587 | pub network_use_binary_prefix: bool, |
| 588 | |
| 589 | #[arg( |
| 590 | long, |
| 591 | action = ArgAction::SetTrue, |
| 592 | help = "Displays the network widget with a log scale.", |
| 593 | long_help = "Displays the network widget with a log scale. Defaults to a non-log scale.", |
| 594 | alias = "network-use-log" |
| 595 | )] |
| 596 | pub network_use_log: bool, |
| 597 | |
| 598 | #[arg( |
| 599 | long, |
| 600 | action = ArgAction::SetTrue, |
| 601 | help = "(DEPRECATED) Uses a separate network legend.", |
| 602 | long_help = "(DEPRECATED) Uses separate network widget legend. This display is not tested and may be broken.", |
| 603 | alias = "use-old-network-legend" |
| 604 | )] |
| 605 | pub use_old_network_legend: bool, |
| 606 | |
| 607 | #[arg( |
| 608 | long, |
| 609 | action = ArgAction::SetTrue, |
| 610 | help = "Displays packets information (packet rate and average packet size) in the network widget.", |
| 611 | long_help = "Displays packets information including packet rate (packets per second) and average packet size in the network widget.", |
| 612 | alias = "show-packets" |
| 613 | )] |
| 614 | pub show_packets: bool, |
| 615 | |
| 616 | #[arg( |
| 617 | long, |
| 618 | action = ArgAction::SetTrue, |
| 619 | help = "Show total network usage from app startup rather than on boot.", |
| 620 | long_help = "Zeroes out the total network usage (\"All\") counters so it shows the total usage since the app is started, rather than the total usage since boot.", |
| 621 | alias = "network-start-zeroed" |
| 622 | )] |
| 623 | pub network_start_zeroed: bool, |
| 624 | } |
| 625 | |
| 626 | /// Battery arguments/config options. |
| 627 | #[cfg(feature = "battery")] |
| 628 | #[derive(Args, Clone, Debug, Default)] |
| 629 | #[command(next_help_heading = "Battery Options", rename_all = "snake_case")] |
| 630 | pub struct BatteryArgs { |
| 631 | #[arg( |
| 632 | long, |
| 633 | action = ArgAction::SetTrue, |
| 634 | help = "Shows the battery widget in non-custom layouts.", |
| 635 | long_help = "Shows the battery widget in default or basic mode, if there is as battery available. This \ |
| 636 | has no effect on custom layouts; if the battery widget is desired for a custom layout, explicitly \ |
| 637 | specify it." |
| 638 | )] |
| 639 | pub battery: bool, |
| 640 | } |
| 641 | |
| 642 | /// GPU arguments/config options. |
| 643 | #[cfg(feature = "gpu")] |
| 644 | #[derive(Args, Clone, Debug, Default)] |
| 645 | #[command(next_help_heading = "GPU Options", rename_all = "snake_case")] |
| 646 | pub struct GpuArgs { |
| 647 | #[arg(long, action = ArgAction::SetTrue, help = "Disable collecting and displaying NVIDIA and AMD GPU information.", alias = "disable-gpu")] |
| 648 | pub disable_gpu: bool, |
| 649 | } |
| 650 | |
| 651 | /// Style arguments/config options. |
| 652 | #[derive(Args, Clone, Debug, Default)] |
| 653 | #[command(next_help_heading = "Style Options", rename_all = "snake_case")] |
| 654 | pub struct StyleArgs { |
| 655 | #[arg( |
| 656 | long, |
| 657 | value_name = "SCHEME", |
| 658 | value_parser = [ |
| 659 | "default", |
| 660 | "default-light", |
| 661 | "gruvbox", |
| 662 | "gruvbox-light", |
| 663 | "nord", |
| 664 | "nord-light", |
| 665 | ], |
| 666 | hide_possible_values = true, |
| 667 | help = indoc! { |
| 668 | "Use a built-in colour theme, use '--help' for info on the colours. [possible values: default, default-light, gruvbox, gruvbox-light, nord, nord-light]", |
| 669 | }, |
| 670 | long_help = indoc! { |
| 671 | "Use a pre-defined colour theme. Currently supported themes are: |
| 672 | - default |
| 673 | - default-light (default but adjusted for lighter backgrounds) |
| 674 | - gruvbox (a bright theme with 'retro groove' colours) |
| 675 | - gruvbox-light (gruvbox but adjusted for lighter backgrounds) |
| 676 | - nord (an arctic, north-bluish colour palette) |
| 677 | - nord-light (nord but adjusted for lighter backgrounds)" |
| 678 | } |
| 679 | )] |
| 680 | pub theme: Option<String>, |
| 681 | } |
| 682 | |
| 683 | /// Other arguments. This just handle options that are for help/version |
| 684 | /// displaying. |
| 685 | #[derive(Args, Clone, Debug)] |
| 686 | #[command(next_help_heading = "Other Options", rename_all = "snake_case")] |
| 687 | pub struct OtherArgs { |
| 688 | #[arg(short = 'h', long, action = ArgAction::Help, help = "Prints help info (for more details use '--help').")] |
| 689 | help: (), |
| 690 | |
| 691 | #[arg(short = 'V', long, action = ArgAction::Version, help = "Prints version information.")] |
| 692 | version: (), |
| 693 | } |
| 694 | |
| 695 | /// Parse arguments and return a [`BottomArgs`]. If this fails it will exit the |
| 696 | /// program. |
| 697 | pub fn get_args() -> BottomArgs { |
| 698 | BottomArgs::parse() |
| 699 | } |
| 700 | |
| 701 | /// Returns an [`Command`] based off of [`BottomArgs`]. |
| 702 | #[cfg(test)] |
| 703 | pub(crate) fn build_cmd() -> Command { |
| 704 | BottomArgs::command() |
| 705 | } |
| 706 | |
| 707 | #[cfg(test)] |
| 708 | mod test { |
| 709 | use std::collections::HashSet; |
| 710 | |
| 711 | use super::*; |
| 712 | |
| 713 | #[test] |
| 714 | fn verify_cli() { |
| 715 | build_cmd().debug_assert(); |
| 716 | } |
| 717 | |
| 718 | #[test] |
| 719 | fn no_default_help_heading() { |
| 720 | let mut cmd = build_cmd(); |
| 721 | |
| 722 | let help_str = cmd.render_help(); |
| 723 | assert!( |
| 724 | !help_str.to_string().contains("\nOptions:\n"), |
| 725 | "the default 'Options' heading should not exist; if it does then an argument is \ |
| 726 | missing a help heading." |
| 727 | ); |
| 728 | |
| 729 | let long_help_str = cmd.render_long_help(); |
| 730 | assert!( |
| 731 | !long_help_str.to_string().contains("\nOptions:\n"), |
| 732 | "the default 'Options' heading should not exist; if it does then an argument is \ |
| 733 | missing a help heading." |
| 734 | ); |
| 735 | } |
| 736 | |
| 737 | #[test] |
| 738 | fn catch_incorrect_long_args() { |
| 739 | // Set this to allow certain ones through if needed. |
| 740 | let allow_list: HashSet<&str> = vec![].into_iter().collect(); |
| 741 | let cmd = build_cmd(); |
| 742 | |
| 743 | for opt in cmd.get_opts() { |
| 744 | let long_flag = opt.get_long().unwrap(); |
| 745 | |
| 746 | if !allow_list.contains(long_flag) { |
| 747 | assert!( |
| 748 | long_flag.len() < 30, |
| 749 | "the long help arg '{long_flag}' might be set wrong, please take a look!" |
| 750 | ); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | #[test] |
| 756 | fn catch_missing_hyphen_alias() { |
| 757 | let cmd = build_cmd(); |
| 758 | |
| 759 | for opt in cmd.get_opts() { |
| 760 | let long_flag = opt.get_long().unwrap(); |
| 761 | if long_flag.contains("_") { |
| 762 | let aliased_version = long_flag.replace("_", "-"); |
| 763 | let stored_alias = opt.get_aliases().unwrap_or_else(|| { |
| 764 | panic!("'{long_flag}' should have an alias, if not, it's missing") |
| 765 | }); |
| 766 | |
| 767 | assert!( |
| 768 | stored_alias.contains(&aliased_version.as_str()), |
| 769 | "'{long_flag}' has an incorrectly defined alias" |
| 770 | ); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |