github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/arguments/view.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package arguments
     5  
     6  // View represents the global command-line arguments which configure the view.
     7  type View struct {
     8  	// NoColor is used to disable the use of terminal color codes in all
     9  	// output.
    10  	NoColor bool
    11  
    12  	// CompactWarnings is used to coalesce duplicate warnings, to reduce the
    13  	// level of noise when multiple instances of the same warning are raised
    14  	// for a configuration.
    15  	CompactWarnings bool
    16  }
    17  
    18  // ParseView processes CLI arguments, returning a View value and a
    19  // possibly-modified slice of arguments. If any of the supported flags are
    20  // found, they will be removed from the slice.
    21  func ParseView(args []string) (*View, []string) {
    22  	common := &View{}
    23  
    24  	// Keep track of the length of the returned slice. When we find an
    25  	// argument we support, i will not be incremented.
    26  	i := 0
    27  	for _, v := range args {
    28  		switch v {
    29  		case "-no-color":
    30  			common.NoColor = true
    31  		case "-compact-warnings":
    32  			common.CompactWarnings = true
    33  		default:
    34  			// Unsupported argument: move left to the current position, and
    35  			// increment the index.
    36  			args[i] = v
    37  			i++
    38  		}
    39  	}
    40  
    41  	// Reduce the slice to the number of unsupported arguments. Any remaining
    42  	// to the right of i have already been moved left.
    43  	args = args[:i]
    44  
    45  	return common, args
    46  }