diff --git a/semver b/semver index 39e0780..4e4d012 100755 --- a/semver +++ b/semver @@ -35,9 +35,9 @@ sub usage() { say STDERR " $program increment-minor "; say STDERR " $program increment-patch "; say STDERR " $program init"; - say STDERR " $program sort -"; + say STDERR " $program sort [-r | --reverse] -"; say STDERR " $program validate "; - say STDERR " $program (-h | --help)"; + say STDERR " $program [-h | --help]"; say STDERR ""; say STDERR "Options:"; say STDERR " -h --help Show this help screen."; @@ -299,7 +299,9 @@ sub semver::init { say "0.0.0"; } -sub semver::sort { +sub semver::sort(&@) { + my ($sort_fn) = @_; + my @lines = ; chomp(@lines); @@ -309,21 +311,25 @@ sub semver::sort { exit 1; } - @lines = sort { - my $result = semver::compare($a, $b); - - if ($result eq $eq) { - return $a cmp $b; - } else { - return $result; - } - } @lines; + @lines = sort $sort_fn @lines; foreach my $line (@lines) { say $line; } } +sub semver::sort_pair { + my ($a, $b) = @_; + + my $result = semver::compare($a, $b); + + if ($result eq $eq) { + return $a cmp $b; + } else { + return $result; + } +} + sub semver::validate { my ($str) = @_; @@ -368,7 +374,11 @@ sub main { } elsif ($subcommand eq "increment-patch") { semver::increment_patch($args[1]); } elsif ($subcommand eq "sort") { - semver::sort(); + if (grep { $_ eq "-r" || $_ eq "--reverse" } @args) { + semver::sort { semver::sort_pair($b, $a) }; + } else { + semver::sort { semver::sort_pair($a, $b) }; + } } elsif ($subcommand eq "validate") { semver::validate($args[1]); } elsif ($subcommand eq "compare") { diff --git a/semver.1 b/semver.1 index 62f8ea4..3d87a72 100644 --- a/semver.1 +++ b/semver.1 @@ -49,6 +49,7 @@ increment-patch init .Nm sort +.Op Fl r | Fl -reverse .Fl .Nm validate @@ -171,9 +172,9 @@ can get the MINOR component of a .Ar version string. .Pp -If +The operation will fail if .Ar version -is invalid, nothing will be printed, and the utility will exit with an error code. +is invalid. .Ss get-patch .Nm can get the PATCH component of a @@ -219,9 +220,7 @@ is invalid. .El .Ss grep .Nm -can parse text from STDIN to extract zero or more Semantic Version strings, in the style of -.Sq grep -\&. It will tolerate quite noisy input. +can parse text from STDIN to extract zero or more Semantic Version strings. It will tolerate quite noisy input. .Pp A candidate version .Sq string @@ -289,11 +288,15 @@ can print the minimum Semantic Version to STDOUT. You can use this as a base-case initializer, for example in a script which fails to find any Semantic Versions in its input. .Ss sort .Nm -can order a list of line-delimited Semantic Version strings from STDIN by precedence, in the style of -.Sq sort -\&. +can sort a list of line-delimited Semantic Version strings from STDIN in precedence order (low-to-high). +.Pp +The sort subcommand understands the following options: +.Bl -tag -width indent +.It Fl r, Fl -reverse +Sort in reverse order (high-to-low). +.El .Pp -If the input is invalid (i.e. it contains anything besides Semantic Versions and line delimiter characters), nothing will be printed, and the utility will exit with an error code. +The operation will fail if the input is invalid (i.e. it contains anything besides Semantic Versions and line delimiter characters). .Pp Note: Some aspects of Semantic Version ordering are undefined in the specification. The .Nm @@ -319,7 +322,7 @@ The .Nm utility understands the following command-line options: .Bl -tag -width indent -.It Fl h, Fl help +.It Fl h, Fl -help Display the usage screen. .El .Sh EXAMPLES diff --git a/test/sort.bats b/test/sort.bats index 91961d3..74a97db 100644 --- a/test/sort.bats +++ b/test/sort.bats @@ -90,4 +90,24 @@ EOF ) [[ "$(semver sort <<< "$input")" = "$expected" ]] +} + +@test "sort: should reverse ordering with the -r (--reverse) flag" { + input=$(cat <