Skip to content

Commit

Permalink
Merge pull request #35 from ericjaystevens/feature_namedParamaters
Browse files Browse the repository at this point in the history
Feature named parameters
  • Loading branch information
ericjaystevens committed Aug 27, 2020
2 parents 15ea52c + 181e3f2 commit 54f8ca0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
3 changes: 3 additions & 0 deletions examples/helloWorld/lotsofargs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ arguments:
errorMsg: Please proved a valid value for text. Expected format is quoted text.
position: 0
required: true
shortName: t
- name: search
argtype: text
description: what you want to search for
position: 1
required: true
shortName: s
- name: replace
argtype: text
description: repalce the found text with
position: 2
required: false
shortName: r
- name: allCaps
argType: switch
description: return changed items in all caps
Expand Down
2 changes: 2 additions & 0 deletions examples/helloWorld/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ arguments:
description: text you want to print
errorMsg: foo is not a valid value for text. Expected format is quoted text.
position: 0
shortName: t
subcommands:
- name: reverse
description: reverses back what you type.
Expand All @@ -16,6 +17,7 @@ subcommands:
description: text you want to print
errorMsg: foo is not a valid value for text. Expected format is quoted text.
required: true
shortName: t
- name: quote
description: helps you stand on the shoulders of giants by using words from histories most articulate people
subcommands:
Expand Down
66 changes: 63 additions & 3 deletions slashparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Argument struct {
ErrorMsg string `yaml:"errorMsg" json:"errorMsg"`
Position int `yaml:"position" json:"position"`
Required bool `yaml:"required" json:"required"`
ShortName string `yaml:"shortName" json:"shortName"`
}

//SlashCommand defines the structure of a slash command string
Expand Down Expand Up @@ -180,19 +181,62 @@ func (s *SlashCommand) getValues(CommandAndArgs string) (map[string]string, erro
}

if strings.EqualFold(command, s.Name) {
return getArgsValues(CommandAndArgs[loc[1]:], s.Arguments, s.Name)
return s.getArgsValues(command, CommandAndArgs[loc[1]:], s.Arguments, s.Name)
}

subCommand, err := s.getSubCommand(command)
if err != nil {
return m, err
}

return getArgsValues(CommandAndArgs[loc[1]:], subCommand.Arguments, s.Name)
return s.getArgsValues(command, CommandAndArgs[loc[1]:], subCommand.Arguments, s.Name)

}

func getArgsValues(argString string, commandArgs []Argument, slashCommandName string) (m map[string]string, err error) {
func (s *SlashCommand) getNamedArgValues(commandString, argString string) (m map[string]string) {
m = make(map[string]string)

splitArgs := GetPositionalArgs(argString)
var argumentName string
for _, splitArg := range splitArgs {
if argumentName != "" {
m[argumentName] = splitArg
argumentName = ""
}
if strings.HasPrefix(splitArg, "--") {
argumentName = splitArg[2:]
} else if strings.HasPrefix(splitArg, "-") {
argument, _ := s.getArgumentFromShortName(commandString, splitArg[1:])
argumentName = argument.Name
}
}

return m
}

// getArgumentFromShortName returns an argument that matches the shortname
func (s *SlashCommand) getArgumentFromShortName(commandString string, shortName string) (argument Argument, err error) {
if strings.EqualFold(commandString, s.Name) {
for _, arg := range s.Arguments {
if arg.ShortName == shortName {
return arg, nil
}
}
return argument, fmt.Errorf("Unknown paramater '%s', see /%s help for more details", shortName, commandString)
}

subCommand, _ := s.getSubCommand(commandString)

for _, arg := range subCommand.Arguments {
if arg.ShortName == shortName {
return arg, nil
}
}

return argument, fmt.Errorf("Unknown paramater '%s', see /%s help for more details", shortName, commandString)
}

func (s *SlashCommand) getArgsValues(commandString string, argString string, commandArgs []Argument, slashCommandName string) (m map[string]string, err error) {

m = make(map[string]string)
missingArgs := make([]string, 0, 8)
Expand All @@ -201,6 +245,9 @@ func getArgsValues(argString string, commandArgs []Argument, slashCommandName st
for _, commandArg := range commandArgs {
position := commandArg.Position
if len(splitArgs) > position {
if strings.HasPrefix(splitArgs[position], "-") {
break
}
switch commandArg.ArgType {
case "text", "quoted text":
m[commandArg.Name] = splitArgs[position]
Expand All @@ -214,6 +261,19 @@ func getArgsValues(argString string, commandArgs []Argument, slashCommandName st
}
}

namedMap := s.getNamedArgValues(commandString, argString)

for k, v := range namedMap {
m[k] = v
for i, missingArg := range missingArgs {
if missingArg == v {
missingArgs[i] = missingArgs[len(missingArgs)-1]
missingArgs[len(missingArgs)-1] = ""
missingArg = missingArg[:len(missingArg)-1]
}
}
}

if len(missingArgs) > 0 {
return m, getMissingArgError(missingArgs, slashCommandName)
}
Expand Down
34 changes: 34 additions & 0 deletions slashparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestNewSlashCommand(t *testing.T) {
Description: "text you want to print",
ErrorMsg: "foo is not a valid value for text. Expected format is quoted text.",
Position: 0,
ShortName: "t",
},
},
SubCommands: []SubCommand{
Expand All @@ -55,6 +56,7 @@ func TestNewSlashCommand(t *testing.T) {
ErrorMsg: "foo is not a valid value for text. Expected format is quoted text.",
Position: 0,
Required: true,
ShortName: "t",
},
},
commandPaths: []string{"Print reverse"},
Expand Down Expand Up @@ -110,6 +112,7 @@ func TestNewSlashCommand(t *testing.T) {
Description: "text you want to print",
ErrorMsg: "foo is not a valid value for text. Expected format is quoted text.",
Position: 0,
ShortName: "t",
},
},
SubCommands: []SubCommand{
Expand All @@ -124,6 +127,7 @@ func TestNewSlashCommand(t *testing.T) {
ErrorMsg: "foo is not a valid value for text. Expected format is quoted text.",
Position: 0,
Required: true,
ShortName: "t",
},
},
commandPaths: []string{"Print reverse"},
Expand Down Expand Up @@ -272,6 +276,36 @@ func TestGetValues(t *testing.T) {
slashDef: SimpleDef,
want: map[string]string{"authorName": "foo"},
},
{
testName: "slash command single named argument",
commandAndArgs: "/print --text foo",
slashDef: SimpleDef,
want: map[string]string{"text": "foo"},
},
{
testName: "positional followed by named arguments",
commandAndArgs: `/search "moon river" --search river --replace rising`,
slashDef: lotsOfArgsDef,
want: map[string]string{"text": "moon river", "search": "river", "replace": "rising"},
},
{
testName: "slash command single short named paramater",
commandAndArgs: `/print -t foo`,
slashDef: SimpleDef,
want: map[string]string{"text": "foo"},
},
{
testName: "slash sub command single short named paramater",
commandAndArgs: `/print reverse -t foo`,
slashDef: SimpleDef,
want: map[string]string{"text": "foo"},
},
{
testName: "mixed short and long named paramater",
commandAndArgs: `/search -t "this land is your land" --search land -r hand`,
slashDef: lotsOfArgsDef,
want: map[string]string{"text": "this land is your land", "search": "land", "replace": "hand"},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 54f8ca0

Please sign in to comment.