Repository: lsd-rs/lsd (language: rust)

Issue:
Add --sort flag
Now that we have all the sort options, it would be good to have `--sort` option as well.

From gnu ls:

```
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                             time (-t), version (-v), extension (-X)
```


Discussion / hints:
I would like to implement it

Fix patch:
diff --git a/src/app.rs b/src/app.rs
index 42498f3b6..8597c783d 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -152,6 +152,7 @@ pub fn build() -> App<'static, 'static> {
                 .overrides_with("sizesort")
                 .overrides_with("extensionsort")
                 .overrides_with("versionsort")
+                .overrides_with("sort")
                 .multiple(true)
                 .help("Sort by time modified"),
         )
@@ -162,6 +163,7 @@ pub fn build() -> App<'static, 'static> {
                 .overrides_with("timesort")
                 .overrides_with("extensionsort")
                 .overrides_with("versionsort")
+                .overrides_with("sort")
                 .multiple(true)
                 .help("Sort by size"),
         )
@@ -172,6 +174,7 @@ pub fn build() -> App<'static, 'static> {
                 .overrides_with("sizesort")
                 .overrides_with("timesort")
                 .overrides_with("versionsort")
+                .overrides_with("sort")
                 .multiple(true)
                 .help("Sort by file extension"),
         )
@@ -183,8 +186,22 @@ pub fn build() -> App<'static, 'static> {
                 .overrides_with("timesort")
                 .overrides_with("sizesort")
                 .overrides_with("extensionsort")
+                .overrides_with("sort")
                 .help("Natural sort of (version) numbers within text"),
         )
+        .arg(
+            Arg::with_name("sort")
+                .long("sort")
+                .multiple(true)
+                .possible_values(&["size", "time", "version", "extension"])
+                .takes_value(true)
+                .value_name("WORD")
+                .overrides_with("timesort")
+                .overrides_with("sizesort")
+                .overrides_with("extensionsort")
+                .overrides_with("versionsort")
+                .help("sort by WORD instead of name")
+        )
         .arg(
             Arg::with_name("reverse")
                 .short("r")
diff --git a/src/flags/sorting.rs b/src/flags/sorting.rs
index d1334bf34..d5b80d75f 100644
--- a/src/flags/sorting.rs
+++ b/src/flags/sorting.rs
@@ -49,13 +49,14 @@ impl Configurable<Self> for SortColumn {
     /// If either the "timesort" or "sizesort" arguments are passed, this returns the corresponding
     /// `SortColumn` variant in a [Some]. Otherwise this returns [None].
     fn from_arg_matches(matches: &ArgMatches) -> Option<Self> {
-        if matches.is_present("timesort") {
+        let sort = matches.value_of("sort");
+        if matches.is_present("timesort") || sort == Some("time") {
             Some(Self::Time)
-        } else if matches.is_present("sizesort") {
+        } else if matches.is_present("sizesort") || sort == Some("size") {
             Some(Self::Size)
-        } else if matches.is_present("extensionsort") {
+        } else if matches.is_present("extensionsort") || sort == Some("extension") {
             Some(Self::Extension)
-        } else if matches.is_present("versionsort") {
+        } else if matches.is_present("versionsort") || sort == Some("version") {
             Some(Self::Version)
         } else {
             None
@@ -282,6 +283,47 @@ mod test_sort_column {
         );
     }
 
+    #[test]
+    fn test_from_arg_matches_sort() {
+        let argv = vec!["lsd", "--sort", "time"];
+        let matches = app::build().get_matches_from_safe(argv).unwrap();
+        assert_eq!(
+            Some(SortColumn::Time),
+            SortColumn::from_arg_matches(&matches)
+        );
+
+        let argv = vec!["lsd", "--sort", "size"];
+        let matches = app::build().get_matches_from_safe(argv).unwrap();
+        assert_eq!(
+            Some(SortColumn::Size),
+            SortColumn::from_arg_matches(&matches)
+        );
+
+        let argv = vec!["lsd", "--sort", "extension"];
+        let matches = app::build().get_matches_from_safe(argv).unwrap();
+        assert_eq!(
+            Some(SortColumn::Extension),
+            SortColumn::from_arg_matches(&matches)
+        );
+
+        let argv = vec!["lsd", "--sort", "version"];
+        let matches = app::build().get_matches_from_safe(argv).unwrap();
+        assert_eq!(
+            Some(SortColumn::Version),
+            SortColumn::from_arg_matches(&matches)
+        );
+    }
+
+    #[test]
+    fn test_multi_sort_use_last() {
+        let argv = vec!["lsd", "--sort", "size", "-t", "-S", "-X", "--sort", "time"];
+        let matches = app::build().get_matches_from_safe(argv).unwrap();
+        assert_eq!(
+            Some(SortColumn::Time),
+            SortColumn::from_arg_matches(&matches)
+        );
+    }
+
     #[test]
     fn test_from_config_none() {
         assert_eq!(None, SortColumn::from_config(&Config::with_none()));


Test patch: