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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::str::FromStr;

use anyhow::bail;
use anyhow::Error;
use anyhow::Result;
use clap::Parser;
use model::BtrfsModelFieldId;
use model::FieldId;
use model::NetworkModelFieldId;
use model::SingleCgroupModelFieldId;
use model::SingleDiskModelFieldId;
use model::SingleNetModelFieldId;
use model::SingleProcessModelFieldId;
use model::SingleQueueModelFieldId;
use model::SystemModelFieldId;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::CommonField;
use crate::DumpField;

/// Field that represents a group of related FieldIds of a Queriable.
/// Shorthand for specifying fields to dump.
pub trait AggField<F: FieldId> {
    fn expand(&self, detail: bool) -> Vec<F>;
}

/// Generic representation of fields accepted by different dump subcommands.
/// Each DumpOptionField is either an aggregation of multiple FieldIds, or a
/// "unit" field which could be either a CommonField or a FieldId.
#[derive(Clone, Debug, PartialEq)]
pub enum DumpOptionField<F: FieldId, A: AggField<F>> {
    Unit(DumpField<F>),
    Agg(A),
}

/// Expand the Agg fields and collect them with other Unit fields.
pub fn expand_fields<F: FieldId + Clone, A: AggField<F>>(
    fields: &[DumpOptionField<F, A>],
    detail: bool,
) -> Vec<DumpField<F>> {
    let mut res = Vec::new();
    for field in fields {
        match field {
            DumpOptionField::Unit(field) => res.push(field.clone()),
            DumpOptionField::Agg(agg) => {
                res.extend(agg.expand(detail).into_iter().map(DumpField::FieldId))
            }
        }
    }
    res
}

/// Used by Clap to parse user provided --fields.
impl<F: FieldId + FromStr, A: AggField<F> + FromStr> FromStr for DumpOptionField<F, A> {
    type Err = Error;

    /// When parsing command line options into DumpOptionField, priority order
    /// is CommonField, AggField, and then FieldId.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(common) = CommonField::from_str(s) {
            Ok(Self::Unit(DumpField::Common(common)))
        } else if let Ok(agg) = A::from_str(s) {
            Ok(Self::Agg(agg))
        } else if let Ok(field_id) = F::from_str(s) {
            Ok(Self::Unit(DumpField::FieldId(field_id)))
        } else {
            bail!("Variant not found: {}", s);
        }
    }
}

/// Used for generating help string that lists all supported fields.
impl<F: FieldId + ToString, A: AggField<F> + ToString> ToString for DumpOptionField<F, A> {
    fn to_string(&self) -> String {
        match self {
            Self::Unit(DumpField::Common(common)) => common.to_string(),
            Self::Unit(DumpField::FieldId(field_id)) => field_id.to_string(),
            Self::Agg(agg) => agg.to_string(),
        }
    }
}

/// Join stringified items with ", ". Used for generating help string that lists
/// all supported fields.
fn join(iter: impl IntoIterator<Item = impl ToString>) -> String {
    iter.into_iter()
        .map(|v| v.to_string())
        .collect::<Vec<_>>()
        .join(", ")
}

// make_option macro will build a enum of tags that map to string values by
// implementing the FromStr trait.
// This is useful when are trying to processing or display fields base on
// a user's input. Here's a use case:
// We display fields in the order of user's input. After we got
// the input array, dfill trait will automatically generate a vec of fns base
// on that array. For example, user input `--fields cpu_usage cpu_user`,
// enum generated by make_option will auto translate string to enum tags. After
// that dfill trait will generate `vec![print_cpu_usage, print_cpu_user]`. And
// the dprint trait will just iterate over the fns and call it with current model.
//
// Another user case is for the select feature, we don't want a giant match
// of string patterns once user select some field to do some operations. Instead,
// we can use a match of enum tags, that will be much faster.
macro_rules! make_option {
    ($name:ident {$($str_field:tt: $enum_field:ident,)*}) => {
        #[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
        pub enum $name {
            $($enum_field,)*
        }

        impl FromStr for $name {
            type Err = Error;

            fn from_str(opt: &str) -> Result<Self> {
                match opt.to_lowercase().as_str() {
                    $($str_field => Ok($name::$enum_field),)*
                    _ => bail!("Fail to parse {}", opt)
                }
            }
        }
    }
}

/// Represents the four sub-model of SystemModel.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum SystemAggField {
    Cpu,
    Mem,
    Vm,
    Stat,
}

