mcli

package module
Version: v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 13 Imported by: 0

README

mcli

GoDoc Go Report Card Issues GitHub release MIT License

mcli is a minimal but powerful cli library for Go. m stands for minimal and magic.

It is extremely easy to use, it makes you like writing cli programs in Go.

Disclaimer: the original idea is borrowed from shafreeck/cortana, which is licensed under the Apache License 2.0.

Features

  1. Extremely easy to use, dead simple buf powerful API to define commands, flags and arguments.
  2. Add arbitrary level sub-command with single line code.
  3. Define your command flags and arguments inside the command processor using a simple struct tag.
  4. Set default value for flags and arguments.
  5. Read environment variables for flags and arguments.
  6. Set default value for flags and arguments.
  7. Work with slice, map out of box (of course the bool, (u)int, (u)int16, (u)int32, (u)int64, float, duration, string types are also supported).
  8. Automatic help for commands, flags and arguments.
  9. Mark commands, flags as hidden, hidden commands and flags won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
  10. Mark flags, arguments as required, it reports error when not given.
  11. Mark flags as deprecated.
  12. Automatic suggestions like git.
  13. Compatible with the standard library's flag.FlagSet.

Usage

Work in main function:

func main() {
    var args struct {
        Name string `cli:"-n, --name, Who do you want to say to" default:"tom"`

        // This argument is required.
        Text string `cli:"#R, text, The 'message' you want to send"`
    }
    mcli.Parse(&args)
    fmt.Printf("Say to %s: %s\n", args.Name, args.Text)
}
$ go run say.go
argument is required but not given: text
USAGE:
  tt [flags] <text>

FLAGS:
  -n, --name string    Who do you want to say to (default "tom")

ARGUMENTS:
  text message (REQUIRED)    The message you want to send

exit status 2

$ go run say.go hello
Say to tom: hello

Use with sub-commands:

func main() {
    mcli.Add("cmd1", runCmd1, "An awesome command cmd1")

    mcli.AddGroup("cmd2", "This is a command group called cmd2")
    mcli.Add("cmd2 sub1", runCmd2Sub1, "Do something with cmd2 sub1")
    mcli.Add("cmd2 sub2", runCmd2Sub2, "Brief description about cmd2 sub2")

    // A sub-command can also be registered without registering the group.
    mcli.Add("group3 sub1 subsub1", runGroup3Sub1Subsub1, "Blah blah Blah")

    // This is a hidden command, it won't be showed in help,
    // except that when flag "--mcli-show-hidden" is given.
    mcli.AddHiden("secret-cmd", secretCmd, "An secret command won't be showed in help")

    mcli.Run()
}

func runCmd1() {
    var 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\""`
    }
	mcli.Parse(&args)

    // Do something
}

type Cmd2CommonArgs struct {
    Repo string `cli:"-R, --repo, Select another repository using the '[HOST/]OWNER/REPO' format"`
}

func runCmd2Sub1() {
    // Note that the flag/argument description can be seperated either
    // by a comma or spaces, and can be mixed.
    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
    }
    mcli.Parse(&args)

    // Do something
}

See example_test for a more sophisticated example which mimics Github's cli command gh.

Tag syntax

Struct tag is a powerful feature in Go, mcli uses struct tag to define flags and argumens.

  • tag cli defines the name and description for flags and arguments
  • tag default optionally provides a default value to a flag or argument
  • tag env tells Parse to lookup environment variables when user doesn't provide a value

The syntax is

/* cli tag, only Name is required.
 * Short name and long name are both optional, but at least one must be given.
 * See below for details about Modifiers.
 * e.g.
 * - `cli:"-c, Open the last commit"`
 * - `cli:"#R, -b, --branch, Select another branch by passing in the branch name"`
 * - `cli:"--an-obvious-flag-dont-need-description"`
 */
CliTag           <-  ( Modifiers ',' Space? )? Name ( ( ',' | Space ) Description )?
Modifiers        <-  '#' [DHR]+
Name             <-  ( ShortName LongName? ) | LongName
Description      <-  ( ![\r\n] . )*

/* default value tag, optional.
 * e.g.
 * - `default:"1.5s"` // duration
 * - `default:"true"` // bool
 */
DefaultValueTag  <-  ( ![\r\n] . )*

/* env tag, optional.
 * Multiple environment names can be specified.
 * e.g.
 * - `env:"SOME_ENV"`
 * - `env:"ANOTHER_ENV_1, ANOTHER_ENV_2"`
 */
EnvTag           <-  ( EnvName ',' Space? )* EnvName

Modifiers

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

Compatibility with package flag

Parse returns a *flag.FlagSet if success, all defined flags are available with the flag set, including both short and long names.

Note that there is a little difference with flag package, while the flag package requires command line flags must present before arguments, and arguments can be accessed using flag.Arg(i), this library doesn't require that, the order that user pass flags and arguments doesn't matter. Arguments should be defined in the struct given to Parse, command line arguments will be set to the struct. As a bonus, you can use slice and map arguments, it just works.

When command line arguments are given before flags, calling FlagSet.Arg(i) won't get the expected arguments.

Performance

Well, definitely command line parsing won't be your hot path, performance is not a main consideration for this library, we always want simpler API and better usage instruction for end-users. (This does not mean the library has poor performance.)

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(name string, f func(), description string)

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

func AddGroup(name string, description string)

AddGroup adds a group to global state. A group is a common prefix for some commands.

func AddHelp

func AddHelp()

AddHelp enables the "help" command to print help about any command.

func AddHidden

func AddHidden(name string, f func(), description string)

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

func Parse(v interface{}, opts ...ParseOpt) (fs *flag.FlagSet, err error)

Parse parses the command line for flags and arguments. v should be a pointer to a struct, else it panics.

func PrintHelp

func PrintHelp()

PrintHelp prints usage doc of the current command to stderr.

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

func WithArgs(args []string) ParseOpt

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.

func WithName

func WithName(name string) ParseOpt

WithName specifies the name to use when printing usage doc.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL