Skip to content

Commit

Permalink
Merge pull request #5 from rsteube/flatten-flags
Browse files Browse the repository at this point in the history
flatten flags
  • Loading branch information
rsteube authored Nov 24, 2023
2 parents 083934c + 046414a commit b47c1df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
name: touch
description: change file timestamps
flags:
--date: parse STRING and use it instead of current time
--help: display this help and exit
--no-create: do not create any files
--no-dereference: affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
--reference: use this file's times instead of current time
--time: 'change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m'
--version: output version information and exit
-a: change only the access time
-c: do not create any files
-d: parse STRING and use it instead of current time
-c, --no-create: do not create any files
-d, --date: parse STRING and use it instead of current time
-f: (ignored)
-h: affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
-h, --no-dereference: affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
-m: change only the modification time
-r: use this file's times instead of current time
-r, --reference: use this file's times instead of current time
-t: use [[CC]YY]MMDDhhmm[.ss] instead of current time
completion:
positionalany:
Expand All @@ -37,7 +33,6 @@ completion:
> - invalid subcommands (`-` in manpage name is assumed as subcommand delimiter)
> - description not truncated well
> - all flags are assumed boolean
> - shorthand flags aren't grouped with their longhand
>
> It is recommended to prepare them manually for [carapace-parse] instead.

Expand Down
19 changes: 18 additions & 1 deletion spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os/exec"
"regexp"
"sort"
"strings"

"github.com/lmorg/murex/utils/man"
Expand All @@ -28,6 +29,22 @@ func getPages(exe string) (map[string]string, error) {
return pages, nil
}

func flatten(m map[string]string) map[string]string {
byDescription := make(map[string][]string)
for key, value := range m {
byDescription[value] = append(byDescription[value], key)
}

flattened := make(map[string]string)
for key, value := range byDescription {
sort.Slice(value, func(i, j int) bool {
return len(value[i]) < len(value[j])
})
flattened[strings.Join(value, ", ")] = key
}
return flattened
}

func parse(manpage string) (*command.Command, error) {
_, m := man.ParseByStdio(strings.NewReader(manpage))

Expand All @@ -41,7 +58,7 @@ func parse(manpage string) (*command.Command, error) {
}
cmd.Completion.PositionalAny = []string{"$files"}

for flag, description := range m {
for flag, description := range flatten(m) {
if strings.HasPrefix(description, "eg: ") {
_, description, _ = strings.Cut(description, "--") // TODO check if found
}
Expand Down

0 comments on commit b47c1df

Please sign in to comment.