impl AggField<SystemModelFieldId> for SystemAggField {
    fn expand(&self, detail: bool) -> Vec<SystemModelFieldId> {
        use model::MemoryModelFieldId as Mem;
        use model::ProcStatModelFieldId as Stat;
        use model::SingleCpuModelFieldId as Cpu;
        use model::SystemModelFieldId as FieldId;
        use model::VmModelFieldId as Vm;

        if detail {
            match self {
                Self::Cpu => enum_iterator::all::<Cpu>()
                    // The Idx field is always -1 (we aggregate all CPUs)
                    .filter(|v| v != &Cpu::Idx)
                    .map(FieldId::Cpu)
                    .collect(),
                Self::Mem => enum_iterator::all::<Mem>().map(FieldId::Mem).collect(),
                Self::Vm => enum_iterator::all::<Vm>().map(FieldId::Vm).collect(),
                Self::Stat => enum_iterator::all::<Stat>().map(FieldId::Stat).collect(),
            }
        } else {
            // Default fields for each group
            match self {
                Self::Cpu => vec![Cpu::UsagePct, Cpu::UserPct, Cpu::SystemPct]
                    .into_iter()
                    .map(FieldId::Cpu)
                    .collect(),
                Self::Mem => vec![Mem::Total, Mem::Free]
                    .into_iter()
                    .map(FieldId::Mem)
                    .collect(),
                Self::Vm => enum_iterator::all::<Vm>().map(FieldId::Vm).collect(),
                Self::Stat => enum_iterator::all::<Stat>().map(FieldId::Stat).collect(),
            }
        }
    }
}

pub type SystemOptionField = DumpOptionField<SystemModelFieldId, SystemAggField>;

pub static DEFAULT_SYSTEM_FIELDS: &[SystemOptionField] = &[
    DumpOptionField::Unit(DumpField::FieldId(SystemModelFieldId::Hostname)),
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Agg(SystemAggField::Cpu),
    DumpOptionField::Agg(SystemAggField::Mem),
    DumpOptionField::Agg(SystemAggField::Vm),
    DumpOptionField::Unit(DumpField::FieldId(SystemModelFieldId::KernelVersion)),
    DumpOptionField::Unit(DumpField::FieldId(SystemModelFieldId::OsRelease)),
    DumpOptionField::Agg(SystemAggField::Stat),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const SYSTEM_ABOUT: &str = "Dump system stats";

/// Generated about message for System dump so supported fields are up-to-date.
static SYSTEM_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {system_fields}

********************** Aggregated fields **********************

* cpu: includes [{agg_cpu_fields}].

* mem: includes [{agg_memory_fields}].

* vm: includes [{agg_vm_fields}].

* stat: includes [{agg_stat_fields}].

* --detail: includes [<agg_field>.*] for each given aggregated field.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

$ below dump system -b "08:30:00" -e "08:30:30" -f datetime vm hostname -O csv

"#,
        about = SYSTEM_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        system_fields = join(enum_iterator::all::<SystemModelFieldId>()),
        agg_cpu_fields = join(SystemAggField::Cpu.expand(false)),
        agg_memory_fields = join(SystemAggField::Mem.expand(false)),
        agg_vm_fields = join(SystemAggField::Vm.expand(false)),
        agg_stat_fields = join(SystemAggField::Stat.expand(false)),
        default_fields = join(DEFAULT_SYSTEM_FIELDS.to_owned()),
    )
});

#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum DiskAggField {
    Read,
    Write,
    Discard,
    FsInfo,
}

impl AggField<SingleDiskModelFieldId> for DiskAggField {
    fn expand(&self, _detail: bool) -> Vec<SingleDiskModelFieldId> {
        use model::SingleDiskModelFieldId::*;

        match self {
            Self::Read => vec![
                ReadBytesPerSec,
                ReadCompleted,
                ReadMerged,
                ReadSectors,
                TimeSpendReadMs,
            ],
            Self::Write => vec![
                WriteBytesPerSec,
                WriteCompleted,
                WriteMerged,
                WriteSectors,
                TimeSpendWriteMs,
            ],
            Self::Discard => vec![
                DiscardBytesPerSec,
                DiscardCompleted,
                DiscardMerged,
                DiscardSectors,
                TimeSpendDiscardMs,
            ],
            Self::FsInfo => vec![DiskUsage, PartitionSize, FilesystemType],
        }
    }
}

pub type DiskOptionField = DumpOptionField<SingleDiskModelFieldId, DiskAggField>;

