github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/flag/no_args.go (about)

     1  // Copyright (c) 2019 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package flag
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  )
    12  
    13  // CheckNoArgs checks if any positional arguments were provided and if so, exit with error.
    14  func CheckNoArgs() error {
    15  	if !flag.Parsed() {
    16  		panic("CheckNoArgs must be called after flags have been parsed")
    17  	}
    18  
    19  	if flag.NArg() == 0 {
    20  		return nil
    21  	}
    22  	return fmt.Errorf("%s doesn't accept positional arguments: %s", os.Args[0], flag.Arg(0))
    23  }