mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Parse Flags From CLI Arguments as a Go TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1526 TILs and counting..._
|
_1527 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -403,6 +403,7 @@ _1526 TILs and counting..._
|
|||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
- [Not So Random](go/not-so-random.md)
|
- [Not So Random](go/not-so-random.md)
|
||||||
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
||||||
|
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
||||||
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
||||||
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
||||||
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
||||||
|
|||||||
65
go/parse-flags-from-cli-arguments.md
Normal file
65
go/parse-flags-from-cli-arguments.md
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
# Parse Flags From CLI Arguments
|
||||||
|
|
||||||
|
Though we can grab the arguments to a Go program from `os.Args`, it requires
|
||||||
|
some manual parsing. With the built-in `flag` package, we can declare specific
|
||||||
|
flags our program accepts, by type. When we parse them, they will be separated
|
||||||
|
out from the rest of the positional arguments.
|
||||||
|
|
||||||
|
Here is an example of the program that accepts a boolean `debug` flag. This
|
||||||
|
will work with either `-debug` or `--debug`.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var debug bool
|
||||||
|
flag.BoolVar(&debug, "debug", false, "turns on debug mode, extra logging")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
positionalArgs := flag.Args()
|
||||||
|
|
||||||
|
if len(positionalArgs) < 1 {
|
||||||
|
fmt.Println("Please specify which part to run: 1 or 2")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if debug {
|
||||||
|
fmt.Println("We are in debug mode...")
|
||||||
|
fmt.Println("Received the following argument:", positionalArgs[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
We can run the program in debug mode like so:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go run . --debug 123
|
||||||
|
We are in debug mode...
|
||||||
|
Received the following argument: 123
|
||||||
|
```
|
||||||
|
|
||||||
|
We can also take advantage of the `help` flag that we get for free:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go run . --help
|
||||||
|
Usage of /var/folders/62/lx9pcjbs1zbd83zg6twwym2r0000gn/T/go-build3212087168/b001/exe/test:
|
||||||
|
-debug
|
||||||
|
turns on debug mode, extra logging
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: any recognized flags need to come before any of the position arguments.
|
||||||
|
The `debug` flag won't be picked up if we run the program like this:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go run . 123 --debug
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](https://pkg.go.dev/flag)
|
||||||
Reference in New Issue
Block a user