pub static DEFAULT_DISK_FIELDS: &[DiskOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Unit(DumpField::FieldId(SingleDiskModelFieldId::Name)),
    DumpOptionField::Unit(DumpField::FieldId(
        SingleDiskModelFieldId::DiskTotalBytesPerSec,
    )),
    DumpOptionField::Unit(DumpField::FieldId(SingleDiskModelFieldId::Major)),
    DumpOptionField::Unit(DumpField::FieldId(SingleDiskModelFieldId::Minor)),
    DumpOptionField::Agg(DiskAggField::Read),
    DumpOptionField::Agg(DiskAggField::Write),
    DumpOptionField::Agg(DiskAggField::Discard),
    DumpOptionField::Agg(DiskAggField::FsInfo),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const DISK_ABOUT: &str = "Dump disk stats";

/// Generated about message for System dump so supported fields are up-to-date.
static DISK_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {disk_fields}

********************** Aggregated fields **********************

* read: includes [{agg_read_fields}].

* write: includes [{agg_write_fields}].

* discard: includes [{agg_discard_fields}].

* fs_info: includes [{agg_fsinfo_fields}].

* --detail: no effect.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Simple example:

$ below dump disk -b "08:30:00" -e "08:30:30" -f read write discard -O csv

Output stats for all "nvme0*" matched disk from 08:30:00 to 08:30:30:

$ below dump disk -b "08:30:00" -e "08:30:30" -s name -F nvme0* -O json

Output stats for top 5 read partitions for each time slice from 08:30:00 to 08:30:30:

$ below dump disk -b "08:30:00" -e "08:30:30" -s read_bytes_per_sec --rsort --top 5

"#,
        about = DISK_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        disk_fields = join(enum_iterator::all::<SingleDiskModelFieldId>()),
        agg_read_fields = join(DiskAggField::Read.expand(false)),
        agg_write_fields = join(DiskAggField::Write.expand(false)),
        agg_discard_fields = join(DiskAggField::Discard.expand(false)),
        agg_fsinfo_fields = join(DiskAggField::FsInfo.expand(false)),
        default_fields = join(DEFAULT_DISK_FIELDS.to_owned()),
    )
});

#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum BtrfsAggField {
    DiskUsage,
}

impl AggField<BtrfsModelFieldId> for BtrfsAggField {
    fn expand(&self, _detail: bool) -> Vec<BtrfsModelFieldId> {
        use model::BtrfsModelFieldId::*;

        match self {
            Self::DiskUsage => vec![DiskFraction, DiskBytes],
        }
    }
}

pub type BtrfsOptionField = DumpOptionField<BtrfsModelFieldId, BtrfsAggField>;

pub static DEFAULT_BTRFS_FIELDS: &[BtrfsOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Unit(DumpField::FieldId(BtrfsModelFieldId::Name)),
    DumpOptionField::Agg(BtrfsAggField::DiskUsage),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const BTRFS_ABOUT: &str = "Dump btrfs Stats";

static BTRFS_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {btrfs_fields}

********************** Aggregated fields **********************

* usage: includes [{agg_disk_usage_fields}].

* --detail: no effect.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Simple example:

$ below dump btrfs -b "08:30:00" -e "08:30:30" -f usage -O csv

Output stats for top 5 subvolumes for each time slice from 08:30:00 to 08:30:30:

$ below dump btrfs -b "08:30:00" -e "08:30:30" -s disk_bytes --rsort --top 5

"#,
        about = BTRFS_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        btrfs_fields = join(enum_iterator::all::<BtrfsModelFieldId>()),
        agg_disk_usage_fields = join(BtrfsAggField::DiskUsage.expand(false)),
        default_fields = join(DEFAULT_BTRFS_FIELDS.to_owned()),
    )
});

/// Represents the four sub-model of ProcessModel.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum ProcessAggField {
    Cpu,
    Mem,
    Io,
}

impl AggField<SingleProcessModelFieldId> for ProcessAggField {
    fn expand(&self, detail: bool) -> Vec<SingleProcessModelFieldId> {
        use model::ProcessCpuModelFieldId as Cpu;
        use model::ProcessIoModelFieldId as Io;
        use model::ProcessMemoryModelFieldId as Mem;
        use model::SingleProcessModelFieldId as FieldId;

        if detail {
            match self {
                Self::Cpu => enum_iterator::all::<Cpu>().map(FieldId::Cpu).collect(),
                Self::Mem => enum_iterator::all::<Mem>().map(FieldId::Mem).collect(),
                Self::Io => enum_iterator::all::<Io>().map(FieldId::Io).collect(),
            }
        } else {
            // Default fields for each group
            match self {
                Self::Cpu => vec![FieldId::Cpu(Cpu::UsagePct)],
                Self::Mem => vec![FieldId::Mem(Mem::RssBytes)],
                Self::Io => vec![FieldId::Io(Io::RbytesPerSec), FieldId::Io(Io::WbytesPerSec)],
            }
        }
    }
}

