| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| // Package cli provides utilities for building robust command-line interfaces. |
| package cli |
| |
| import "flag" |
| |
| // NewFlagSet creates a private FlagSet with ContinueOnError and inherits |
| // all flags currently registered in the global flag.CommandLine (e.g. glog flags). |
| // This prevents silent os.Exit(2) crashes and allows for explicit error handling |
| // in CI/CQ environments. |
| func NewFlagSet(name string) *flag.FlagSet { |
| fs := flag.NewFlagSet(name, flag.ContinueOnError) |
| |
| // Copy flags from the global FlagSet to our private FlagSet. |
| // This ensures that system-wide flags like -log_dir or -v (from glog) |
| // continue to work correctly in the isolated set. |
| flag.CommandLine.VisitAll(func(f *flag.Flag) { |
| fs.Var(f.Value, f.Name, f.Usage) |
| }) |
| |
| return fs |
| } |