Documentation ¶
Overview ¶
Example (GithubCliCommandBrowse) ¶
args := struct { Branch string `cli:"-b, --branch, Select another branch by passing in the branch name"` Commit bool `cli:"-c, --commit, Open the last commit"` NoBrowser bool `cli:"-n, --no-browser, Print destination URL instead of opening the browser"` Projects bool `cli:"-p, --projects, Open repository projects"` Repo string `cli:"-R, --repo, Select another repository using the '[HOST/]OWNER/REPO' format"` Settings bool `cli:"-s, --settings, Open repository settings"` Wiki bool `cli:"-w, --wiki, Open repository wiki"` Location string `cli:"location, A browser location can be specified using arguments in the following format:\n- by number for issue or pull request, e.g. \"123\"; or\n- by path for opening folders and files, e.g. \"cmd/gh/main.go\""` }{} _, err := Parse(&args, WithErrorHandling(flag.ContinueOnError)) if err != nil && err != flag.ErrHelp { panic(err) }
Output:
Example (GithubCliCommandIssueClose) ¶
var args struct { CommonIssueArgs } _, err := Parse(&args, WithErrorHandling(flag.ContinueOnError)) if err != nil && err != flag.ErrHelp { panic(err) }
Output:
Example (GithubCliCommandIssueComment) ¶
var args struct { Body string `cli:"-b, --body Supply a body. Will prompt for one otherwise."` BodyFile string `cli:"-F, --body-file Read body text from 'file' (use \"-\" to read from standard input)"` Editor bool `cli:"-e, --editor, Add body using editor"` Web bool `cli:"-w, --web, Add body in browser"` CommonIssueArgs } _, err := Parse(&args, WithErrorHandling(flag.ContinueOnError)) if err != nil && err != flag.ErrHelp { panic(err) }
Output:
Example (GithubCliCommandIssueCreate) ¶
var args struct { Assignee string `cli:"-a, --assignee Assign people by their 'login'. Use \"@me\" to self-assign."` Body string `cli:"-b, --body Supply a body. Will prompt for one otherwise."` BodyFile string `cli:"-F, --body-file Read body text from 'file' (use \"-\" to read from standard input)"` Label string `cli:"-l, --label Add labels by 'name'"` Milestone string `cli:"-m, --milestone Add the issue to a milestone by 'name'"` Project string `cli:"-p, --project Add the issue to projects by 'name'"` Recover string `cli:" --recover Recover input from a failed run of create"` Title string `cli:"-t, --title Supply a title. Will prompt for one otherwise."` Web bool `cli:"-w, --web Open the browser to create an issue"` CommonIssueArgs } _, err := Parse(&args, WithErrorHandling(flag.ContinueOnError)) if err != nil && err != flag.ErrHelp { panic(err) }
Output:
Example (GithubCliCommandIssueEdit) ¶
// The description don't need to be aligned. var args struct { AddAssignee bool `cli:"--add-assignee Add assigned users by their 'login'. Use \"@me\" to assign yourself."` AddLabel string `cli:"--add-label Add labels by 'name'"` AddProject string `cli:"--add-project Add the issue to projects by 'name'"` Body string `cli:"-b, --body Set the new body."` BodyFile string `cli:"-F, --body-file Read body text from 'file' (use \"-\" to read from standard input)"` Milestone string `cli:"-m, --milestone Edit the milestone the issue belongs to by 'name'"` RemoveAssignee string `cli:"--remove-assignee Remove assigned users by their 'login'. Use \"@me\" to unassign yourself."` RemoveLabel string `cli:"--remove-label Remove labels by 'name'"` RemoveProject string `cli:"--remove-project Remove the issue from projects by 'name'"` Title string `cli:"-t, --title Set the new title."` CommonIssueArgs } _, err := Parse(&args, WithErrorHandling(flag.ContinueOnError)) if err != nil && err != flag.ErrHelp { panic(err) }
Output:
Index ¶
- func Add(name string, f func(), description string)
- func AddGroup(name string, description string)
- func AddHelp()
- func AddHidden(name string, f func(), description string)
- func Parse(v interface{}, opts ...ParseOpt) (fs *flag.FlagSet, err error)
- func PrintHelp()
- func Run()
- type Command
- type Modifier
- type ParseOpt
Examples ¶
- Package (GithubCliCommandBrowse)
- Package (GithubCliCommandIssueClose)
- Package (GithubCliCommandIssueComment)
- Package (GithubCliCommandIssueCreate)
- Package (GithubCliCommandIssueEdit)
- Add (AddCommands)
- Run (GroupCommand)
- Run (HelpCommand)
- Run (HelpIssueCreate)
- Run (MainCommand)
- Run (SubCommandBrowse)
- Run (SubCommandIssueCreate)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add adds a command to global state.
Example (AddCommands) ¶
Add("browse", Example_githubCliCommandBrowse, "Open the repository in the browser") Add("actions", dummyCmd, "Learn about working with GitHub Actions") // Enable the "help" command. AddHelp() AddGroup("issue", "Manage issues") Add("issue close", Example_githubCliCommandIssueClose, "Close issue") Add("issue comment", Example_githubCliCommandIssueComment, "Create a new issue comment") Add("issue create", Example_githubCliCommandIssueCreate, "Create a new issue") Add("issue delete", dummyCmd, "Delete issue") Add("issue edit", Example_githubCliCommandIssueEdit, "Edit an issue") Add("issue list", dummyCmd, "List and filter issues in this repository") Add("issue repopen", dummyCmd, "Reopen issue") Add("issue status", dummyCmd, "Show status of relevant issues") Add("issue transfer", dummyCmd, "Transfer issue to another repository") Add("issue view", dummyCmd, "View an issue") AddGroup("codespace", "Connect to and manage your codespaces") Add("codespace code", dummyCmd, "Open a codespace in Visual Studio Code") Add("codespace cp", dummyCmd, "Copy files between local and remote file systems") Add("codespace create", dummyCmd, "Create a codespace") Add("codespace delete", dummyCmd, "Delete a codespace") Add("codespace list", dummyCmd, "List your codespaces") Add("codespace logs", dummyCmd, "Access codespace logs") Add("codespace ports", dummyCmd, "List ports in a codespace") Add("codespace ssh", dummyCmd, "SSH into a codespace") Add("codespace stop", dummyCmd, "Stop a running codespace") AddGroup("gist", "Manage gists") Add("gist clone", dummyCmd, "Clone a gist locally") Add("gist create", dummyCmd, "Create a new gist") Add("gist delete", dummyCmd, "Delete a gist") Add("gist edit", dummyCmd, "Edit one of your gists") Add("gist list", dummyCmd, "List your gists") Add("gist view", dummyCmd, "View a gist") AddGroup("pr", "Manage pull requests") Add("pr checkout", dummyCmd, "Check out a pull request in git") Add("pr checks", dummyCmd, "Show CI status for a single pull request") Add("pr close", dummyCmd, "Close a pull request") Add("pr comment", dummyCmd, "Create a new pr comment") Add("pr create", dummyCmd, "Create a pull request") Add("pr diff", dummyCmd, "View changes in a pull request") Add("pr edit", dummyCmd, "Edit a pull request") Add("pr list", dummyCmd, "List and filter pull requests in this repository") Add("pr merge", dummyCmd, "Merge a pull request") Add("pr ready", dummyCmd, "Mark a pull request as ready for review") Add("pr reopen", dummyCmd, "Reopen a pull request") Add("pr review", dummyCmd, "Add a review to a pull request") Add("pr status", dummyCmd, "Show status of relevant pull requests") Add("pr view", dummyCmd, "View a pull request") AddGroup("release", "Manage GitHub releases") Add("release create", dummyCmd, "Create a new release") Add("release delete", dummyCmd, "Delete a release") Add("release download", dummyCmd, "Download release assets") Add("release list", dummyCmd, "List releases in a repository") Add("release upload", dummyCmd, "Upload assets to a release") Add("release view", dummyCmd, "View information about a release") AddGroup("repo", "Create, clone, fork, and view repositories") Add("repo archive", dummyCmd, "Archive a repository") Add("repo clone", dummyCmd, "Clone a repository locally") Add("repo create", dummyCmd, "Create a new repository") Add("repo delete", dummyCmd, "Delete a repository") Add("repo edit", dummyCmd, "Edit repository settings") Add("repo fork", dummyCmd, "Create a fork of a repository") Add("repo list", dummyCmd, "List repositories owned by user or organization") Add("repo rename", dummyCmd, "Rename a repository") Add("repo sync", dummyCmd, "Sync a repository") Add("repo view", dummyCmd, "View a repository") AddGroup("run", "View details about workflow runs") Add("run cancel", dummyCmd, "Cancel a workflow run") Add("run download", dummyCmd, "Download artifacts generated by a workflow run") Add("run list", dummyCmd, "List recent workflow runs") Add("run rerun", dummyCmd, "Rerun a failed run") Add("run view", dummyCmd, "View a summary of a workflow run") Add("run watch", dummyCmd, "Watch a run until it completes, showing its progress") AddGroup("workflow", "View details about GitHub Actions workflows") Add("workflow disable", dummyCmd, "Disable a workflow") Add("workflow enable", dummyCmd, "Enable a workflow") Add("workflow list", dummyCmd, "List workflows") Add("workflow run", dummyCmd, "Run a workflow by creating a workflow_dispatch event") Add("workflow view", dummyCmd, "View the summary of a workflow") AddGroup("alias", "Create command shortcuts") Add("alias delete", dummyCmd, "Delete an alias") Add("alias list", dummyCmd, "List your aliases") Add("alias set", dummyCmd, "Create a shortcut for a gh command") Add("api", dummyCmd, "Make an authenticated GitHub API request") AddGroup("auth", "Login, logout, and refresh your authentication") Add("auth login", dummyCmd, "Authenticate with a GitHub host") Add("auth logout", dummyCmd, "Log out of a GitHub host") Add("auth refresh", dummyCmd, "Refresh stored authentication credentials") Add("auth setup-git", dummyCmd, "Configure git to use GitHub CLI as a credential helper") Add("auth status", dummyCmd, "View authentication status") Add("completion", dummyCmd, "Generate shell completion scripts") AddGroup("config", "Manage configuration for gh") Add("config get", dummyCmd, "Print the value of a given configuration key") Add("config list", dummyCmd, "Print a list of configuration keys and values") Add("config set", dummyCmd, "Update configuration with a value for the given key") AddGroup("extension", "Manage gh extensions") Add("extension create", dummyCmd, "Create a new extension") Add("extension install", dummyCmd, "Install a gh extension from a repository") Add("extension list", dummyCmd, "List installed extension commands") Add("extension remove", dummyCmd, "Remove an installed extension") Add("extension upgrade", dummyCmd, "Upgrade installed extensions") AddGroup("gpg-key", "Manage GPG keys") Add("gpg-key add", dummyCmd, "Add a GPG key to your GitHub account") Add("gpg-key list", dummyCmd, "Lists GPG keys in your GitHub account") AddGroup("secret", "Manage GitHub secrets") Add("secret list", dummyCmd, "List secrets") Add("secret remove", dummyCmd, "Remove secrets") Add("secret set", dummyCmd, "Create or update secrets") AddGroup("ssh-key", "Manage SSH keys") Add("ssh-key add", dummyCmd, "Add an SSH key to your GitHub account") Add("ssh-key list", dummyCmd, "Lists SSH keys in your GitHub account")
Output:
func AddGroup ¶
AddGroup adds a group to global state. A group is a common prefix for some commands.
func AddHidden ¶
AddHidden adds a hidden command to global state.
A hidden command won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
func Parse ¶
Parse parses the command line for flags and arguments. v should be a pointer to a struct, else it panics.
func Run ¶
func Run()
Run runs the program, it will parse the command line and search for a registered command, it runs the command if a command is found, else it will report an error and exit the program.
Example (GroupCommand) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "issue"} Run()
Output: Manage issues USAGE: gh issue <command> ... COMMANDS: issue close Close issue issue comment Create a new issue comment issue create Create a new issue issue delete Delete issue issue edit Edit an issue issue list List and filter issues in this repository issue repopen Reopen issue issue status Show status of relevant issues issue transfer Transfer issue to another repository issue view View an issue
Example (HelpCommand) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "help"} Run()
Output: USAGE: gh <command> ... COMMANDS: actions Learn about working with GitHub Actions alias Create command shortcuts api Make an authenticated GitHub API request auth Login, logout, and refresh your authentication browse Open the repository in the browser codespace Connect to and manage your codespaces completion Generate shell completion scripts config Manage configuration for gh extension Manage gh extensions gist Manage gists gpg-key Manage GPG keys help Help about any command issue Manage issues pr Manage pull requests release Manage GitHub releases repo Create, clone, fork, and view repositories run View details about workflow runs secret Manage GitHub secrets ssh-key Manage SSH keys workflow View details about GitHub Actions workflows
Example (HelpIssueCreate) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "help", "issue", "create"} Run()
Output: Create a new issue USAGE: gh issue create [flags] FLAGS: -a, --assignee login Assign people by their login. Use "@me" to self-assign. -b, --body string Supply a body. Will prompt for one otherwise. -F, --body-file file Read body text from file (use "-" to read from standard input) -l, --label name Add labels by name -m, --milestone name Add the issue to a milestone by name -p, --project name Add the issue to projects by name --recover string Recover input from a failed run of create -R, --repo [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format -t, --title string Supply a title. Will prompt for one otherwise. -w, --web Open the browser to create an issue
Example (MainCommand) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "-h"} Run()
Output: USAGE: gh <command> ... COMMANDS: actions Learn about working with GitHub Actions alias Create command shortcuts api Make an authenticated GitHub API request auth Login, logout, and refresh your authentication browse Open the repository in the browser codespace Connect to and manage your codespaces completion Generate shell completion scripts config Manage configuration for gh extension Manage gh extensions gist Manage gists gpg-key Manage GPG keys help Help about any command issue Manage issues pr Manage pull requests release Manage GitHub releases repo Create, clone, fork, and view repositories run View details about workflow runs secret Manage GitHub secrets ssh-key Manage SSH keys workflow View details about GitHub Actions workflows
Example (SubCommandBrowse) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "browse", "-h"} Run()
Output: Open the repository in the browser USAGE: gh browse [flags] [location] FLAGS: -b, --branch string Select another branch by passing in the branch name -c, --commit Open the last commit -n, --no-browser Print destination URL instead of opening the browser -p, --projects Open repository projects -R, --repo [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format -s, --settings Open repository settings -w, --wiki Open repository wiki ARGUMENTS: location string A browser location can be specified using arguments in the following format: - by number for issue or pull request, e.g. "123"; or - by path for opening folders and files, e.g. "cmd/gh/main.go"
Example (SubCommandIssueCreate) ¶
resetState() defer setExampleTestMark()() ExampleAdd_addCommands() os.Args = []string{"gh", "issue", "create", "-h"} Run()
Output: Create a new issue USAGE: gh issue create [flags] FLAGS: -a, --assignee login Assign people by their login. Use "@me" to self-assign. -b, --body string Supply a body. Will prompt for one otherwise. -F, --body-file file Read body text from file (use "-" to read from standard input) -l, --label name Add labels by name -m, --milestone name Add the issue to a milestone by name -p, --project name Add the issue to projects by name --recover string Recover input from a failed run of create -R, --repo [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format -t, --title string Supply a title. Will prompt for one otherwise. -w, --web Open the browser to create an issue
Types ¶
type Command ¶
type Command struct { Name string Description string Hidden bool // contains filtered or unexported fields }
Command holds the information of a command.
type Modifier ¶
type Modifier byte
Modifier represents an option to a flag, it sets the flag to be deprecated, hidden, or required. In a `cli` tag, modifiers appears as the first segment, starting with a `#` character.
Fow now the following modifiers are available:
D - marks a flag or argument as deprecated, "DEPRECATED" will be showed in help R - marks a flag or argument as required, "REQUIRED" will be showed in help H - marks a flag as hidden, see below for more about hidden flags
Hidden flags won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
Modifier `H` shall not be used for an argument, else it panics. An argument must be showed in help to tell user how to use the program correctly.
Some modifiers cannot be used together, else it panics, e.g.
H & R - a required flag must be showed in help to tell user to set it D & R - a required flag must not be deprecated, it does not make sense and make user confusing
type ParseOpt ¶
type ParseOpt func(*parseOptions)
ParseOpt specifies options to customize the behavior of Parse.
func WithArgs ¶
WithArgs indicates Parse to parse from the given args, instead of parsing from the program's command line arguments.
func WithErrorHandling ¶
func WithErrorHandling(h flag.ErrorHandling) ParseOpt
WithErrorHandling indicates Parse to use the given ErrorHandling. By default, Parse exits the program when an error happens.