|
1 |
| -# rsc.io/getopt |
| 1 | +# getopt (forked from rsc.io/getopt) |
2 | 2 |
|
3 |
| -[For full package documentation, see [https://godoc.org/rsc.io/getopt](https://godoc.org/rsc.io/getopt).] |
| 3 | +## Note |
4 | 4 |
|
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. |
6 | 9 |
|
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 |
10 | 12 |
|
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 |
14 | 14 |
|
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`. |
17 | 18 |
|
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 |
19 | 21 |
|
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`). |
21 | 25 |
|
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`: |
26 | 28 |
|
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 | +} |
29 | 34 |
|
30 |
| -Then parse the command-line: |
| 35 | +func main() { |
| 36 | + fs := getopt.NewFlagSet("awesome", flag.ContinueOnError) |
| 37 | + cfg := &cfg{} |
31 | 38 |
|
32 |
| - getopt.Parse() |
| 39 | + fs.BoolVar(&cfg.quiet, "quiet", false, "Print only errors") |
| 40 | + fs.BoolVar(&cfg.help, "v", false, "Print way too much") |
33 | 41 |
|
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 | +``` |
36 | 46 |
|
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: |
40 | 48 |
|
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 | +``` |
44 | 55 |
|
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 |
46 | 58 |
|
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"). |
48 | 61 |
|
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. |
0 commit comments