Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update formatting help #6359

Merged
merged 2 commits into from
Oct 4, 2022
Merged

update formatting help #6359

merged 2 commits into from
Oct 4, 2022

Conversation

mntlty
Copy link
Collaborator

@mntlty mntlty commented Sep 28, 2022

closes #6342

Adds more details to gh formatting -h

@mntlty mntlty marked this pull request as ready for review October 3, 2022 10:25
@mntlty mntlty requested a review from a team as a code owner October 3, 2022 10:25
@mntlty mntlty requested review from samcoe and removed request for a team October 3, 2022 10:25
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Oct 3, 2022
@cliAutomation cliAutomation added this to Needs review 🤔 in The GitHub CLI Oct 3, 2022
@mntlty mntlty changed the title mntlty/formatting help update formatting help Oct 3, 2022
# format a pull request using multiple tables with headers
# default output format
$ gh pr list
Showing 23 of 23 open pull requests in cli/cli
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included example output from each of the example commands so users can view them and compare without needing to run the commands, which could be helpful in the web view. The downside is it's not consistent with other examples, and a lot more verbose which may be distracting, and risks getting out of date at some point in the future.

Leaving it in for discussion but it can definitely be removed

pkg/cmd/root/help_topic.go Outdated Show resolved Hide resolved
]


# adding the --jq flag and filtering the output to select the second result
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it makes sense to add a more complex jq example as well, or if that would be more confusing than it's worth

Copy link
Contributor

@mislav mislav Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change an example to something that prints raw values of leaf nodes, rather than printing a nested JSON structure. For example: .[].author.login prints

monalisa
codercat
cli-maintainer

The result of such jq expression can then be piped into a line-based script such as shell loop, which makes it more useful in my mind than outputting JSON.

@mntlty
Copy link
Collaborator Author

mntlty commented Oct 3, 2022

I wasn't 100 percent sure what should have backticks around it and what shouldn't, I added them around command names but not flag names.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

General suggestions:

  • I would enclose --flag names in backticks for consistency and for readability, since encountering -- in prose can be disorienting, and since inline code tags will look better both in generated HTML and in generated man pages;
  • Note that we hard wrap our help text to 80 columns since there is nothing that will automatically wrap our help output for us. We aren't consistent with this, but some wrapping is better than completely unwrapped since some people can have ridiculously wide terminal windows but probably would not like to read paragraphs stretched across a whole ultrawide monitor.

- %[1]struncate <length> <input>%[1]s: ensures input fits within length
By default, the result of %[1]sgh%[1]s commands are output in line-based plain text format. Some commands support passing the --json flag, which converts the output to JSON format. Once in JSON, the output can be further formatted according to a required formatting string by adding either the --jq or --template flag. This is useful for selecting a subset of data, creating new data structures, displaying the data in a different format, or as input to another command line script.

The --json flag requires a comma separated list of fields which match the JSON field names from an API response. For a list of available fields, see: <https://docs.github.com/en/graphql/reference/>. Note that you must pass the --json flag and field names to use the --jq or --template flags.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"For a list of available fields, see GraphQL reference" — this isn't correct, since our json fields do not necessarily have 1-1 mapping to our GraphQL schema (sometimes they are just made up). The previous documentation which you removed was correct:

Use the flag without a value to get the list of available fields.

If that sentence wasn't clear, perhaps you can modify it to make it clearer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for clarifying, I wasn't sure about that and should have called it out in the PR. I think this captures it, let me know what you think!


The --json flag requires a comma separated list of fields which match the JSON field names from an API response. For a list of available fields, see: <https://docs.github.com/en/graphql/reference/>. Note that you must pass the --json flag and field names to use the --jq or --template flags.

The --jq flag requires a string argument in %[1]sjq%[1]s query syntax, and will only print those JSON values which match the query. %[1]sjq%[1]s queries can be used to select elements from an array, fields from an object, create a new array, and more. The %[1]sjq%[1]s utility does not need to be installed on the system to use this formatting directive. To learn about %[1]sjq%[1]s query syntax, see: <https://stedolan.github.io/jq/manual/v1.6/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this paragraph, "jq" is a proper noun of not just the command-line utility but also of the query language, and since we're mostly referencing the query language here, jq needn't be quoted in backticks.

This is equivalent to piping the output to jq -r,

I thought this bit of the old documentation was useful to draw a parallel between using --jq gh flag and the jq utility directly. Do you think it was too noisy to keep in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't quite sure what the -r flag was for, and had to look it up in the jq documentation. To be honest, I'm still not sure what it is meant for. My feeling was that it probably makes sense to people who are very familiar with jq and confusing to anyone who isn't. If you think it's important of course it should be added back in

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think was a useful distinction to point out that using --jq is not equivalent to piping the --json output to jq using the same expression.

gh pr view --json author --jq .author.login
#=> mntlty
gh pr view --json author | jq .author.login
#=> "mntlty"

pkg/cmd/root/help_topic.go Outdated Show resolved Hide resolved
]


