Skip to content

Commit fb35b42

Browse files
committed
Initial commit
0 parents  commit fb35b42

24 files changed

+8122
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.DS_Store
2+
Thumbs.db
3+
.vscode
4+
5+
*.log
6+
*.exe
7+
*.prof
8+
*.out
9+
10+
tmp/
11+
logs/
12+
reports/
13+
releases/
14+
vendor/
15+
node_modules/
16+
17+
examples/basic/basic

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
sudo: false
2+
3+
language: go
4+
go:
5+
- 1.9
6+
7+
install:
8+
- go get github.com/golang/lint/golint
9+
- go get github.com/modocache/gover
10+
- go get github.com/mattn/goveralls
11+
12+
script:
13+
- go build .
14+
- ./test.sh
15+
- mkdir -p reports
16+
- go test -v -coverprofile=reports/coverage-flagset.coverprofile -covermode=count github.com/devfacet/gocmd/flagset
17+
- go test -v -coverprofile=reports/coverage-table.coverprofile -covermode=count github.com/devfacet/gocmd/table
18+
- go test -v -coverprofile=reports/coverage-template.coverprofile -covermode=count github.com/devfacet/gocmd/template
19+
- go test -v -coverprofile=reports/coverage-main.coverprofile -covermode=count github.com/devfacet/gocmd
20+
- gover reports/ reports/coverage-all.coverprofile
21+
- go tool cover -func=reports/coverage-all.coverprofile
22+
- goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN -covermode=count -coverprofile=reports/coverage-all.coverprofile

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## v1.0.0 (2018-01-07)
4+
5+
- Initial release

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Fatih Cetinkaya (http://github.com/devfacet/gocmd)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# gocmd
2+
3+
[![Release][release-image]][release-url] [![Build Status][build-image]][build-url] [![Coverage][coverage-image]][coverage-url] [![GoDoc][doc-image]][doc-url]
4+
5+
A Go library for building command line applications.
6+
7+
## Features
8+
9+
- Advanced command line arguments handling
10+
- Short and long command line arguments
11+
- Subcommand handling
12+
- Well formatted usage printing
13+
- Output tables in the terminal
14+
- Template support for config files
15+
- No external dependency
16+
17+
## Installation
18+
19+
```bash
20+
go get github.com/devfacet/gocmd
21+
```
22+
23+
## Usage
24+
25+
### A basic app
26+
27+
See [basic](examples/basic/main.go) for full code.
28+
29+
```go
30+
func main() {
31+
var flags = struct {
32+
Help bool `short:"h" long:"help" default:"false" description:"Display usage"`
33+
Version bool `short:"v" long:"version" default:"false" description:"Display version"`
34+
VersionEx bool `long:"vv" default:"false" description:"Display version (extended)"`
35+
Echo struct {
36+
} `command:"echo" description:"Print arguments"`
37+
}{}
38+
39+
app, err := gocmd.New(gocmd.Options{
40+
Name: "basic",
41+
Version: "1.0.0",
42+
Description: "A basic app",
43+
Flags: &flags,
44+
})
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
49+
// Version
50+
if flags.Version || flags.VersionEx {
51+
app.PrintVersion(flags.VersionEx)
52+
return
53+
}
54+
55+
// Echo
56+
if args, ok := app.LookupFlag("Echo"); ok && args != nil {
57+
fmt.Printf("%s\n", strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", args[1:]), "["), "]"))
58+
return
59+
}
60+
61+
// Help
62+
app.PrintUsage()
63+
return
64+
}
65+
```
66+
```bash
67+
cd examples/basic/
68+
go build .
69+
```
70+
```
71+
$ ./basic
72+
Usage: basic [options...] COMMAND [options...]
73+
74+
A basic app
75+
76+
Options:
77+
-h, --help Display usage
78+
-v, --version Display version
79+
--vv Display version (extended)
80+
81+
Commands:
82+
echo Print arguments
83+
84+
```
85+
86+
## Build
87+
88+
```bash
89+
go build .
90+
```
91+
92+
## Test
93+
94+
```bash
95+
./test.sh
96+
```
97+
98+
## Release
99+
100+
```bash
101+
git add CHANGELOG.md # update CHANGELOG.md
102+
./release.sh v1.0.0 # replace "v1.0.0" with new version
103+
104+
git ls-remote --tags # check the new tag
105+
```
106+
107+
## Contributing
108+
109+
- Code contributions must be through pull requests
110+
- Run tests, linting and formatting before a pull request
111+
- Pull requests can not be merged without being reviewed
112+
- Use "Issues" for bug reports, feature requests and discussions
113+
- Do not refactor existing code without a discussion
114+
- Do not add a new third party dependency without a discussion
115+
- Use semantic versioning and git tags for versioning
116+
117+
## License
118+
119+
Licensed under The MIT License (MIT)
120+
For the full copyright and license information, please view the LICENSE.txt file.
121+
122+
123+
[release-url]: https://github.com/devfacet/gocmd/releases/latest
124+
[release-image]: https://img.shields.io/github/release/devfacet/gocmd.svg
125+
126+
[build-url]: https://travis-ci.org/devfacet/gocmd
127+
[build-image]: https://travis-ci.org/devfacet/gocmd.svg?branch=master
128+
129+
[coverage-url]: https://coveralls.io/github/devfacet/gocmd?branch=master
130+
[coverage-image]: https://coveralls.io/repos/devfacet/gocmd/badge.svg?branch=master&service=github
131+
132+
[doc-url]: https://godoc.org/github.com/devfacet/gocmd
133+
[doc-image]: https://godoc.org/github.com/devfacet/gocmd?status.svg

examples/basic/main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// A basic app
2+
package main
3+
4+
import (
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
"github.com/devfacet/gocmd"
10+
)
11+
12+
func main() {
13+
var flags = struct {
14+
Help bool `short:"h" long:"help" default:"false" description:"Display usage"`
15+
Version bool `short:"v" long:"version" default:"false" description:"Display version"`
16+
VersionEx bool `long:"vv" default:"false" description:"Display version (extended)"`
17+
Echo struct {
18+
} `command:"echo" description:"Print arguments"`
19+
}{}
20+
21+
app, err := gocmd.New(gocmd.Options{
22+
Name: "basic",
23+
Version: "1.0.0",
24+
Description: "A basic app",
25+
Flags: &flags,
26+
})
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
// Version
32+
if flags.Version || flags.VersionEx {
33+
app.PrintVersion(flags.VersionEx)
34+
return
35+
}
36+
37+
// Echo
38+
if args, ok := app.LookupFlag("Echo"); ok && args != nil {
39+
fmt.Printf("%s\n", strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", args[1:]), "["), "]"))
40+
return
41+
}
42+
43+
// Help
44+
app.PrintUsage()
45+
return
46+
}

flagset/arg.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* gocmd
3+
* For the full copyright and license information, please view the LICENSE.txt file.
4+
*/
5+
6+
package flagset
7+
8+
// Arg represents an argument
9+
type Arg struct {
10+
id int
11+
arg string
12+
name string
13+
value string
14+
dash string
15+
hasEq bool
16+
unnamed bool
17+
unset bool
18+
kind string
19+
flagID int
20+
commandID int
21+
parentID int
22+
indexFrom int
23+
indexTo int
24+
updatedBy string
25+
err error
26+
}

flagset/command.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* gocmd
3+
* For the full copyright and license information, please view the LICENSE.txt file.
4+
*/
5+
6+
package flagset
7+
8+
// Command represents a command
9+
type Command struct {
10+
id int
11+
command string
12+
flagID int
13+
parentID int
14+
argID int
15+
indexFrom int
16+
indexTo int
17+
err error
18+
}

0 commit comments

Comments
 (0)