pub type ProcessOptionField = DumpOptionField<SingleProcessModelFieldId, ProcessAggField>;

pub static DEFAULT_PROCESS_FIELDS: &[ProcessOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::Pid)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::Ppid)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::Comm)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::State)),
    DumpOptionField::Agg(ProcessAggField::Cpu),
    DumpOptionField::Agg(ProcessAggField::Mem),
    DumpOptionField::Agg(ProcessAggField::Io),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::UptimeSecs)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::Cgroup)),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::Cmdline)),
    DumpOptionField::Unit(DumpField::FieldId(SingleProcessModelFieldId::ExePath)),
];

const PROCESS_ABOUT: &str = "Dump process stats";

/// Generated about message for Process dump so supported fields are up-to-date.
static PROCESS_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {process_fields}

********************** Aggregated fields **********************

* cpu: includes [{agg_cpu_fields}].

* mem: includes [{agg_memory_fields}].

* io: includes [{agg_io_fields}].

* --detail: includes [<agg_field>.*] for each given aggregated field.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Simple example:

$ below dump process -b "08:30:00" -e "08:30:30" -f comm cpu io.rwbytes_per_sec -O csv

Output stats for all "below*" matched processes from 08:30:00 to 08:30:30:

$ below dump process -b "08:30:00" -e "08:30:30" -s comm -F below* -O json

Output stats for top 5 CPU intense processes for each time slice from 08:30:00 to 08:30:30:

$ below dump process -b "08:30:00" -e "08:30:30" -s cpu.usage_pct --rsort --top 5

"#,
        about = PROCESS_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        process_fields = join(enum_iterator::all::<SingleProcessModelFieldId>()),
        agg_cpu_fields = join(ProcessAggField::Cpu.expand(false)),
        agg_memory_fields = join(ProcessAggField::Mem.expand(false)),
        agg_io_fields = join(ProcessAggField::Io.expand(false)),
        default_fields = join(DEFAULT_PROCESS_FIELDS.to_owned()),
    )
});

/// Represents the four sub-model of SingleCgroupModel.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum CgroupAggField {
    Cpu,
    Mem,
    Io,
    Pids,
    Pressure,
}

impl AggField<SingleCgroupModelFieldId> for CgroupAggField {
    fn expand(&self, detail: bool) -> Vec<SingleCgroupModelFieldId> {
        use model::CgroupCpuModelFieldId as Cpu;
        use model::CgroupIoModelFieldId as Io;
        use model::CgroupMemoryModelFieldId as Mem;
        use model::CgroupPidsModelFieldId as Pid;
        use model::CgroupPressureModelFieldId as Pressure;
        use model::SingleCgroupModelFieldId as FieldId;

        if detail {
            match self {
                Self::Cpu => enum_iterator::all::<Cpu>().map(FieldId::Cpu).collect(),
                Self::Mem => enum_iterator::all::<Mem>().map(FieldId::Mem).collect(),
                Self::Io => enum_iterator::all::<Io>().map(FieldId::Io).collect(),
                Self::Pids => enum_iterator::all::<Pid>().map(FieldId::Pids).collect(),
                Self::Pressure => enum_iterator::all::<Pressure>()
                    .map(FieldId::Pressure)
                    .collect(),
            }
        } else {
            // Default fields for each group
            match self {
                Self::Cpu => vec![FieldId::Cpu(Cpu::UsagePct)],
                Self::Mem => vec![FieldId::Mem(Mem::Total)],
                Self::Io => vec![FieldId::Io(Io::RbytesPerSec), FieldId::Io(Io::WbytesPerSec)],
                Self::Pids => vec![FieldId::Pids(Pid::TidsCurrent)],
                Self::Pressure => vec![
                    FieldId::Pressure(Pressure::CpuSomePct),
                    FieldId::Pressure(Pressure::MemoryFullPct),
                    FieldId::Pressure(Pressure::IoFullPct),
                ],
            }
        }
    }
}

pub type CgroupOptionField = DumpOptionField<SingleCgroupModelFieldId, CgroupAggField>;

