Skip to content

Commit 50b97a6

Browse files
committed
feat: update for 2024
This contains a bunch of small updates and changes. + Add go.mod file + Add Makefile, linters, and GitHub action + Update LICENSE and README + Add TODO + Make some initial changes to the code
1 parent 20be209 commit 50b97a6

File tree

11 files changed

+322
-144
lines changed

11 files changed

+322
-144
lines changed

.github/workflows/go.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: gitmirror test
2+
on:
3+
push:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
os:
13+
- ubuntu-latest
14+
- macos-latest
15+
- windows-latest
16+
go: ['1.22', '1.23']
17+
name: humane test (using go ${{ matrix.go }} on ${{ matrix.os }})
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: actions/setup-go@v5
21+
with:
22+
go-version: ${{ matrix.go }}
23+
- run: make test

.golangci.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
linters-settings:
2+
copyloopvar:
3+
check-alias: true
4+
errcheck:
5+
check-type-assertions: true
6+
check-blank: true
7+
exclude-functions:
8+
- fmt.Printf
9+
- fmt.Println
10+
- fmt.Fprintf
11+
- fmt.Fprintln
12+
- (*flag.FlagSet).Set
13+
exhaustive:
14+
default-signifies-exhaustive: true
15+
goconst:
16+
min-len: 2
17+
min-occurrences: 3
18+
gocritic:
19+
disabled-checks:
20+
- hugeParam
21+
enabled-tags:
22+
- diagnostic
23+
- experimental
24+
- opinionated
25+
- performance
26+
- style
27+
govet:
28+
enable-all: true
29+
shadow:
30+
strict: true
31+
nolintlint:
32+
require-explanation: true
33+
require-specific: true
34+
35+
linters:
36+
disable-all: true
37+
enable:
38+
- bodyclose
39+
- copyloopvar
40+
- cyclop
41+
- dogsled
42+
- dupl
43+
- errcheck
44+
- errchkjson
45+
- errname
46+
- errorlint
47+
- exhaustive
48+
- goconst
49+
- gocritic
50+
- gosec
51+
- gosimple
52+
- govet
53+
- ineffassign
54+
- ireturn
55+
- maintidx
56+
- misspell
57+
- nolintlint
58+
- nakedret
59+
- prealloc
60+
- predeclared
61+
- stylecheck
62+
- thelper
63+
- typecheck
64+
- unconvert
65+
- unparam
66+
- unused
67+
- whitespace
68+
69+
run:
70+
concurrency: 8
71+
issues-exit-code: 1
72+
timeout: 30m
73+
tests: true
74+
allow-parallel-runners: false
75+
76+
issues:
77+
exclude-dirs:
78+
- internal

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Copyright (c) 2009 The Go Authors. All rights reserved.
2+
Copyright (c) 2024 Peter Aronoff. All rights reserved.
23

34
Redistribution and use in source and binary forms, with or without
45
modification, are permitted provided that the following conditions are

Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.DEFAULT_GOAL := test
2+
3+
fmt:
4+
golangci-lint run --disable-all --no-config -Egofmt --fix
5+
golangci-lint run --disable-all --no-config -Egofumpt --fix
6+
7+
lint: fmt
8+
staticcheck ./...
9+
revive -config revive.toml ./...
10+
golangci-lint run
11+
12+
golangci: fmt
13+
golangci-lint run
14+
15+
staticcheck: fmt
16+
staticcheck ./...
17+
18+
revive: fmt
19+
revive -config revive.toml ./...
20+
21+
test:
22+
go test -shuffle on .
23+
24+
testv:
25+
go test -shuffle on -v .
26+
27+
testr:
28+
go test -race -shuffle on .
29+
30+
build: lint testr
31+
go build .
32+
33+
install: build
34+
go install .
35+
36+
clean:
37+
go clean -i -r -cache
38+
39+
.PHONY: fmt lint build install test testv testr clean

README.md

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,66 @@
1-
# rsc.io/getopt
1+
# getopt (forked from rsc.io/getopt)
22

3-
[For full package documentation, see [https://godoc.org/rsc.io/getopt](https://godoc.org/rsc.io/getopt).]
3+
## Note
44

5-
package getopt // import "rsc.io/getopt"
5+
This repository is a fork of [rsc's][rsc] [getopt][rgetopt]. I have
6+
updated the code in various ways, and I will probably continue to do so. But
7+
the core idea (and implementation) is his rather than mine. td;dr — credit where
8+
credit is due, but all mistakes and bad ideas are my fault and not rsc's.
69

7-
Package getopt parses command lines using [_getopt_(3)](http://man7.org/linux/man-pages/man3/getopt.3.html) syntax. It is a
8-
replacement for `flag.Parse` but still expects flags themselves to be defined
9-
in package flag.
10+
[rsc]: https://github.com/rsc
11+
[rgetopt]: https://github.com/rsc/getopt
1012

11-
Flags defined with one-letter names are available as short flags (invoked
12-
using one dash, as in `-x`) and all flags are available as long flags (invoked
13-
using two dashes, as in `--x` or `--xylophone`).
13+
## Introduction
1414

15-
To use, define flags as usual with [package flag](https://godoc.org/flag). Then introduce any aliases
16-
by calling `getopt.Alias`:
15+
Package getopt parses command lines using [`getopt_(3)`][getopt3] syntax. It is
16+
a replacement for [`FlagSet.Parse`][fs.Parse] but still expects flags themselves
17+
to be defined in package `flag`.
1718

18-
getopt.Alias("v", "verbose")
19+
[getopt3]: http://man7.org/linux/man-pages/man3/getopt.3.html
20+
[fs.Parse]: https://pkg.go.dev/flag#FlagSet.Parse
1921

20-
Or call `getopt.Aliases` to define a list of aliases:
22+
Flags defined with one-letter names are available as short flags (invoked using
23+
one dash, as in `-x`) and all flags are available as long flags (invoked using
24+
two dashes, as in `--x` or `--xylophone`).
2125

22-
getopt.Aliases(
23-
"v", "verbose",
24-
"x", "xylophone",
25-
)
26+
To use, define a [`FlagSet`][FlagSet] and flags as usual with [package
27+
flag][flag]. Then introduce any aliases by calling `getopt.Alias`:
2628

27-
One name in each pair must already be defined in package flag (so either
28-
"v" or "verbose", and also either "x" or "xylophone").
29+
```go
30+
type cfg struct {
31+
quiet bool
32+
verbose bool
33+
}
2934

30-
Then parse the command-line:
35+
func main() {
36+
fs := getopt.NewFlagSet("awesome", flag.ContinueOnError)
37+
cfg := &cfg{}
3138

32-
getopt.Parse()
39+
fs.BoolVar(&cfg.quiet, "quiet", false, "Print only errors")
40+
fs.BoolVar(&cfg.help, "v", false, "Print way too much")
3341

34-
If it encounters an error, `Parse` calls `flag.Usage` and then exits the
35-
program.
42+
getopt.Alias("v", "verbose")
43+
getopt.Alias("q", "quiet")
44+
}
45+
```
3646

37-
When writing a custom `flag.Usage` function, call `getopt.PrintDefaults` instead
38-
of `flag.PrintDefaults` to get a usage message that includes the
39-
names of aliases in flag descriptions.
47+
Or call `getopt.Aliases` to define a list of aliases:
4048

41-
At initialization time, package getopt installs a new `flag.Usage` that is the same
42-
as the default `flag.Usage` except that it calls `getopt.PrintDefaults` instead
43-
of `flag.PrintDefaults`.
49+
```go
50+
getopt.Aliases(
51+
"q", "quiet",
52+
"v", "verbose",
53+
)
54+
```
4455

45-
This package also defines a `FlagSet` wrapping the standard `flag.FlagSet`.
56+
[FlagSet]: https://pkg.go.dev/flag#FlagSet
57+
[flag]: https://godoc.org/flag
4658

47-
## Caveat
59+
One name in each pair must already be defined in package `flag` (so either
60+
"q" or "quiet", and also either "v" or "verbose").
4861

49-
In general Go flag parsing is preferred for new programs, because it is not
50-
as pedantic about the number of dashes used to invoke a flag (you can write
51-
`-verbose` or `--verbose` and the program does not care). This package is meant
52-
to be used in situations where, for legacy reasons, it is important to use
53-
exactly _getopt_(3) syntax, such as when rewriting in Go an existing tool that
54-
already uses _getopt_(3).
62+
Then parse the command-line using [`fs.Parse()`][fs.Parse].
63+
64+
When writing a custom `flag.Usage` function, call `getopt.PrintDefaults` instead
65+
of `flag.PrintDefaults` to get a usage message that includes the names of
66+
aliases in flag descriptions.

TODO.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TODO
2+
3+
1. Improve and expand tests.
4+
+ Use example tests or cmp to test usage output.
5+
+ Add tests for defaults and isZero check.
6+
+ Check coverage?
7+
1. Change usage display to suit my preferences. Do not special case short
8+
boolean flags; put usage on the same line as the flag. Use a tabwriter and
9+
wrap text to some good default for terminals?
10+
1. Use a generic isZero check rather than reflect?
11+
1. Add `Subcommand` and `Subcommands` (similar to `Alias` and `Aliases`). These
12+
would support display of subcommands in usage, but I do not want to handle
13+
the overall parsing or running of subcommands (for now?). Users will
14+
manually parse and run their subcommands.
15+
1. Read both `flag` and `ff`. Decide how I want to implement both long and
16+
short. Do I want to use something like `Alias` and the stdlib, or should
17+
I do it from scratch. Also, think about how I want to implement help
18+
displays and how much of the stdlib is worth carrying over. E.g., `Visit`
19+
and `VisitAll`.
20+
1. Demand that all flags implement an interface with two methods: `IsZero` and
21+
`Default`? (Maybe call this a `Defaulter` interface?) This could get rid of
22+
brittle, incomplete, `panic`-inducing, or `reflect`-requiring tests for
23+
whether something is a zero value.

0 commit comments

Comments
 (0)