Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
35432e184a | ||
![]() |
b942c67d8e | ||
![]() |
4a628ccc7e | ||
![]() |
e0a8e22fea | ||
![]() |
b895c86fab | ||
![]() |
bebebac341 |
2 changed files with 28 additions and 25 deletions
30
README.md
30
README.md
|
@ -1,15 +1,14 @@
|
||||||
### The "Version" package
|
### The "Help" package
|
||||||
|
|
||||||
This package provides --version flag feature to your cli.
|
This package provides --help flag feature to your cli.
|
||||||
It recognize both short and long flags. Version number is
|
It recognize both short and long flags.
|
||||||
automatically added from the latest tag in your git repository.
|
|
||||||
|
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
1. Download the package by **go get** command:
|
1. Download the package by **go get** command:
|
||||||
```shell
|
```shell
|
||||||
go get code.foilhatguy.casa/pub/version
|
go get code.foilhatguy.casa/pub/help
|
||||||
```
|
```
|
||||||
2. Add the package as a tool dependency to your project in
|
2. Add the package as a tool dependency to your project in
|
||||||
tools.go:
|
tools.go:
|
||||||
|
@ -19,23 +18,28 @@ tools.go:
|
||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "code.foilhatguy.casa/pub/version"
|
_ "code.foilhatguy.casa/pub/help"
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
3. In the main package add a go:generate directive:
|
3. In the main package add a go:generate directive:
|
||||||
```go
|
```go
|
||||||
//go:generate go run code.foilhatguy.casa/pub/version
|
//go:generate go run code.foilhatguy.casa/pub/help HELP.md
|
||||||
```
|
```
|
||||||
|
The last part in this directive is a name of the help file for
|
||||||
|
your application. It should be placed in the root directory
|
||||||
|
of your project along with **main.go** file. The help file
|
||||||
|
can be written ether in the MarkDown format, or in plain text.
|
||||||
|
MarkDown will be automatically converted in to plain text,
|
||||||
|
which is suitable for the console output.
|
||||||
|
|
||||||
4. Run code generator by **go generate** command:
|
4. Run code generator by **go generate** command:
|
||||||
```shell
|
```shell
|
||||||
go generate ./...
|
go generate ./...
|
||||||
```
|
```
|
||||||
The package will detect a version number from the latest git
|
The package will generate a **help.go** file as a part of
|
||||||
tag from your project. Then it will generate a **version.go**
|
the main package. It will contain code for --help flag detection.
|
||||||
file as a part of the main package. It will contain a version
|
|
||||||
number as a constant and code for --version flag detection.
|
|
||||||
|
|
||||||
5. Build and run your application with the --version flag.
|
5. Build and run your application with the --help flag.
|
||||||
```shell
|
```shell
|
||||||
go run . --version
|
go run . --help
|
||||||
```
|
```
|
23
main.go
23
main.go
|
@ -22,25 +22,24 @@ import (
|
||||||
//go:embed help
|
//go:embed help
|
||||||
var goFS embed.FS
|
var goFS embed.FS
|
||||||
|
|
||||||
func flagIsSet(arg string, sfortFlag string) bool {
|
|
||||||
if len(arg)>1 {
|
|
||||||
if arg[0] == "-"[0] && arg[1] != "-"[0] {
|
|
||||||
return strings.Contains(arg, sfortFlag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
help, err := goFS.ReadFile("help")
|
flagIsSet := func(arg string, sfortFlag string) bool {
|
||||||
|
arg = strings.Split(arg, "=")[0]
|
||||||
|
if len(arg)>1 {
|
||||||
|
if arg[0] == "-"[0] && arg[1] != "-"[0] {
|
||||||
|
return strings.Contains(arg, sfortFlag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
help, err := goFS.ReadFile("help")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, arg := range os.Args {
|
for _, arg := range os.Args {
|
||||||
fmt.Println(flagIsSet(arg, "h"))
|
|
||||||
fmt.Println(arg == "--help")
|
|
||||||
if flagIsSet(arg, "h") || arg == "--help" {
|
if flagIsSet(arg, "h") || arg == "--help" {
|
||||||
fmt.Println(string(help))
|
fmt.Println(string(help))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
Loading…
Add table
Reference in a new issue