Created
July 9, 2021 21:05
-
-
Save jeanbza/217927ee5a6d632961ea0bf432ba604a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
now, let's convert digraph with `usage` | |
with the following usage, | |
var digraphHelp = usage.Text{ | |
Pages: []usage.Page{{Content: []byte(` | |
Directed graph. | |
Usage: | |
digraph nodes | |
digraph transpose | |
digraph somepath <from> <to> | |
digraph -h,-help | |
digraph -version | |
Options: | |
-h,-help Show this screen. | |
-version Show version. | |
`)}}, | |
Map: map[string]string{}, | |
} | |
getting error, | |
digraph: no field found for "digraph" | |
bit confusing. what is a field? why is it not found? | |
--------------------------- | |
fixed by adding, | |
Map: map[string]string{ | |
"nodes": "entity", | |
"transpose": "entity", | |
"somepath": "entity", | |
}, | |
ok, so you can provide a pre-struct mapping with this map. i get it now! | |
--------------------------- | |
honestly, pretty quick to get started. this is really neat! | |
--------------------------- | |
got -h | --h | -help | --help working with: | |
var digraphHelp = usage.Text{ | |
Pages: []usage.Page{{Content: []byte(` | |
Directed graph. | |
Usage: | |
digraph nodes | |
digraph transpose | |
digraph somepath <from> <to> | |
digraph -h,-help | |
digraph -v,-version | |
Options: | |
-h,-help Show this screen. | |
-v,-version Show version. | |
`)}}, | |
Map: map[string]string{ | |
"nodes": "entity", | |
"transpose": "entity", | |
"somepath": "entity", | |
}, | |
} | |
pretty slick how -foo and --foo are equally valid, without having to specify all of them | |
--------------------------- | |
when I try to -version, i get: | |
digraph: flag v present but not allowed | |
that's odd: | |
1. it's set up the exact same as -help wut? | |
2. why is it saying "flag v present" when it's actually flag -version | |
(-v gives us the same issue) | |
--------------------------- | |
sidenote: not sure if there's a better way to print usage than, | |
if opts.Help { | |
fmt.Println(string(digraphHelp.Pages[0].Content)) | |
return nil | |
} | |
feels a bit janky. i reached for something like usage.Usage(digraphHelp), but it doesn't exist AFAICT | |
also, when would you have multiple pages? probably will find the answer out as i go further down the rabbit hole | |
--------------------------- | |
sidenote: looking at naval_fate, why are we defining -v and -h in both Usage and Options? is that required? bit confusing | |
--------------------------- | |
having a hard time figuring out this "flag v present but not allowed" error :/ | |
seems like everything is in order. wish the error would tell me more | |
--------------------------- | |
ah, this appears to be a bug with usage.peg. when i remove the -h,-help, my issue disappears and -v starts working | |
--------------------------- | |
ah, there are more issues. for example: | |
$ go build . && cat input.txt | ./digraph -help nodes | |
digraph: flag h present but not allowed | |
$ go build . && cat input.txt | ./digraph nodes -help | |
digraph: flag h present but not allowed | |
--------------------------- | |
this should probably be an error, but is not: | |
$ go build . && cat input.txt | ./digraph nodes somepath | |
bar | |
foo | |
gaz | |
--------------------------- | |
similarly, should be an error but is not: | |
$ go build . && cat input.txt | ./digraph nodes nodes | |
bar | |
foo | |
gaz | |
--------------------------- | |
this "command not found" message is broken: | |
$ go build . && cat input.txt | ./digraph foo | |
digraph: no such command "" | |
here, it's entering my default case of opts.Entity, since opts.Entity is being set to empty string. probably instead | |
of empty string for opts.Entity, `usage.Process` should have thrown an error that the input doesn't match `digraphUsage` | |
similarly, when I pass no args (`./digraph`), it again just sets opts.Entity to empty string and continues, when it | |
should throw a usage error | |
--------------------------- | |
subcommands and flags for subcommands are really easy, and intuitive. the following "just works": | |
var digraphHelp = usage.Text{ | |
Pages: []usage.Page{{Content: []byte(` | |
Directed graph. | |
Usage: | |
digraph nodes | |
digraph transpose | |
digraph somepath <from> <to> | |
digraph newcmd foo [-gaz] | |
digraph newcmd bar [-gaz] | |
digraph -h,-help | |
digraph -v,-version | |
Options: | |
-h,-help Show this screen. | |
-v,-version Show version. | |
-gaz Does... nothing, really. | |
`)}}, | |
Map: map[string]string{ | |
"nodes": "entity", | |
"transpose": "entity", | |
"somepath": "entity", | |
"newcmd": "entity", | |
"foo": "newcmdaction", | |
"bar": "newcmdaction", | |
}, | |
} | |
that's very neat! | |
i imagine with pages it'll be quite easy to implement `help subcmd` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment