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 interface{}, description string)
- func AddAlias(aliasName, target string)
- func AddGroup(name string, description string)
- func AddHelp()
- func AddHidden(name string, f interface{}, description string)
- func Parse(v interface{}, opts ...ParseOpt) (fs *flag.FlagSet, err error)
- func PrintHelp()
- func Run(args ...string)
- func SetGlobalFlags(v interface{})
- func SetOptions(opts Options)
- type App
- func (p *App) Add(name string, f interface{}, description string)
- func (p *App) AddAlias(aliasName, target string)
- func (p *App) AddGroup(name string, description string)
- func (p *App) AddHelp()
- func (p *App) AddHidden(name string, f interface{}, description string)
- func (p *App) Run(args ...string)
- func (p *App) SetGlobalFlags(v interface{})
- func (p *App) SetOptions(opts Options)
- type Command
- type Context
- type Modifier
- type Options
- type ParseOpt
Examples ¶
- Package (GithubCliCommandBrowse)
- Package (GithubCliCommandIssueClose)
- Package (GithubCliCommandIssueComment)
- Package (GithubCliCommandIssueCreate)
- Package (GithubCliCommandIssueEdit)
- Add (AddCommands)
- AddAlias
- 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. f must be a function of signature `func()` or `func(*Context)`, else it panics.
Example (AddCommands) ¶
Add("browse", Example_githubCliCommandBrowse, "Open the repository in the browser") Add("actions", dummyCmd, "Learn about working with GitHub Actions") 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 reopen", 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") // Enable the "help" command. AddHelp()
Output:
func AddAlias ¶ added in v0.4.0
func AddAlias(aliasName, target string)
AddAlias adds an alias name for a command.
Example ¶
defer markExampleTest()() var addCommands = func() { Add("cmda", dummyCmd, "A description for cmda") Add("cmdb", dummyCmd, "Do something with this cmdb command") AddAlias("cmd alias", "cmdb") } resetDefaultApp() addCommands() os.Args = []string{"demo", "cmd", "alias"} Run()
Output: Alias of command "cmdb" Do something with this cmdb command USAGE: demo cmdb
func AddGroup ¶
AddGroup adds a group explicitly. A group is a common prefix for some commands. It's not required to add group before adding sub commands, but user can use this function to add a description to a group, which will be showed in help.
func AddHidden ¶
AddHidden adds a hidden command. f must be a function of signature `func()` or `func(*Context)`, else it panics.
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 must be a pointer to a struct, else it panics.
func Run ¶
func Run(args ...string)
Run runs the program, it parses the command line and searches for a registered command, it runs the command if a command is found, else it will report an error and exit the program.
Optionally you may specify args to parse, by default it parses the command line arguments os.Args[1:].
Example (GroupCommand) ¶
resetDefaultApp() defer markExampleTest()() 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 reopen Reopen issue issue status Show status of relevant issues issue transfer Transfer issue to another repository issue view View an issue
Example (HelpCommand) ¶
resetDefaultApp() defer markExampleTest()() 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) ¶
resetDefaultApp() defer markExampleTest()() ExampleAdd_addCommands() os.Args = []string{"gh", "help", "issue", "create"} Run()
Output: Create a new issue USAGE: gh issue create [flags] FLAGS: -a, --assignee string Assign people by their 'login'. Use "@me" to self-assign. -b, --body string Supply a body. Will prompt for one otherwise. -F, --body-file string Read body text from 'file' (use "-" to read from standard input) -l, --label string Add labels by 'name' -m, --milestone string Add the issue to a milestone by 'name' -p, --project string 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) ¶
resetDefaultApp() defer markExampleTest()() 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) ¶
resetDefaultApp() defer markExampleTest()() 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) ¶
resetDefaultApp() defer markExampleTest()() ExampleAdd_addCommands() os.Args = []string{"gh", "issue", "create", "-h"} Run()
Output: Create a new issue USAGE: gh issue create [flags] FLAGS: -a, --assignee string Assign people by their 'login'. Use "@me" to self-assign. -b, --body string Supply a body. Will prompt for one otherwise. -F, --body-file string Read body text from 'file' (use "-" to read from standard input) -l, --label string Add labels by 'name' -m, --milestone string Add the issue to a milestone by 'name' -p, --project string 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
func SetGlobalFlags ¶ added in v0.2.0
func SetGlobalFlags(v interface{})
SetGlobalFlags sets global flags, global flags are available to all commands. DisableGlobalFlags may be used to disable global flags for a specific command when calling Parse.
func SetOptions ¶ added in v0.4.0
func SetOptions(opts Options)
SetOptions sets optional options for App.
Types ¶
type App ¶ added in v0.3.0
type App struct {
// contains filtered or unexported fields
}
App holds the state of a cli application.
func NewApp ¶ added in v0.3.0
func NewApp() *App
NewApp creates a new cli application instance. Typically, there is no need to manually create an application, using the package-level functions with the default application is preferred.
func (*App) Add ¶ added in v0.3.0
Add adds a command. f must be a function of signature `func()` or `func(*Context)`, else it panics.
func (*App) AddGroup ¶ added in v0.3.0
AddGroup adds a group explicitly. A group is a common prefix for some commands. It's not required to add group before adding sub commands, but user can use this function to add a description to a group, which will be showed in help.
func (*App) AddHelp ¶ added in v0.3.0
func (p *App) AddHelp()
AddHelp enables the "help" command to print help about any command.
func (*App) AddHidden ¶ added in v0.3.0
AddHidden adds a hidden command. f must be a function of signature `func()` or `func(*Context)`, else it panics.
A hidden command won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
func (*App) Run ¶ added in v0.3.0
Run is the entry point to an application, it parses the command line and searches for a registered command, it runs the command if a command is found, else it will report an error and exit the program.
Optionally you may specify args to parse, by default it parses the command line arguments os.Args[1:].
func (*App) SetGlobalFlags ¶ added in v0.3.0
func (p *App) SetGlobalFlags(v interface{})
SetGlobalFlags sets global flags, global flags are available to all commands. DisableGlobalFlags may be used to disable global flags for a specific command when calling Parse.
func (*App) SetOptions ¶ added in v0.4.0
SetOptions sets optional options for App.
type Command ¶
type Command struct { Name string Description string Hidden bool AliasOf string // contains filtered or unexported fields }
Command holds the information of a command.
type Context ¶ added in v0.5.0
Context holds context-specific information, which is passed to a Command when executing it. Context embeds context.Context, it can be passed to functions which take context.Context as parameter.
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 appear in help to tell user to set it D & R - a required flag must not be deprecated, it does not make sense, but makes user confused
type Options ¶ added in v0.4.0
type Options struct { // KeepCommandOrder makes Parse to print commands in the order of // adding the commands. // By default, it prints commands in lexicographic order. KeepCommandOrder bool // AllowPosixSTMO enables using the posix-style single token to specify // multiple boolean options. e.g. ‘-abc’ is equivalent to ‘-a -b -c’. AllowPosixSTMO bool }
Options specifies optional options for an App.
type ParseOpt ¶
type ParseOpt struct {
// contains filtered or unexported fields
}
ParseOpt specifies options to customize the behavior of Parse.
func DisableGlobalFlags ¶ added in v0.2.0
func DisableGlobalFlags() ParseOpt
DisableGlobalFlags tells Parse to don't parse and print global flags in help.
func ReplaceUsage ¶ added in v0.3.0
ReplaceUsage specifies a function to generate a usage help to replace the default help.
func WithArgs ¶
WithArgs tells Parse to parse from the given args, instead of parsing from the command line arguments.
func WithErrorHandling ¶
func WithErrorHandling(h flag.ErrorHandling) ParseOpt
WithErrorHandling tells Parse to use the given ErrorHandling. By default, Parse exits the program when an error happens.
func WithFooter ¶ added in v0.3.0
WithFooter specifies a function to generate extra help text to print after the default help.