Skip to content

Commit

Permalink
Add example app (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikogs authored Jan 1, 2025
1 parent b49c328 commit 400e774
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/cmd/example1/example1
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ The `go-phings/broccli` package simplifies command line interface management. It
* [Features + Roadmap](#features)

## Sample code
The following code snippet from another tiny project shows how the module can be used.
Example dummy application can be found in `cmd/example1` directory.

However, the following code snippet from another tiny project shows how the module can be used.

```go
// create new CLI object
Expand Down
5 changes: 5 additions & 0 deletions cmd/example1/hellos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hi:Hello
Cześć:Dzień dobry
Halo:Guten Tag
Hei:Tere
Moi:Hyvää päivää
61 changes: 61 additions & 0 deletions cmd/example1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bufio"
"fmt"
"os"
"strings"

rand "math/rand/v2"

"github.com/go-phings/broccli"
)

func main() {
cli := broccli.NewCLI("example1", "Example app", "[email protected]")

printCmd := cli.AddCmd("print", "Prints a hello message", printHandler)

printCmd.AddArg("first-name", "FIRST_NAME", "First name of the person to welcome", broccli.TypeString, broccli.IsRequired)
printCmd.AddArg("last-name", "LAST_NAME", "Optional last name", broccli.TypeString, 0)

printCmd.AddFlag("language-file", "l", "PATH_TO_FILE", "File containing 'hello' in many languages", broccli.TypePathFile, broccli.IsRegularFile|broccli.IsExistent|broccli.IsRequired)
printCmd.AddFlag("alternative", "a", "", "Use alternative welcoming", broccli.TypeBool, 0)

os.Exit(cli.Run())
}

func printHandler(c *broccli.CLI) int {
langFile := c.Flag("language-file")
file, err := os.Open(langFile)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening file %s: %s", langFile, err.Error())
return 1
}

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if line != "" {
lines = append(lines, line)
}
}

i := rand.IntN(len(lines)-1)
messageArr := strings.Split(lines[i], ":")
message := messageArr[0]
if c.Flag("alternative") == "true" {
message = messageArr[1]
}

firstName := c.Arg("first-name")
lastName := ""
if c.Arg("last-name") != "" {
lastName = fmt.Sprintf(" %s", c.Arg("last-name"))
}

fmt.Fprintf(os.Stdout, "%s, %s%s!", message, firstName, lastName)

return 0
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package broccli

const VERSION = "2.1.2"
const VERSION = "2.1.3"

0 comments on commit 400e774

Please sign in to comment.