Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit e5b12cf

Browse files
committed
Initial import
0 parents  commit e5b12cf

16 files changed

+2080
-0
lines changed

LICENSE

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

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2020
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE. }}}
20+
21+
GO := $(shell which go)
22+
23+
all: check
24+
25+
check: lint
26+
27+
clean:
28+
29+
lint:
30+
golint ./...
31+
32+
.PHONY: all lint check
33+
34+
# vim: foldmethod=marker

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# hz.tools/cli
2+
3+
> :warning: Please read [Expectations within this Organization](https://github.com/hztools/.github/tree/main/profile#expectations-within-this-organization) before using it.
4+
5+
[![Go Reference](https://pkg.go.dev/badge/hz.tools/cli.svg)](https://pkg.go.dev/hz.tools/cli)
6+
[![Go Report Card](https://goreportcard.com/badge/hz.tools/cli)](https://goreportcard.com/report/hz.tools/cli)
7+
8+
9+
This package contains a number of helpers for RF-aware CLI applications. This
10+
package lets you do things like Open an SDR from CLI flags, without having to
11+
worry about registering all the flags. This also contains the version helper,
12+
which will dump out diagnostic information that's handy when tracking down bugs.

flag.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2020
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE. }}}
20+
21+
package cli
22+
23+
import (
24+
"fmt"
25+
"os"
26+
"strings"
27+
28+
"github.com/spf13/pflag"
29+
)
30+
31+
// Turn a flag into an environment variable.
32+
func createEnvName(prefix string, flag *pflag.Flag) string {
33+
return fmt.Sprintf("%s%s", prefix, strings.Replace(strings.ToUpper(flag.Name), "-", "_", -1))
34+
}
35+
36+
// EnvRegister will set the default values for all flags in the FlagSet to values
37+
// taken from the environment.
38+
func EnvRegister(prefix string, flagSet *pflag.FlagSet) {
39+
EnvRegisterWithOverrides(prefix, flagSet, map[string]string{})
40+
}
41+
42+
// EnvRegisterWithOverrides will set the default values for all flags in the
43+
// FlagSet to values taken from the environment.
44+
func EnvRegisterWithOverrides(prefix string, flagSet *pflag.FlagSet, overrides map[string]string) {
45+
flagSet.VisitAll(func(flag *pflag.Flag) {
46+
envName, ok := overrides[flag.Name]
47+
if !ok {
48+
envName = createEnvName(prefix, flag)
49+
}
50+
flag.Usage = fmt.Sprintf("%s (${%s})", flag.Usage, envName)
51+
value := os.Getenv(envName)
52+
if value == "" {
53+
return
54+
}
55+
flag.Value.Set(value)
56+
})
57+
}
58+
59+
// vim: foldmethod=marker

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module hz.tools/cli
2+
3+
go 1.15
4+
5+
require (
6+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
7+
github.com/spf13/cobra v1.5.0
8+
github.com/spf13/pflag v1.0.5
9+
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
10+
golang.org/x/sys v0.1.0
11+
hz.tools/fftw v0.0.8
12+
hz.tools/nice v0.0.0-20221219030408-bb857d731555
13+
hz.tools/rf v0.0.7
14+
hz.tools/rfzeug v0.0.0-20220822174644-54f50a47c59f
15+
hz.tools/sdr v0.0.0-20221219061934-cd0f8494c253
16+
)

0 commit comments

Comments
 (0)