From ab6ef0e897797db5b867649682f75822606d8a27 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Mon, 25 Mar 2024 07:58:18 +0100 Subject: [PATCH] docs: improve command usage documentation (#709) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR makes command usage documentation consistent by enforcing [docopt](http://docopt.org/) rules and also defining and documenting other additional conventions. Fixes #708 --------- Co-authored-by: Julian Tölle --- CONTRIBUTING.md | 10 ++++++++++ internal/cmd/all/list.go | 2 +- internal/cmd/base/delete.go | 7 ++++++- internal/cmd/base/describe.go | 2 +- internal/cmd/base/labels.go | 4 ++-- internal/cmd/base/list.go | 2 +- internal/cmd/base/set_rdns.go | 2 +- internal/cmd/base/update.go | 2 +- internal/cmd/certificate/create.go | 2 +- internal/cmd/completion/completion.go | 2 +- internal/cmd/context/active.go | 2 +- internal/cmd/context/context.go | 2 +- internal/cmd/context/create.go | 2 +- internal/cmd/context/delete.go | 2 +- internal/cmd/context/list.go | 2 +- internal/cmd/context/use.go | 2 +- internal/cmd/firewall/add_rule.go | 2 +- internal/cmd/firewall/apply_to_resource.go | 2 +- internal/cmd/firewall/create.go | 2 +- internal/cmd/firewall/delete_rule.go | 2 +- internal/cmd/firewall/remove_from_resource.go | 2 +- internal/cmd/firewall/replace_rules.go | 2 +- internal/cmd/floatingip/assign.go | 2 +- internal/cmd/floatingip/create.go | 2 +- internal/cmd/floatingip/disable_protection.go | 2 +- internal/cmd/floatingip/enable_protection.go | 2 +- internal/cmd/floatingip/unassign.go | 2 +- internal/cmd/image/disable_protection.go | 2 +- internal/cmd/image/enable_protection.go | 2 +- internal/cmd/loadbalancer/add_service.go | 2 +- internal/cmd/loadbalancer/add_target.go | 2 +- internal/cmd/loadbalancer/attach_to_network.go | 2 +- internal/cmd/loadbalancer/change_algorithm.go | 2 +- internal/cmd/loadbalancer/change_type.go | 2 +- internal/cmd/loadbalancer/create.go | 2 +- internal/cmd/loadbalancer/delete_service.go | 2 +- internal/cmd/loadbalancer/detach_from_network.go | 2 +- internal/cmd/loadbalancer/disable_protection.go | 2 +- internal/cmd/loadbalancer/disable_public_interface.go | 2 +- internal/cmd/loadbalancer/enable_protection.go | 2 +- internal/cmd/loadbalancer/enable_public_interface.go | 2 +- internal/cmd/loadbalancer/metrics.go | 3 ++- internal/cmd/loadbalancer/remove_target.go | 2 +- internal/cmd/loadbalancer/update_service.go | 2 +- internal/cmd/network/add_route.go | 2 +- internal/cmd/network/add_subnet.go | 2 +- internal/cmd/network/change_ip_range.go | 2 +- internal/cmd/network/create.go | 2 +- internal/cmd/network/disable_protection.go | 2 +- internal/cmd/network/enable_protection.go | 2 +- internal/cmd/network/expose_routes_to_vswitch.go | 2 +- internal/cmd/network/remove_route.go | 2 +- internal/cmd/network/remove_subnet.go | 2 +- internal/cmd/placementgroup/create.go | 2 +- internal/cmd/primaryip/assign.go | 2 +- internal/cmd/primaryip/create.go | 2 +- internal/cmd/primaryip/disable_protection.go | 2 +- internal/cmd/primaryip/enable_protection.go | 2 +- internal/cmd/primaryip/unassign.go | 2 +- internal/cmd/server/add_to_placement_group.go | 2 +- internal/cmd/server/attach_iso.go | 2 +- internal/cmd/server/attach_to_network.go | 2 +- internal/cmd/server/change_alias_ips.go | 2 +- internal/cmd/server/change_type.go | 2 +- internal/cmd/server/create.go | 2 +- internal/cmd/server/create_image.go | 2 +- internal/cmd/server/detach_from_network.go | 2 +- internal/cmd/server/detach_iso.go | 2 +- internal/cmd/server/disable_backup.go | 2 +- internal/cmd/server/disable_protection.go | 2 +- internal/cmd/server/disable_rescue.go | 2 +- internal/cmd/server/enable_backup.go | 2 +- internal/cmd/server/enable_protection.go | 2 +- internal/cmd/server/enable_rescue.go | 2 +- internal/cmd/server/ip.go | 2 +- internal/cmd/server/metrics.go | 3 ++- internal/cmd/server/poweroff.go | 2 +- internal/cmd/server/poweron.go | 2 +- internal/cmd/server/reboot.go | 2 +- internal/cmd/server/rebuild.go | 2 +- internal/cmd/server/remove_from_placement_group.go | 2 +- internal/cmd/server/request_console.go | 2 +- internal/cmd/server/reset.go | 2 +- internal/cmd/server/reset_password.go | 2 +- internal/cmd/server/shutdown.go | 2 +- internal/cmd/server/ssh.go | 2 +- internal/cmd/sshkey/create.go | 2 +- internal/cmd/volume/attach.go | 2 +- internal/cmd/volume/create.go | 2 +- internal/cmd/volume/detach.go | 2 +- internal/cmd/volume/disable_protection.go | 2 +- internal/cmd/volume/enable_protection.go | 2 +- internal/cmd/volume/resize.go | 2 +- 93 files changed, 110 insertions(+), 93 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 71a9bda5..93267bbf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -35,6 +35,16 @@ goreleaser --snapshot --skip-publish --rm-dist ## Conventions +### Command usage + +Command usage descriptions (namely `cobra.Command.Use`) should follow the [docopt](http://docopt.org/) syntax. + +Additionally: +- Optional flags should be the first in the usage string. + - If there is more than one optional flag, you can use `[options]` to abbreviate. +- Required flags should always be documented explicitly and before the positional arguments, but after the + optional flags. + ### Subcommand groups Cobra offers the functionality to group subcommands. The conventions on when and how to group commands are as follows: diff --git a/internal/cmd/all/list.go b/internal/cmd/all/list.go index 37a9ac12..efa56950 100644 --- a/internal/cmd/all/list.go +++ b/internal/cmd/all/list.go @@ -50,7 +50,7 @@ var ListCmd = base.Cmd{ } cmd := &cobra.Command{ - Use: "list FLAGS", + Use: "list [options]", Short: "List all resources in the project", Long: `List all resources in the project. This does not include static/public resources like locations, public ISOs, etc. diff --git a/internal/cmd/base/delete.go b/internal/cmd/base/delete.go index a1ef8342..1f215805 100644 --- a/internal/cmd/base/delete.go +++ b/internal/cmd/base/delete.go @@ -26,8 +26,13 @@ type DeleteCmd struct { // CobraCommand creates a command that can be registered with cobra. func (dc *DeleteCmd) CobraCommand(s state.State) *cobra.Command { + opts := "" + if dc.AdditionalFlags != nil { + opts = "[options] " + } + cmd := &cobra.Command{ - Use: fmt.Sprintf("delete [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), + Use: fmt.Sprintf("delete %s<%s>", opts, strings.ToLower(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), diff --git a/internal/cmd/base/describe.go b/internal/cmd/base/describe.go index 2edb556d..0e3b7bcd 100644 --- a/internal/cmd/base/describe.go +++ b/internal/cmd/base/describe.go @@ -33,7 +33,7 @@ type DescribeCmd struct { // CobraCommand creates a command that can be registered with cobra. func (dc *DescribeCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ - Use: fmt.Sprintf("describe [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), + Use: fmt.Sprintf("describe [options] <%s>", strings.ToLower(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), diff --git a/internal/cmd/base/labels.go b/internal/cmd/base/labels.go index 71897c0a..a3aa4dbd 100644 --- a/internal/cmd/base/labels.go +++ b/internal/cmd/base/labels.go @@ -27,7 +27,7 @@ type LabelCmds struct { // AddCobraCommand creates a command that can be registered with cobra. func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ - Use: fmt.Sprintf("add-label [FLAGS] %s LABEL...", strings.ToUpper(lc.ResourceNameSingular)), + Use: fmt.Sprintf("add-label [--overwrite] <%s>