Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: urfave/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.11.4
Choose a base ref
...
head repository: urfave/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.11.5
Choose a head ref
  • 5 commits
  • 33 files changed
  • 1 contributor

Commits on Aug 14, 2022

  1. Verified

    This commit was signed with the committer’s verified signature.
    kzaher Krunoslav Zaher
    Copy the full SHA
    5361b38 View commit details
  2. breakup v2 documentation

    hay-kot committed Aug 14, 2022
    Copy the full SHA
    cbe642e View commit details
  3. update sidebar configuration

    hay-kot committed Aug 14, 2022
    Copy the full SHA
    dec6654 View commit details

Commits on Aug 15, 2022

  1. add tags plugin

    hay-kot committed Aug 15, 2022
    Copy the full SHA
    286d5ad View commit details
  2. add tags and boost for pages

    hay-kot committed Aug 15, 2022
    Copy the full SHA
    dac1c40 View commit details
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ an expressive way.
These are the guides for each major supported version:

- [`v2`](./v2/)
- [`v1`](./v1/)
- [`v1`](./v1/getting-started)

In addition to the version-specific guides, these other documents are available:

35 changes: 35 additions & 0 deletions docs/v1/examples/arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
tags:
- v1
---

You can lookup arguments by calling the `Args` function on `cli.Context`, e.g.:

<!-- {
"output": "Hello \""
} -->
``` go
package main

import (
"fmt"
"log"
"os"

"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()

app.Action = func(c *cli.Context) error {
fmt.Printf("Hello %q", c.Args().Get(0))
return nil
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```
119 changes: 119 additions & 0 deletions docs/v1/examples/bash-completions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
tags:
- v1
---

You can enable completion commands by setting the `EnableBashCompletion`
flag on the `App` object. By default, this setting will only auto-complete to
show an app's subcommands, but you can write your own completion methods for
the App or its subcommands.

<!-- {
"args": ["complete", "&#45;&#45;generate&#45;bash&#45;completion"],
"output": "laundry"
} -->
``` go
package main

import (
"fmt"
"log"
"os"

"github.com/urfave/cli"
)

func main() {
tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}

app := cli.NewApp()
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "complete",
Aliases: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) error {
fmt.Println("completed task: ", c.Args().First())
return nil
},
BashComplete: func(c *cli.Context) {
// This will complete if no args are passed
if c.NArg() > 0 {
return
}
for _, t := range tasks {
fmt.Println(t)
}
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```

#### Enabling

Source the `autocomplete/bash_autocomplete` file in your `.bashrc` file while
setting the `PROG` variable to the name of your program:

`PROG=myprogram source /.../cli/autocomplete/bash_autocomplete`

#### Distribution

Copy `autocomplete/bash_autocomplete` into `/etc/bash_completion.d/` and rename
it to the name of the program you wish to add autocomplete support for (or
automatically install it there if you are distributing a package). Don't forget
to source the file to make it active in the current shell.

```
sudo cp src/bash_autocomplete /etc/bash_completion.d/<myprogram>
source /etc/bash_completion.d/<myprogram>
```

Alternatively, you can just document that users should source the generic
`autocomplete/bash_autocomplete` in their bash configuration with `$PROG` set
to the name of their program (as above).

#### Customization

The default bash completion flag (`--generate-bash-completion`) is defined as
`cli.BashCompletionFlag`, and may be redefined if desired, e.g.:

<!-- {
"args": ["&#45;&#45;compgen"],
"output": "wat\nhelp\nh"
} -->
``` go
package main

import (
"log"
"os"

"github.com/urfave/cli"
)

func main() {
cli.BashCompletionFlag = cli.BoolFlag{
Name: "compgen",
Hidden: true,
}

app := cli.NewApp()
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "wat",
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```
72 changes: 72 additions & 0 deletions docs/v1/examples/combining-short-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
tags:
- v1
---

Traditional use of options using their shortnames look like this:

```
$ cmd -s -o -m "Some message"
```

Suppose you want users to be able to combine options with their shortnames. This
can be done using the `UseShortOptionHandling` bool in your app configuration,
or for individual commands by attaching it to the command configuration. For
example:

<!-- {
"args": ["short", "&#45;som", "Some message"],
"output": "serve: true\noption: true\nmessage: Some message\n"
} -->
``` go
package main

import (
"fmt"
"log"
"os"

"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.UseShortOptionHandling = true
app.Commands = []cli.Command{
{
Name: "short",
Usage: "complete a task on the list",
Flags: []cli.Flag{
cli.BoolFlag{Name: "serve, s"},
cli.BoolFlag{Name: "option, o"},
cli.StringFlag{Name: "message, m"},
},
Action: func(c *cli.Context) error {
fmt.Println("serve:", c.Bool("serve"))
fmt.Println("option:", c.Bool("option"))
fmt.Println("message:", c.String("message"))
return nil
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```

If your program has any number of bool flags such as `serve` and `option`, and
optionally one non-bool flag `message`, with the short options of `-s`, `-o`,
and `-m` respectively, setting `UseShortOptionHandling` will also support the
following syntax:

```
$ cmd -som "Some message"
```

If you enable `UseShortOptionHandling`, then you must not use any flags that
have a single leading `-` or this will result in failures. For example,
`-option` can no longer be used. Flags with two leading dashes (such as
`--options`) are still valid.
43 changes: 43 additions & 0 deletions docs/v1/examples/exit-codes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
tags:
- v1
---

Calling `App.Run` will not automatically call `os.Exit`, which means that by
default the exit code will "fall through" to being `0`. An explicit exit code
may be set by returning a non-nil error that fulfills `cli.ExitCoder`, *or* a
`cli.MultiError` that includes an error that fulfills `cli.ExitCoder`, e.g.:
<!-- {
"error": "Ginger croutons are not in the soup"
} -->
``` go
package main

import (
"log"
"os"

"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "ginger-crouton",
Usage: "Add ginger croutons to the soup",
},
}
app.Action = func(ctx *cli.Context) error {
if !ctx.Bool("ginger-crouton") {
return cli.NewExitError("Ginger croutons are not in the soup", 86)
}
return nil
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```
Loading