Skip to content

Commit edeec0f

Browse files
committed
Improve help text
Instead of using the getopts crate’s dynamically-generated usage string, use a more static one: - The options are organised by category now - You can use `--help --long` to display only the ones that pertain to `--long` - They’re aligned in a table sort of way It could be generated statically, because all the options to change it are determined at compile time, but they’re not, yet...
1 parent 590fb9c commit edeec0f

File tree

1 file changed

+74
-14
lines changed

1 file changed

+74
-14
lines changed

src/options.rs

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,45 @@ impl Options {
3636
#[allow(unused_results)]
3737
pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
3838
let mut opts = getopts::Options::new();
39+
40+
opts.optflag("v", "version", "display version of exa");
41+
opts.optflag("?", "help", "show list of command-line options");
42+
43+
// Display options
3944
opts.optflag("1", "oneline", "display one entry per line");
45+
opts.optflag("G", "grid", "display entries in a grid view (default)");
46+
opts.optflag("l", "long", "display extended details and attributes");
47+
opts.optflag("R", "recurse", "recurse into directories");
48+
opts.optflag("T", "tree", "recurse into subdirectories in a tree view");
49+
opts.optflag("x", "across", "sort multi-column view entries across");
50+
51+
// Filtering and sorting options
52+
opts.optflag("", "group-directories-first", "list directories before other files");
4053
opts.optflag("a", "all", "show dot-files");
54+
opts.optflag("d", "list-dirs", "list directories as regular files");
55+
opts.optflag("r", "reverse", "reverse order of files");
56+
opts.optopt ("s", "sort", "field to sort by", "WORD");
57+
58+
// Long view options
4159
opts.optflag("b", "binary", "use binary prefixes in file sizes");
4260
opts.optflag("B", "bytes", "list file sizes in bytes, without prefixes");
43-
opts.optflag("d", "list-dirs", "list directories as regular files");
4461
opts.optflag("g", "group", "show group as well as user");
45-
opts.optflag("G", "grid", "display entries in a grid view (default)");
46-
opts.optflag("", "group-directories-first", "list directories before other files");
4762
opts.optflag("h", "header", "show a header row at the top");
4863
opts.optflag("H", "links", "show number of hard links");
4964
opts.optflag("i", "inode", "show each file's inode number");
50-
opts.optflag("l", "long", "display extended details and attributes");
5165
opts.optopt ("L", "level", "maximum depth of recursion", "DEPTH");
5266
opts.optflag("m", "modified", "display timestamp of most recent modification");
53-
opts.optflag("r", "reverse", "reverse order of files");
54-
opts.optflag("R", "recurse", "recurse into directories");
55-
opts.optopt ("s", "sort", "field to sort by", "WORD");
5667
opts.optflag("S", "blocks", "show number of file system blocks");
5768
opts.optopt ("t", "time", "which timestamp to show for a file", "WORD");
58-
opts.optflag("T", "tree", "recurse into subdirectories in a tree view");
5969
opts.optflag("u", "accessed", "display timestamp of last access for a file");
6070
opts.optflag("U", "created", "display timestamp of creation for a file");
61-
opts.optflag("x", "across", "sort multi-column view entries across");
62-
63-
opts.optflag("", "version", "display version of exa");
64-
opts.optflag("?", "help", "show list of command-line options");
6571

6672
if cfg!(feature="git") {
6773
opts.optflag("", "git", "show git status");
6874
}
6975

7076
if xattr::ENABLED {
71-
opts.optflag("@", "extended", "display extended attribute keys and sizes in long (-l) output");
77+
opts.optflag("@", "extended", "display extended attribute keys and sizes");
7278
}
7379

7480
let matches = match opts.parse(args) {
@@ -77,7 +83,25 @@ impl Options {
7783
};
7884

7985
if matches.opt_present("help") {
80-
return Err(Misfire::Help(opts.usage("Usage:\n exa [options] [files...]")));
86+
let mut help_string = "Usage:\n exa [options] [files...]\n".to_owned();
87+
88+
if !matches.opt_present("long") {
89+
help_string.push_str(OPTIONS);
90+
}
91+
92+
help_string.push_str(LONG_OPTIONS);
93+
94+
if cfg!(feature="git") {
95+
help_string.push_str(GIT_HELP);
96+
help_string.push('\n');
97+
}
98+
99+
if xattr::ENABLED {
100+
help_string.push_str(EXTENDED_HELP);
101+
help_string.push('\n');
102+
}
103+
104+
return Err(Misfire::Help(help_string));
81105
}
82106
else if matches.opt_present("version") {
83107
return Err(Misfire::Version);
@@ -569,6 +593,42 @@ impl fmt::Display for Misfire {
569593
}
570594
}
571595

596+
static OPTIONS: &'static str = r##"
597+
DISPLAY OPTIONS
598+
-1, --oneline display one entry per line
599+
-G, --grid display entries in a grid view (default)
600+
-l, --long display extended details and attributes
601+
-R, --recurse recurse into directories
602+
-T, --tree recurse into subdirectories in a tree view
603+
-x, --across sort multi-column view entries across
604+
605+
FILTERING AND SORTING OPTIONS
606+
-a, --all show dot-files
607+
-d, --list-dirs list directories as regular files
608+
-r, --reverse reverse order of files
609+
-s, --sort WORD field to sort by
610+
--group-directories-first list directories before other files
611+
"##;
612+
613+
static LONG_OPTIONS: &'static str = r##"
614+
LONG VIEW OPTIONS
615+
-b, --binary use binary prefixes in file sizes
616+
-B, --bytes list file sizes in bytes, without prefixes
617+
-g, --group show group as well as user
618+
-h, --header show a header row at the top
619+
-H, --links show number of hard links
620+
-i, --inode show each file's inode number
621+
-L, --level DEPTH maximum depth of recursion
622+
-m, --modified display timestamp of most recent modification
623+
-S, --blocks show number of file system blocks
624+
-t, --time WORD which timestamp to show for a file
625+
-u, --accessed display timestamp of last access for a file
626+
-U, --created display timestamp of creation for a file
627+
"##;
628+
629+
static GIT_HELP: &'static str = r##" -@, --extended display extended attribute keys and sizes"##;
630+
static EXTENDED_HELP: &'static str = r##" --git show git status for files"##;
631+
572632

573633
#[cfg(test)]
574634
mod test {

0 commit comments

Comments
 (0)