pub static DEFAULT_CGROUP_FIELDS: &[CgroupOptionField] = &[
    DumpOptionField::Unit(DumpField::FieldId(SingleCgroupModelFieldId::Name)),
    DumpOptionField::Unit(DumpField::FieldId(SingleCgroupModelFieldId::InodeNumber)),
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Agg(CgroupAggField::Cpu),
    DumpOptionField::Agg(CgroupAggField::Mem),
    DumpOptionField::Agg(CgroupAggField::Io),
    DumpOptionField::Agg(CgroupAggField::Pressure),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const CGROUP_ABOUT: &str = "Dump cgroup stats";

/// Generated about message for Cgroup dump so supported fields are up-to-date.
static CGROUP_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {cgroup_fields}

********************** Aggregated fields **********************

* cpu: includes [{agg_cpu_fields}].

* mem: includes [{agg_memory_fields}].

* io: includes [{agg_io_fields}].

* pressure: includes [{agg_pressure_fields}].

* --detail: includes [<agg_field>.*] for each given aggregated field.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Simple example:

$ below dump cgroup -b "08:30:00" -e "08:30:30" -f name cpu -O csv

Output stats for all cgroups matching pattern "below*" for time slices
from 08:30:00 to 08:30:30:

$ below dump cgroup -b "08:30:00" -e "08:30:30" -s name -F below* -O json

Output stats for top 5 CPU intense cgroups for each time slice
from 08:30:00 to 08:30:30 recursively:

$ below dump cgroup -b "08:30:00" -e "08:30:30" -s cpu.usage_pct --rsort --top 5

"#,
        about = CGROUP_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        cgroup_fields = join(enum_iterator::all::<SingleCgroupModelFieldId>()),
        agg_cpu_fields = join(CgroupAggField::Cpu.expand(false)),
        agg_memory_fields = join(CgroupAggField::Mem.expand(false)),
        agg_io_fields = join(CgroupAggField::Io.expand(false)),
        agg_pressure_fields = join(CgroupAggField::Pressure.expand(false)),
        default_fields = join(DEFAULT_CGROUP_FIELDS.to_owned()),
    )
});

/// Represents the iface sub-models of network model.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum IfaceAggField {
    Rate,
    Rx,
    Tx,
    Ethtool,
}

impl AggField<SingleNetModelFieldId> for IfaceAggField {
    fn expand(&self, detail: bool) -> Vec<SingleNetModelFieldId> {
        use model::SingleNetModelFieldId::*;

        match self {
            Self::Rate => vec![
                RxBytesPerSec,
                TxBytesPerSec,
                ThroughputPerSec,
                RxPacketsPerSec,
                TxPacketsPerSec,
            ],
            Self::Rx => vec![
                RxBytes,
                RxCompressed,
                RxCrcErrors,
                RxDropped,
                RxErrors,
                RxFifoErrors,
                RxFrameErrors,
                RxLengthErrors,
                RxMissedErrors,
                RxNohandler,
                RxOverErrors,
                RxPackets,
            ],
            Self::Tx => vec![
                TxAbortedErrors,
                TxBytes,
                TxCarrierErrors,
                TxCompressed,
                TxDropped,
                TxErrors,
                TxFifoErrors,
                TxHeartbeatErrors,
                TxPackets,
                TxWindowErrors,
            ],
            Self::Ethtool => {
                let mut fields = vec![TxTimeoutPerSec];
                if detail {
                    fields.push(RawStats);
                }
                fields
            }
        }
    }
}

pub type IfaceOptionField = DumpOptionField<SingleNetModelFieldId, IfaceAggField>;

pub static DEFAULT_IFACE_FIELDS: &[IfaceOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Unit(DumpField::FieldId(SingleNetModelFieldId::Collisions)),
    DumpOptionField::Unit(DumpField::FieldId(SingleNetModelFieldId::Multicast)),
    DumpOptionField::Unit(DumpField::FieldId(SingleNetModelFieldId::Interface)),
    DumpOptionField::Agg(IfaceAggField::Rate),
    DumpOptionField::Agg(IfaceAggField::Rx),
    DumpOptionField::Agg(IfaceAggField::Tx),
    DumpOptionField::Agg(IfaceAggField::Ethtool),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const IFACE_ABOUT: &str = "Dump the link layer iface stats";

/// Generated about message for Iface dump so supported fields are up-to-date.
static IFACE_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {iface_fields}

********************** Aggregated fields **********************

* rate: includes [{agg_rate_fields}].

* rx: includes [{agg_rx_fields}].

* tx: includes [{agg_tx_fields}].

* ethtool: includes [{agg_ethtool_fields}].

* --detail: includes `raw_stats` field.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Simple example:

$ below dump iface -b "08:30:00" -e "08:30:30" -f interface rate -O csv

Output stats for all iface stats matching pattern "eth*" for time slices
from 08:30:00 to 08:30:30:

$ below dump iface -b "08:30:00" -e "08:30:30" -s interface -F eth* -O json

"#,
        about = IFACE_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        iface_fields = join(enum_iterator::all::<SingleNetModelFieldId>()),
        agg_rate_fields = join(IfaceAggField::Rate.expand(false)),
        agg_rx_fields = join(IfaceAggField::Rx.expand(false)),
        agg_tx_fields = join(IfaceAggField::Tx.expand(false)),
        agg_ethtool_fields = join(IfaceAggField::Ethtool.expand(false)),
        default_fields = join(DEFAULT_IFACE_FIELDS.to_owned()),
    )
});

/// Represents the ip and icmp sub-models of the network model.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum NetworkAggField {
    Ip,
    Ip6,
    Icmp,
    Icmp6,
}

impl AggField<NetworkModelFieldId> for NetworkAggField {
    fn expand(&self, _detail: bool) -> Vec<NetworkModelFieldId> {
        use model::NetworkModelFieldId as FieldId;
        match self {
            Self::Ip => enum_iterator::all::<model::IpModelFieldId>()
                .map(FieldId::Ip)
                .collect(),
            Self::Ip6 => enum_iterator::all::<model::Ip6ModelFieldId>()
                .map(FieldId::Ip6)
                .collect(),
            Self::Icmp => enum_iterator::all::<model::IcmpModelFieldId>()
                .map(FieldId::Icmp)
                .collect(),
            Self::Icmp6 => enum_iterator::all::<model::Icmp6ModelFieldId>()
                .map(FieldId::Icmp6)
                .collect(),
        }
    }
}

pub type NetworkOptionField = DumpOptionField<NetworkModelFieldId, NetworkAggField>;

pub static DEFAULT_NETWORK_FIELDS: &[NetworkOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Agg(NetworkAggField::Ip),
    DumpOptionField::Agg(NetworkAggField::Ip6),
    DumpOptionField::Agg(NetworkAggField::Icmp),
    DumpOptionField::Agg(NetworkAggField::Icmp6),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const NETWORK_ABOUT: &str = "Dump the network layer stats including ip and icmp";

/// Generated about message for Network dump so supported fields are up-to-date.
static NETWORK_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {network_fields}

********************** Aggregated fields **********************

* ip: includes [{agg_ip_fields}].

* ip6: includes [{agg_ip6_fields}].

* icmp: includes [{agg_icmp_fields}].

* icmp6: includes [{agg_icmp6_fields}].

* --detail: no effect.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Example:

$ below dump network -b "08:30:00" -e "08:30:30" -f ip ip6 -O json

"#,
        about = NETWORK_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        network_fields = join(enum_iterator::all::<NetworkModelFieldId>()),
        agg_ip_fields = join(NetworkAggField::Ip.expand(false)),
        agg_ip6_fields = join(NetworkAggField::Ip6.expand(false)),
        agg_icmp_fields = join(NetworkAggField::Icmp.expand(false)),
        agg_icmp6_fields = join(NetworkAggField::Icmp6.expand(false)),
        default_fields = join(DEFAULT_NETWORK_FIELDS.to_owned()),
    )
});

/// Represents the tcp and udp sub-models of the network model.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum TransportAggField {
    Tcp,
    Udp,
    Udp6,
}

impl AggField<NetworkModelFieldId> for TransportAggField {
    fn expand(&self, _detail: bool) -> Vec<NetworkModelFieldId> {
        use model::NetworkModelFieldId as FieldId;
        match self {
            Self::Tcp => enum_iterator::all::<model::TcpModelFieldId>()
                .map(FieldId::Tcp)
                .collect(),
            Self::Udp => enum_iterator::all::<model::UdpModelFieldId>()
                .map(FieldId::Udp)
                .collect(),
            Self::Udp6 => enum_iterator::all::<model::Udp6ModelFieldId>()
                .map(FieldId::Udp6)
                .collect(),
        }
    }
}

pub type TransportOptionField = DumpOptionField<NetworkModelFieldId, TransportAggField>;