# adding the --jq flag and filtering the output to select the second result
Copy link
Contributor

@mislav mislav Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change an example to something that prints raw values of leaf nodes, rather than printing a nested JSON structure. For example: .[].author.login prints

monalisa
codercat
cli-maintainer

The result of such jq expression can then be piped into a line-based script such as shell loop, which makes it more useful in my mind than outputting JSON.

Comment on lines +156 to +157
$ gh pr list --json number,title,headRefName,updatedAt --template \
'{{range .}}{{tablerow (printf "#%v" .number | autocolor "green") .title .headRefName (timeago .updatedAt)}}{{end}}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This --template example outputs the exact format of regular gh pr list, which isn't very compelling as an example for using --template. How about changing something about this expression so that the final output is somehow different (and potentially more useful to some people) than regular gh pr list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that. I think this is more helpful because it's already known, and you can see how to sort of reverse engineer it using templates. I personally find a small tweak or revealing how something is done is easier for me to pattern match against. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it can stay like this if the goal was to demonstrate how one can reverse-engineer the default output. But, from the user's perspective, they might be scanning these examples and seeing how one simple gh invocation and one seemingly very complicated gh invocation both produce the same result.

I'll leave the final decision up to you. The example itself is indeed useful as a starting point for editing one's template.

pkg/cmd/root/help_topic.go Outdated Show resolved Hide resolved
- %[1]stimeago <time>%[1]s: renders a timestamp as relative to now
- %[1]stimefmt <format> <time>%[1]s: formats a timestamp using Go's Time.Format function
- %[1]struncate <length> <input>%[1]s: ensures input fits within length
By default, the result of %[1]sgh%[1]s commands are output in line-based plain text format. Some commands support passing the --json flag, which converts the output to JSON format. Once in JSON, the output can be further formatted according to a required formatting string by adding either the --jq or --template flag. This is useful for selecting a subset of data, creating new data structures, displaying the data in a different format, or as input to another command line script.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The result … is" instead of "The result … are" sounds better to me

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are because of the plural of commands, not the singular of result, sounds correct to me when reading the whole sentence 🤷 I honestly am not sure about this one

@mntlty
Copy link
Collaborator Author

mntlty commented Oct 3, 2022

This looks great!

General suggestions:

  • I would enclose --flag names in backticks for consistency and for readability, since encountering -- in prose can be disorienting, and since inline code tags will look better both in generated HTML and in generated man pages;
  • Note that we hard wrap our help text to 80 columns since there is nothing that will automatically wrap our help output for us. We aren't consistent with this, but some wrapping is better than completely unwrapped since some people can have ridiculously wide terminal windows but probably would not like to read paragraphs stretched across a whole ultrawide monitor.

done! some of the lines look longer in the go code but with the string substitution the backtick makes them shorter when they are rendered. they're now almost all the same length in the terminal, with a bit of wiggle room if it made the sentence a bit easier to parse based on where I added the line break

@mntlty
Copy link
Collaborator Author

mntlty commented Oct 3, 2022

@mislav I think I addressed all the feedback, it's hard to do long form text in a PR. please update anything that doesn't look right or let me know if you'd like to discuss further 🙏

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thank you!

To learn more about Go templates, see: <https://golang.org/pkg/text/template/>.
By default, the result of %[1]sgh%[1]s commands are output in line-based plain text format.
Some commands support passing the %[1]s--json%[1]s flag, which converts the output to JSON format.
Once in JSON, the output can be further formatted according to a required formatting string by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've missed this in my original review: “according to a required formatting string” doesn't sound right to me. When using --json, passing a formatting string isn't required at all. Also, “formatting according to … formatting string” sound repetitive. How about:

Once in JSON, the output can be further formatted using either --jq or --template flag.

creating new data structures, displaying the data in a different format, or as input to another
command line script.

The %[1]s--json%[1]s flag requires a comma separated list of fields to fetch. To view the possible JSON
Copy link
Contributor

@mislav mislav Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think I understand now. You have been using the pattern “the --foo flag requires so and so value”, which reads odd to me because I haven't seen this phrasing before, but it is technically correct that all of these flags require a value. Only boolean flags do not need a value.

Elsewhere, I think we've been using “the --foo flag accepts so and so value”, which reads a bit softer than “requires”, but ultimately there are no hard and fast rules around this.

The GitHub CLI automation moved this from Needs review 🤔 to Needs to be merged 🎉 Oct 4, 2022
@vilmibm vilmibm merged commit f82c6a6 into trunk Oct 4, 2022
The GitHub CLI automation moved this from Needs to be merged 🎉 to Pending Release 🥚 Oct 4, 2022
@vilmibm vilmibm deleted the mntlty/formatting_help branch October 4, 2022 19:04
@github-actions github-actions bot moved this from Pending Release 🥚 to Done 💤 in The GitHub CLI Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
No open projects
The GitHub CLI
  
Done 💤
Development

Successfully merging this pull request may close these issues.

Improve formatting docs
4 participants