mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 05:28:03 +00:00
Compare commits
5 Commits
24940ed99f
...
bd33b0f29c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd33b0f29c | ||
|
|
dce54bd689 | ||
|
|
f1cc33fe40 | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -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).
|
||||
|
||||
_1526 TILs and counting..._
|
||||
_1527 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -191,7 +191,7 @@ _1526 TILs and counting..._
|
||||
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
||||
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
||||
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
||||
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
||||
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
||||
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
||||
@@ -403,6 +403,7 @@ _1526 TILs and counting..._
|
||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||
- [Not So Random](go/not-so-random.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)
|
||||
- [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)
|
||||
@@ -734,7 +735,7 @@ _1526 TILs and counting..._
|
||||
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
||||
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
||||
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
||||
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Checking Inequality](postgres/checking-inequality.md)
|
||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.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)
|
||||
@@ -17,7 +17,7 @@ You can also manually create the TXT record if necessary.
|
||||
Either way, it will look something like:
|
||||
|
||||
```bash
|
||||
!dig -t TXT visualmode.dev
|
||||
$ dig -t TXT visualmode.dev
|
||||
|
||||
;; ANSWER SECTION:
|
||||
visualmode.dev. 377 IN TXT "google-site-verification=MBZ2S2fhnh2gHRxFniRrYW-O6mdyimJDRFj-f
|
||||
|
||||
Reference in New Issue
Block a user