pub static DEFAULT_TRANSPORT_FIELDS: &[TransportOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Agg(TransportAggField::Tcp),
    DumpOptionField::Agg(TransportAggField::Udp),
    DumpOptionField::Agg(TransportAggField::Udp6),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const TRANSPORT_ABOUT: &str = "Dump the transport layer stats including tcp and udp";

/// Generated about message for Transport dump so supported fields are up-to-date.
static TRANSPORT_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, {network_fields}.

********************** Aggregated fields **********************

* tcp: includes [{agg_tcp_fields}].

* udp: includes [{agg_udp_fields}].

* udp6: includes [{agg_udp6_fields}].

* --detail: no effect.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Example:

$ below dump transport -b "08:30:00" -e "08:30:30" -f tcp udp -O json

"#,
        about = TRANSPORT_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        network_fields = join(enum_iterator::all::<NetworkModelFieldId>()),
        agg_tcp_fields = join(TransportAggField::Tcp.expand(false)),
        agg_udp_fields = join(TransportAggField::Udp.expand(false)),
        agg_udp6_fields = join(TransportAggField::Udp6.expand(false)),
        default_fields = join(DEFAULT_TRANSPORT_FIELDS.to_owned()),
    )
});

/// Represents the ethtool queue sub-model of the network model.
#[derive(
    Clone,
    Debug,
    PartialEq,
    below_derive::EnumFromStr,
    below_derive::EnumToString
)]
pub enum EthtoolQueueAggField {
    Queue,
}

impl AggField<SingleQueueModelFieldId> for EthtoolQueueAggField {
    fn expand(&self, detail: bool) -> Vec<SingleQueueModelFieldId> {
        use model::SingleQueueModelFieldId::*;
        let mut fields = vec![
            Interface,
            QueueId,
            RxBytesPerSec,
            TxBytesPerSec,
            RxCountPerSec,
            TxCountPerSec,
            TxMissedTx,
            TxUnmaskInterrupt,
        ];
        if detail {
            fields.push(RawStats);
        }
        match self {
            Self::Queue => fields,
        }
    }
}

pub type EthtoolQueueOptionField = DumpOptionField<SingleQueueModelFieldId, EthtoolQueueAggField>;

pub static DEFAULT_ETHTOOL_QUEUE_FIELDS: &[EthtoolQueueOptionField] = &[
    DumpOptionField::Unit(DumpField::Common(CommonField::Datetime)),
    DumpOptionField::Agg(EthtoolQueueAggField::Queue),
    DumpOptionField::Unit(DumpField::Common(CommonField::Timestamp)),
];

const ETHTOOL_QUEUE_ABOUT: &str = "Dump network interface queue stats";

/// Generated about message for Ethtool Queue dump so supported fields are up-to-date.
static ETHTOOL_QUEUE_LONG_ABOUT: Lazy<String> = Lazy::new(|| {
    format!(
        r#"{about}

********************** Available fields **********************

{common_fields}, and expanded fields below.

********************** Aggregated fields **********************

* queue: includes [{agg_queue_fields}].

* --detail: includes `raw_stats` field.

* --default: includes [{default_fields}].

* --everything: includes everything (equivalent to --default --detail).

********************** Example Commands **********************

Example:

$ below dump ethtool-queue -b "08:30:00" -e "08:30:30" -O json

"#,
        about = ETHTOOL_QUEUE_ABOUT,
        common_fields = join(enum_iterator::all::<CommonField>()),
        agg_queue_fields = join(EthtoolQueueAggField::Queue.expand(false)),
        default_fields = join(DEFAULT_ETHTOOL_QUEUE_FIELDS.to_owned()),
    )
});

make_option! (OutputFormat {
    "raw": Raw,
    "csv": Csv,
    "tsv": Tsv,
    "json": Json,
    "kv": KeyVal,
    "openmetrics": OpenMetrics,
});

