├── .github └── workflows │ └── test.yaml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── example ├── custom_help │ └── main.go ├── custom_type │ └── main.go └── main.go ├── flagarize.go ├── flagarize_ext_test.go ├── flagarize_test.go ├── go.mod ├── go.sum ├── internal ├── camelcase │ ├── camelcase.go │ └── camelcase_test.go └── timestamp │ └── timestamp.go ├── pathorcontent.go ├── regexp.go ├── scripts ├── build-check-comments.sh ├── cleanup-white-noise.sh └── copyright │ └── copyright.go ├── testutil ├── testorbench.go ├── testorbench_test.go └── testutil.go ├── timeorduration.go └── timeorduration_test.go /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | unit-tests: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Install Go. 14 | uses: actions/setup-go@v1 15 | with: 16 | go-version: 1.13.6 17 | - name: Check out code into the Go module directory 18 | uses: actions/checkout@v1 19 | - uses: actions/cache@v1 20 | with: 21 | path: ~/go/pkg/mod 22 | key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} 23 | - name: Static Analysis 24 | run: make lint 25 | - name: Unit tests 26 | run: make test 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | vendor/ 16 | .bin -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 | NOTE: As semantic versioning states all 0.y.z releases can contain breaking changes in API (flags, grpc API, any backward compatibility) 9 | 10 | We use *breaking* word for marking changes that are not backward compatible (relates only to v0.y.z releases.) 11 | 12 | ## [v0.9.0](https://github.com/bwplotka/flagarize/releases/tag/v0.9.0) - 2020.03.22 13 | 14 | Initial release 💪💪 💪 15 | 16 | Why 0.9.0? Well, because we plan to release 1.0 once we introduce this library to [Thanos](http://github.com/thanos-io/thanos) as the final test (: 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= $(shell pwd) 2 | FILES_TO_FMT ?= $(shell find . -path ./vendor -prune -o -name '*.go' -print) 3 | 4 | # Ensure everything works even if GOPATH is not set, which is often the case. 5 | # The `go env GOPATH` will work for all cases for Go 1.8+. 6 | GOPATH ?= $(shell go env GOPATH) 7 | 8 | TMP_GOPATH ?= /tmp/flagarize-go-tmp2 9 | GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin 10 | GO111MODULE ?= on 11 | export GO111MODULE 12 | GOPROXY ?= https://proxy.golang.org 13 | export GOPROXY 14 | 15 | # Tools. 16 | EMBEDMD ?= $(GOBIN)/embedmd-$(EMBEDMD_VERSION) 17 | # v2.0.0 18 | EMBEDMD_VERSION ?= 97c13d6e41602fc6e397eb51c45f38069371a969 19 | LICHE ?= $(GOBIN)/liche-$(LICHE_VERSION) 20 | LICHE_VERSION ?= 2a2e6e56f6c615c17b2e116669c4cdb31b5453f3 21 | GOIMPORTS ?= $(GOBIN)/goimports-$(GOIMPORTS_VERSION) 22 | GOIMPORTS_VERSION ?= 9d4d845e86f14303813298ede731a971dd65b593 23 | GIT ?= $(shell which git) 24 | GOLANGCILINT_VERSION ?= d2b1eea2c6171a1a1141a448a745335ce2e928a1 25 | GOLANGCILINT ?= $(GOBIN)/golangci-lint-$(GOLANGCILINT_VERSION) 26 | MISSPELL_VERSION ?= c0b55c8239520f6b5aa15a0207ca8b28027ba49e 27 | MISSPELL ?= $(GOBIN)/misspell-$(MISSPELL_VERSION) 28 | FUNCBENCH_VERSION ?= 36bc2803457da1bb58ae220523c78ed229834546 29 | FUNCBENCH ?= $(GOBIN)/funcbench-$(FUNCBENCH_VERSION) 30 | BENCH_FUNC ?="*" 31 | 32 | # Support gsed on OSX (installed via brew), falling back to sed. On Linux 33 | # systems gsed won't be installed, so will use sed as expected. 34 | SED ?= $(shell which gsed 2>/dev/null || which sed) 35 | 36 | ME ?= $(shell whoami) 37 | 38 | FAILLINT_VERSION ?= eb3cc62b577b2b0c488a41b91d49c60c676885b9 39 | FAILLINT ?=$(GOBIN)/faillint-$(FAILLINT_VERSION) 40 | 41 | # fetch_go_bin_version downloads (go gets) the binary from specific version and installs it in $(GOBIN)/- 42 | # arguments: 43 | # $(1): Install path. (e.g github.com/campoy/embedmd) 44 | # $(2): Tag or revision for checkout. 45 | define fetch_go_bin_version 46 | @mkdir -p $(GOBIN) 47 | @mkdir -p $(TMP_GOPATH) 48 | 49 | @echo ">> fetching $(1)@$(2) revision/version" 50 | @if [ ! -d '$(TMP_GOPATH)/src/$(1)' ]; then \ 51 | GOPATH='$(TMP_GOPATH)' GO111MODULE='off' go get -d '$(1)/...'; \ 52 | else \ 53 | CDPATH='' cd -- '$(TMP_GOPATH)/src/$(1)' && git fetch; \ 54 | fi 55 | @CDPATH='' cd -- '$(TMP_GOPATH)/src/$(1)' && git checkout -f -q '$(2)' 56 | @echo ">> installing $(1)@$(2)" 57 | @GOBIN='$(TMP_GOPATH)/bin' GOPATH='$(TMP_GOPATH)' GO111MODULE='off' go install '$(1)' 58 | @mv -- '$(TMP_GOPATH)/bin/$(shell basename $(1))' '$(GOBIN)/$(shell basename $(1))-$(2)' 59 | @echo ">> produced $(GOBIN)/$(shell basename $(1))-$(2)" 60 | 61 | endef 62 | 63 | # fetch_go_bin_version_mod downloads (go gets) the binary from specific version and installs it in $(GOBIN)/- 64 | # arguments: 65 | # $(1): Install path. (e.g github.com/campoy/embedmd) 66 | # $(2): Tag or revision for checkout. 67 | define fetch_go_bin_version_mod 68 | @mkdir -p $(GOBIN) 69 | @mkdir -p $(TMP_GOPATH) 70 | 71 | @echo ">> fetching $(1)@$(2) revision/version" 72 | @if [ ! -d '$(TMP_GOPATH)/src/$(1)' ]; then \ 73 | GOPATH='$(TMP_GOPATH)' GO111MODULE='off' go get -d -u '$(1)/...'; \ 74 | else \ 75 | CDPATH='' cd -- '$(TMP_GOPATH)/src/$(1)' && git fetch; \ 76 | fi 77 | @CDPATH='' cd -- '$(TMP_GOPATH)/src/$(1)' && git checkout -f -q '$(2)' 78 | @echo ">> installing $(1)@$(2)" 79 | @ # Extra step for those who does not vendor deps. 80 | @CDPATH='' cd -- '$(TMP_GOPATH)/src/$(1)' && go mod vendor 81 | @GOBIN='$(TMP_GOPATH)/bin' GOPATH='$(TMP_GOPATH)' GO111MODULE='off' go install '$(1)' 82 | @mv -- '$(TMP_GOPATH)/bin/$(shell basename $(1))' '$(GOBIN)/$(shell basename $(1))-$(2)' 83 | @echo ">> produced $(GOBIN)/$(shell basename $(1))-$(2)" 84 | 85 | endef 86 | 87 | 88 | define require_clean_work_tree 89 | @git update-index -q --ignore-submodules --refresh 90 | 91 | @if ! git diff-files --quiet --ignore-submodules --; then \ 92 | echo >&2 "cannot $1: you have unstaged changes."; \ 93 | git diff-files --name-status -r --ignore-submodules -- >&2; \ 94 | echo >&2 "Please commit or stash them."; \ 95 | exit 1; \ 96 | fi 97 | 98 | @if ! git diff-index --cached --quiet HEAD --ignore-submodules --; then \ 99 | echo >&2 "cannot $1: your index contains uncommitted changes."; \ 100 | git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2; \ 101 | echo >&2 "Please commit or stash them."; \ 102 | exit 1; \ 103 | fi 104 | 105 | endef 106 | 107 | help: ## Displays help. 108 | @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) 109 | 110 | .PHONY: all 111 | all: format build 112 | 113 | .PHONY: bench 114 | bench: ## Run $(BENCH_FUNC) benchmarks and compare with master using https://github.com/prometheus/test-infra/tree/master/funcbench. 115 | bench: $(FUNCBENCH) 116 | @echo ">> benchmark $(BENCH_FUNC) and compare with master" 117 | @$(FUNCBENCH) -v --bench-time=30s --timeout=2h --result-cache=/tmp/cache master '"$(BENCH_FUNC)"' 118 | 119 | .PHONY: bench-master 120 | bench-master: ## Run $(BENCH_FUNC) benchmarks and compare with newest using https://github.com/prometheus/test-infra/tree/master/funcbench. 121 | bench-master: $(FUNCBENCH) 122 | @echo ">> benchmark $(BENCH_FUNC) and compare with: $(shell git tag | grep -E ^v[0-9]+\.[0-9]\.[0-9]+$$ | sort -t . -k1,1n -k2,2n -k3,3n | tail -1)" 123 | @$(FUNCBENCH) -v --bench-time=30s --timeout=2h --result-cache=/tmp/cache "$(shell git tag | grep -E ^v[0-9]+\.[0-9]\.[0-9]+$$ | sort -t . -k1,1n -k2,2n -k3,3n | tail -1)" '"$(BENCH_FUNC)"' 124 | 125 | .PHONY: bench-release 126 | bench-release: ## Run $(BENCH_FUNC) benchmarks and compare with older release using https://github.com/prometheus/test-infra/tree/master/funcbench. 127 | bench-release: $(FUNCBENCH) 128 | @echo ">> benchmark $(BENCH_FUNC) and compare with" 129 | @$(FUNCBENCH) -v --bench-time=30s --timeout=2h --result-cache=/tmp/cache "$(shell git tag | grep -E ^v[0-9]+\.[0-9]\.[0-9]+$$ | sort -t . -k1,1n -k2,2n -k3,3n | grep -B1 $(CURRENT_RELEASE) | head -1)" '"$(BENCH_FUNC)"' 130 | 131 | .PHONY: deps 132 | deps: ## Ensures fresh go.mod and go.sum. 133 | @go mod tidy 134 | @go mod verify 135 | 136 | .PHONY: check-comments 137 | check-comments: ## Checks Go code comments if they have trailing period (excludes protobuffers and vendor files). Comments with more than 3 spaces at beginning are omitted from the check, example: '// - foo'. 138 | @printf ">> checking Go comments trailing periods\n\n\n" 139 | @./scripts/build-check-comments.sh 140 | 141 | .PHONY: format 142 | format: ## Formats Go code including imports and cleans up white noise. 143 | format: $(GOIMPORTS) check-comments 144 | @echo ">> formatting code" 145 | @$(GOIMPORTS) -w $(FILES_TO_FMT) 146 | @SED_BIN="$(SED)" scripts/cleanup-white-noise.sh $(FILES_TO_FMT) 147 | 148 | .PHONY: test 149 | test: ## Runs all Go unit tests. 150 | test: 151 | @echo ">> running unit tests" 152 | @go test $(shell go list ./... | grep -v /vendor/); 153 | 154 | .PHONY: check-git 155 | check-git: 156 | ifneq ($(GIT),) 157 | @test -x $(GIT) || (echo >&2 "No git executable binary found at $(GIT)."; exit 1) 158 | else 159 | @echo >&2 "No git binary found."; exit 1 160 | endif 161 | 162 | # PROTIP: 163 | # Add 164 | # --cpu-profile-path string Path to CPU profile output file 165 | # --mem-profile-path string Path to memory profile output file 166 | # to debug big allocations during linting. 167 | lint: ## Runs various static analysis against our code. 168 | lint: format check-git deps $(GOLANGCILINT) $(MISSPELL) $(FAILLINT) 169 | $(call require_clean_work_tree,"detected not clean master before running lint") 170 | @echo ">> verifying modules being imported" 171 | @$(FAILLINT) -paths "errors=github.com/pkg/errors,github.com/thanos-io/thanos/pkg/testutil=github.com/bwplotka/flagarize/testutil" ./... 172 | @$(FAILLINT) -paths "fmt.{Print,PrintfPrintln,Sprint}" -ignore-tests ./... 173 | @echo ">> examining all of the Go files" 174 | @go vet -stdmethods=false ./... 175 | @echo ">> linting all of the Go files GOGC=${GOGC}" 176 | @$(GOLANGCILINT) run 177 | @echo ">> detecting misspells" 178 | @find . -type f | grep -v vendor/ | grep -vE '\./\..*' | xargs $(MISSPELL) -error 179 | @echo ">> detecting white noise" 180 | @find . -type f \( -name "*.md" -o -name "*.go" \) | SED_BIN="$(SED)" xargs scripts/cleanup-white-noise.sh 181 | $(call require_clean_work_tree,"detected white noise") 182 | @echo ">> ensuring Copyright headers" 183 | @go run ./scripts/copyright 184 | $(call require_clean_work_tree,"detected files without copyright") 185 | 186 | $(EMBEDMD): 187 | $(call fetch_go_bin_version,github.com/campoy/embedmd,$(EMBEDMD_VERSION)) 188 | 189 | $(GOIMPORTS): 190 | $(call fetch_go_bin_version,golang.org/x/tools/cmd/goimports,$(GOIMPORTS_VERSION)) 191 | 192 | $(LICHE): 193 | $(call fetch_go_bin_version,github.com/raviqqe/liche,$(LICHE_VERSION)) 194 | 195 | $(GOLANGCILINT): 196 | $(call fetch_go_bin_version,github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCILINT_VERSION)) 197 | 198 | $(MISSPELL): 199 | $(call fetch_go_bin_version,github.com/client9/misspell/cmd/misspell,$(MISSPELL_VERSION)) 200 | 201 | $(FAILLINT): 202 | $(call fetch_go_bin_version,github.com/fatih/faillint,$(FAILLINT_VERSION)) 203 | 204 | $(FUNCBENCH): 205 | $(call fetch_go_bin_version_mod,github.com/prometheus/test-infra/funcbench,$(FUNCBENCH_VERSION)) 206 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flagarize 2 | [![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/bwplotka/flagarize) 3 | [![Latest Release](https://img.shields.io/github/release/bwplotka/flagarize.svg?style=flat-square)](https://github.com/bwplotka/flagarize/releases/latest) 4 | [![CI](https://github.com/bwplotka/flagarize/workflows/test/badge.svg)](https://github.com/bwplotka/flagarize/actions?query=workflow%3Atest) 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/bwplotka/flagarize)](https://goreportcard.com/report/github.com/bwplotka/flagarize) 6 | 7 | Flagarize your Go struct to initialize your even complex CLI config struct from flags! 8 | 9 | **NOTE: This project is in maintainance mode. This means that until new maintainer will be willing to support it, no additional features will be added. The project was fun to create and is fully functional, but benefits are not strong enough to justify lack of type safety her (thus lack of autocomplection).** 10 | 11 | ## Goals 12 | 13 | * Allow flag parsing for any struct field using Go struct tags. 14 | * Minimal dependencies: Only `"gopkg.in/alecthomas/kingpin.v2"`. 15 | * Extensible with [custom types](#custom-type-parsing) and [custom flagarizing](#custom-flags). 16 | * Native supports for all [kingpin](https://github.com/alecthomas/kingpin) flag types and more like [`regexp`](./regexp.go) , [`pathorcontent`](./pathorcontent.go), [`timeorduration`](./timeorduration.go). 17 | 18 | ## Requirements: 19 | 20 | * Go 1.3+ 21 | * `gopkg.in/alecthomas/kingpin.v2` 22 | 23 | 24 | ## Usage 25 | 26 | For each field of the configuration struct that you want to register as a flag, add ``flagarize:""`` struct tag. 27 | 28 | ### Flagarize tags 29 | 30 | Flagarize struct tag expected value to be map where key=values are separated by `|` (can be configured via `WithElemSep` function.) 31 | 32 | **Available keys:** 33 | 34 | * `name`: Name of the flag. If empty field name will be used and parsed to different case (e.g `FooBar` field will be `foo-bar`) 35 | * `help`: Usage description for the flag. If empty, value from string `FlagarizeHelp` field in the same struct will be used. 36 | * `hidden`: Optional. if `true` flag will be hidden. 37 | * `required`: Optional. if `true` flag will be required. 38 | * `default`: Optional. Value will be used as a value if the flag is not specified. Otherwise default value for type will be used. 39 | * `envvar`: Optional. Name of environment variable if needed next to the flag. 40 | * `short`: Optional. Short single character for a flag name alternative. 41 | * `placeholder` Optional. Flag placeholder for expected type. 42 | 43 | Short tag example: 44 | 45 | ```go 46 | type config struct { 47 | Field1 string `flagarize:"name=case9|help=help|hidden=true|required=true|default=some|envvar=LOL|short=z|placeholder="` 48 | } 49 | ``` 50 | 51 | ### Supported types 52 | 53 | Without extensions flagarize supports all kingpin supported types plus few more. For current supported types it's best to 54 | see `TestFlagarize_OK` unit test [here](flagarize_ext_test.go). 55 | 56 | ### Example 57 | 58 | See below example for usage: 59 | 60 | ```go 61 | func main() { 62 | // Create new kingpin app as usual. 63 | a := kingpin.New(filepath.Base(os.Args[0]), "") 64 | 65 | // Define you own config. 66 | type ComponentAOptions struct { 67 | Field1 []string `flagarize:"name=a.flag1|help=Help for field 1 in nested struct for component A."` 68 | } 69 | type ConfigForCLI struct { 70 | Field1 string `flagarize:"name=flag1|help=Help for field 1.|default=something"` 71 | Field2 *url.URL `flagarize:"name=flag2|help=Help for field 2.|placeholder="` 72 | Field3 int `flagarize:"name=flag3|help=Help for field 3.|default=2144"` 73 | Field4 flagarize.TimeOrDuration `flagarize:"name=flag4|help=Help for field 4.|default=1m|placeholder=