github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/args/parse.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the Apache License Version 2.0.
     3  // This product includes software developed at Datadog (https://www.datadoghq.com/).
     4  // Copyright 2021-present Datadog, Inc.
     5  
     6  // Package args holds args related files
     7  package args
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/DataDog/datadog-agent/pkg/security/secl/model"
    13  )
    14  
    15  // ParseProcessFlags parses the process flags
    16  func ParseProcessFlags(args []string) []string {
    17  	flags := make([]string, 0)
    18  	for _, arg := range args {
    19  		if len(arg) > 1 && arg[0] == '-' {
    20  			isFlag := true
    21  			name := arg[1:]
    22  			if len(name) >= 1 && name[0] == '-' {
    23  				name = name[1:]
    24  				isFlag = false
    25  			}
    26  
    27  			isOption := false
    28  			for _, r := range name {
    29  				isFlag = isFlag && model.IsAlphaNumeric(r)
    30  				isOption = isOption || r == '='
    31  			}
    32  
    33  			if len(name) > 0 {
    34  				if isFlag {
    35  					for _, r := range name {
    36  						flags = append(flags, string(r))
    37  					}
    38  				}
    39  				if !isOption && len(name) > 1 {
    40  					flags = append(flags, name)
    41  				}
    42  			}
    43  		}
    44  	}
    45  	return flags
    46  }
    47  
    48  // ParseProcessOptions parses the process options
    49  func ParseProcessOptions(args []string) []string {
    50  	options := make([]string, 0)
    51  	for i := 0; i < len(args); i++ {
    52  		arg := args[i]
    53  		if len(arg) > 1 && arg[0] == '-' {
    54  			name := arg[1:]
    55  			if len(name) >= 1 && name[0] == '-' {
    56  				name = name[1:]
    57  			}
    58  			if len(name) > 0 && model.IsAlphaNumeric(rune(name[0])) {
    59  				if index := strings.IndexRune(name, '='); index == -1 {
    60  					if i < len(args)-1 && (len(args[i+1]) == 0 || args[i+1][0] != '-') {
    61  						options = append(options, name+"="+args[i+1])
    62  						i++
    63  					}
    64  				} else {
    65  					options = append(options, name)
    66  				}
    67  			}
    68  		}
    69  	}
    70  	return options
    71  }