#[derive(Debug, Parser, Default, Clone)]
pub struct GeneralOpt {
    /// Show all top layer fields. If --default is specified, it overrides any specified fields via --fields.
    #[clap(long)]
    pub default: bool,
    /// Show all fields. If --everything is specified, --fields and --default are overridden.
    #[clap(long)]
    pub everything: bool,
    /// Show more infomation other than default.
    #[clap(short, long)]
    pub detail: bool,
    /// Begin time, same format as replay
    #[clap(long, short)]
    pub begin: String,
    /// End time, same format as replay
    #[clap(long, short, group = "time")]
    pub end: Option<String>,
    /// Time string specifying the duration, e.g. "10 min"{n}
    /// Keywords: days min, h, sec{n}
    /// Relative: {humantime}, e.g. "2 days 3 hr 15m 10sec"{n}
    /// _
    #[clap(long, group = "time")]
    pub duration: Option<String>,
    /// Take a regex and apply to --select selected field. See command level doc for example.
    #[clap(long, short = 'F')]
    pub filter: Option<Regex>,
    /// Sort (lower to higher) by --select selected field. See command level doc for example.
    #[clap(long)]
    pub sort: bool,
    /// Sort (higher to lower) by --select selected field. See command level doc for example.
    #[clap(long)]
    pub rsort: bool,
    // display top N field. See command level doc for example.
    #[clap(long, default_value = "0")]
    pub top: u32,
    /// Repeat title, for each N line, it will render a line of title. Only for raw output format.
    #[clap(long = "repeat-title")]
    pub repeat_title: Option<usize>,
    /// Output format. Choose from raw, csv, tsv, kv, json, openmetrics. Default to raw
    #[clap(long, short = 'O')]
    pub output_format: Option<OutputFormat>,
    /// Output destination, default to stdout.
    #[clap(long, short)]
    pub output: Option<String>,
    /// Disable title in raw, csv or tsv format output
    #[clap(long)]
    pub disable_title: bool,
    /// Days adjuster, same as -r option in replay.
    #[clap(short = 'r')]
    pub yesterdays: Option<String>,
    /// Line break symbol between samples
    #[clap(long)]
    pub br: Option<String>,
    /// Dump raw data without units or conversion
    #[clap(long)]
    pub raw: bool,
}

#[derive(Debug, Parser, Clone)]
pub enum DumpCommand {
    #[clap(about = SYSTEM_ABOUT, long_about = SYSTEM_LONG_ABOUT.as_str())]
    System {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<SystemOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Saved pattern in the dumprc file under [system] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = DISK_ABOUT, long_about = DISK_LONG_ABOUT.as_str())]
    Disk {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<DiskOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Select field for operation, use with --sort, --rsort, --filter, --top
        #[clap(long, short)]
        select: Option<SingleDiskModelFieldId>,
        /// Saved pattern in the dumprc file under [disk] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = BTRFS_ABOUT, long_about = BTRFS_LONG_ABOUT.as_str())]
    Btrfs {
        /// Select which fields to display and in what order.
        #[clap(short, long)]
        fields: Option<Vec<BtrfsOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Select field for operation, use with --sort, --rsort, --filter, --top
        #[clap(long, short)]
        select: Option<BtrfsModelFieldId>,
        /// Saved pattern in the dumprc file under [btrfs] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = PROCESS_ABOUT, long_about = PROCESS_LONG_ABOUT.as_str())]
    Process {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<ProcessOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Select field for operation, use with --sort, --rsort, --filter, --top
        #[clap(long, short)]
        select: Option<SingleProcessModelFieldId>,
        /// Saved pattern in the dumprc file under [process] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = CGROUP_ABOUT, long_about = CGROUP_LONG_ABOUT.as_str())]
    Cgroup {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<CgroupOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Select field for operation, use with --sort, --rsort, --filter, --top
        #[clap(long, short)]
        select: Option<SingleCgroupModelFieldId>,
        /// Saved pattern in the dumprc file under [cgroup] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = IFACE_ABOUT, long_about = IFACE_LONG_ABOUT.as_str())]
    Iface {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<IfaceOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Select field for operation, use with --filter
        #[clap(long, short)]
        select: Option<SingleNetModelFieldId>,
        /// Saved pattern in the dumprc file under [iface] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = NETWORK_ABOUT, long_about = NETWORK_LONG_ABOUT.as_str())]
    Network {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<NetworkOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Saved pattern in the dumprc file under [network] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = TRANSPORT_ABOUT, long_about = TRANSPORT_LONG_ABOUT.as_str())]
    Transport {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<TransportOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Saved pattern in the dumprc file under [transport] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
    #[clap(about = ETHTOOL_QUEUE_ABOUT, long_about = ETHTOOL_QUEUE_LONG_ABOUT.as_str())]
    EthtoolQueue {
        /// Select which fields to display and in what order.
        #[clap(short, long, num_args = 1..)]
        fields: Option<Vec<EthtoolQueueOptionField>>,
        #[clap(flatten)]
        opts: GeneralOpt,
        /// Saved pattern in the dumprc file under [ethtool] section.
        #[clap(long, short, conflicts_with("fields"))]
        pattern: Option<String>,
    },
}