Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
test:
name: Test
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# broccli

[![Go Reference](https://pkg.go.dev/badge/gopkg.pl/phings/broccli/v2.svg)](https://pkg.go.dev/gopkg.pl/phings/broccli/v2) [![Go Report Card](https://goreportcard.com/badge/gopkg.pl/phings/broccli/v2)](https://goreportcard.com/report/gopkg.pl/phings/broccli/v2)
[![Go Reference](https://pkg.go.dev/badge/gopkg.pl/phings/broccli/v3.svg)](https://pkg.go.dev/gopkg.pl/phings/broccli/v3) [![Go Report Card](https://goreportcard.com/badge/gopkg.pl/phings/broccli/v3)](https://goreportcard.com/report/gopkg.pl/phings/broccli/v3)

----

The `phings/broccli` package simplifies command line interface management. It allows you to define commands complete with arguments and flags, and attach handlers to them. The package handles all the parsing automatically.
The `phings/broccli/v3` package simplifies command line interface management. It allows you to define commands complete with arguments and flags, and attach handlers to them. The package handles all the parsing automatically.


:warning: `v3` is not compatible with `v2`. The latest version requires a context in handlers and when calling `Run` method. Also
methods for adding commands, args etc. have been shortened to `Command`, `Arg`, `Flag` and `Env`.

----

Expand All @@ -26,17 +30,17 @@ However, the following code snippet from another tiny project shows how the modu

Import the code with the following URL:
```go
import "gopkg.pl/phings/broccli/v2"
import "gopkg.pl/phings/broccli/v3"
```

```go
// create new CLI object
cli := broccli.NewCLI("snakey-letters", "Classic snake but with letters and words!", "")
cli := broccli.NewBroccli("snakey-letters", "Classic snake but with letters and words!", "")

// add a command and attach a function to it
cmd := cli.AddCmd("start", "Starts the game", startHandler)
cmd := cli.Command("start", "Starts the game", startHandler)
// add a flag to the command
cmd.AddFlag("words", "f", "",
cmd.Flag("words", "f", "",
"Text file with wordlist",
// should be a path to a file
broccli.TypePathFile,
Expand All @@ -45,39 +49,39 @@ cmd.AddFlag("words", "f", "",
)

// add another command to print version number
_ = cli.AddCmd("version", "Shows version", versionHandler)
_ = cli.Command("version", "Shows version", versionHandler)

// run cli
os.Exit(cli.Run())
os.Exit(cli.Run(context.Background()))

// handlers for each command
func startHandler(c *broccli.CLI) int {
func startHandler(ctx context.Context, c *broccli.CLI) int {
fmt.Fprint(os.Stdout, "Starting with file %s...", c.Flag("words"))
return 0
}

func versionHandler(c *broccli.CLI) int {
func versionHandler(ctx context.Context, c *broccli.CLI) int {
fmt.Fprintf(os.Stdout, VERSION+"\n")
return 0
}
```

## Structs explained
### CLI
The main `CLI` object has three arguments such as name, description and author. These guys are displayed when syntax is printed out.
### Broccli
The main `Broccli` object has three arguments such as name, usage and author. These guys are displayed when syntax is printed out.

### Commands
Method `AddCmd` creates a new command which has the following properties.

* a `name`, used to call it
* short `description` - few words to display what the command does on the syntax screen
* short `usage` - few words to display what the command does on the syntax screen
* `handler` function that is executed when the command is called and all its flags and argument are valid

#### Command Options
Optionally, after command flags and arguments are successfully validated, and just before the execution of `handler`, additional code (func) can be executed. This can be passed as a last argument.

```go
cmd := cli.AddCmd("start", "Starts the game", startHandler,
cmd := cli.Command("start", "Starts the game", startHandler,
broccli.OnPostValidation(func(c *broccli.Cmd) {
// do something, even with the command
}),
Expand All @@ -97,16 +101,16 @@ To setup a flag in a command, method `AddFlag` is used. It takes the following a

* `name` and `alias` that are used to call the flag (eg. `--help` and `-h`, without the hyphens in the func args)
* `valuePlaceholder`, a placeholder that is printed out on the syntax screen, eg. in `-f PATH_TO_FILE` it is the `PATH_TO_FILE`
* `description` - few words telling what the command does (syntax screen again)
* `usage` - few words telling what the command does (syntax screen again)
* `types`, an int64 value that defines the value type, currently one of `TypeString`, `TypeBool`, `TypeInt`, `TypeFloat`, `TypeAlphanumeric` or `TypePathFile` (see `flags.go` for more information)
* `flags`, an int64 value containing validation requirement, eg. `IsRequired|IsExistent|IsDirectory` could be used with `TypePathFile` to require the flag to be a non-empty path to an existing directory (again, navigate to `flags.go` for more detailed information)

Optionally, a function can be attached to a boolean flag that is triggered when a flag is true. The motivation behind that was a use case when setting a certain flag to true would make another string flag required. However, it's not recommended to be used.

To add an argument for a command, method `AddArg` shall be used. It has almost the same arguments, apart from the fact that `alias` is not there.
To add an argument for a command, method `Arg` shall be used. It has almost the same arguments, apart from the fact that `alias` is not there.

### Environment variables to check
Command may require environment variables. `AddEnvVar` can be called to setup environment variables that should be verified before running the command. For example, a variable might need to contain a path to an existing regular file.
Command may require environment variables. `Env` can be called to setup environment variables that should be verified before running the command. For example, a variable might need to contain a path to an existing regular file.

### Accessing flag and arg values
See sample code that does that below.
Expand All @@ -129,3 +133,4 @@ func startHandler(c *broccli.CLI) int {
- [X] Check for file existence and its type (can be directory, regular file or other)
- [X] Post validation hook
- [X] Boolean flag on-true hook before validation
- [X] Handlers require context
Loading