github.com/WindomZ/go-commander@v1.2.2/regexp.go (about)

     1  package commander
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  // defineCommand define commands
     9  func defineCommand(str string) string {
    10  	return regexp.MustCompile(`[A-Za-z0-9_-]+`).FindString(str)
    11  }
    12  
    13  // regexpCommand Regular screening out docopt commands
    14  func regexpCommand(str string) []string {
    15  	return regexp.MustCompile(`[A-Za-z0-9_-]+`).FindAllString(
    16  		regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str), -1)
    17  }
    18  
    19  // regexpArgument Regular screening out docopt arguments
    20  func regexpArgument(str string) []string {
    21  	return regexp.MustCompile(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`).
    22  		FindAllString(str, -1)
    23  }
    24  
    25  // regexpOption Regular screening out docopt options
    26  func regexpOption(str string) []string {
    27  	return regexp.MustCompile(`-{1,2}[A-Za-z0-9_-]+\b`).
    28  		FindAllString(regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`).
    29  			ReplaceAllString(str, ""), -1)
    30  }
    31  
    32  //// containCommand str contain docopt command format
    33  //func containCommand(str string) (ok bool) {
    34  //	ok, _ = regexp.MatchString(`[A-Za-z0-9_-]+`,
    35  //		regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str))
    36  //	return
    37  //}
    38  //
    39  //// containArgument str contain docopt argument format
    40  //func containArgument(str string) (ok bool) {
    41  //	ok, _ = regexp.MatchString(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`, str)
    42  //	return
    43  //}
    44  //
    45  //// containOption str contain docopt option format
    46  //func containOption(str string) (ok bool) {
    47  //	ok, _ = regexp.MatchString(`-{1,2}[A-Za-z0-9_-]+\b`,
    48  //		regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`).
    49  //			ReplaceAllString(str, ""))
    50  //	return
    51  //}
    52  
    53  //// isCommand str contain docopt command format
    54  //func isCommand(str string) bool {
    55  //	return !isArgument(str) && !isOption(str)
    56  //}
    57  
    58  // isArgument str contain docopt command format
    59  func isArgument(str string) (ok bool) {
    60  	ok, _ = regexp.MatchString(`^<[A-Za-z0-9_-]+>$`, str)
    61  	return
    62  }
    63  
    64  // isOption str contain docopt command format
    65  func isOption(str string) (ok bool) {
    66  	ok, _ = regexp.MatchString(`^-{1,2}[A-Za-z0-9_-]+$`, str)
    67  	return
    68  }
    69  
    70  // replaceCommand replace name of command in usage with new name
    71  func replaceCommand(usage, oldName, newName string) string {
    72  	return strings.Replace(usage, oldName, newName, 1)
    73  }
    74  
    75  // firstParameter find first parameter in usage
    76  func firstParameter(usage string) string {
    77  	return regexp.MustCompile(`^([^\s=]+){1}`).FindString(strings.TrimSpace(usage))
    78  }