Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implement subsub -> sub; subsub -> options

  • Loading branch information...
commit 90d689935c5991b07a52e9e12d0875f28021c1b7 1 parent 6ea7544
@kesselborn authored
View
2  TESTED_GO_RELEASE
@@ -1 +1 @@
-go version weekly.2012-03-13
+go version weekly.2012-03-04 +f4470a54e6db
View
16 subcommand_options_test.go
@@ -11,8 +11,22 @@ import (
"testing"
)
+func equalSubCommandOptions(sco1 SubCommandOptions, sco2 SubCommandOptions) (equal bool) {
+ if equalOptions(sco1.Global, sco2.Global) && len(sco1.SubCommands) == len(sco2.SubCommands) {
+ for key, options := range sco1.SubCommands {
+ if !equalOptions(options, sco2.SubCommands[key]) {
+ goto loopend
+ }
+ }
+ equal = true
+ }
+loopend:
+
+ return
+}
+
func equalOptions(options1 Options, options2 Options) (equal bool) {
- if len(options1.Definitions) == len(options2.Definitions) && options1.Description == options2.Description {
+ if options1.Description == options2.Description && len(options1.Definitions) == len(options2.Definitions) {
for i := 0; i < len(options1.Definitions); i++ {
if options1.Definitions[i] != options2.Definitions[i] {
goto loopend
View
44 subsubcommand_options.go
@@ -6,40 +6,42 @@
package getopt
import (
- //"os"
- //"path/filepath"
- //"sort"
- //"fmt"
+//"os"
+//"path/filepath"
+//"sort"
+//"strings"
+//"fmt"
)
type Scopes map[string]SubCommandOptions
-type SubSubCommandOptions struct{
- Global Option
- Scopes Scopes
+type SubSubCommandOptions struct {
+ Global Options
+ Scopes Scopes
}
-func (ssco SubSubCommandOptions) flattenToSubcommandOptions(subCommand string) (sco SubCommandOptions, err *GetOptError) {
- genericOptions := ssco.Global
+func (ssco SubSubCommandOptions) flattenToSubcommandOptions(scope string) (sco SubCommandOptions, err *GetOptError) {
+ globalCommand := ssco.Global
+ var present bool
- if subSubCommandOptions, present := ssco.Scopes[subCommand]; present == true {
+ if sco, present = ssco.Scopes[scope]; present == true {
+ //print("\n======= " + scope + "========\n"); print(strings.Replace(fmt.Sprintf("%#v", scope.SubCommands), "getopt", "\n", -1)); print("\n================")
+ sco.Global.Definitions = append(globalCommand.Definitions, sco.Global.Definitions...)
+ } else {
+ err = &GetOptError{UnknownSubcommand, "Unknown scope: " + scope}
+ }
- if subCommand != "*" {
- for _, option := range genericOptions.Definitions {
- options.Definitions = append(options.Definitions, option)
- }
- }
+ return
+}
- for _, option := range subCommandOptions.Definitions {
- options.Definitions = append(options.Definitions, option)
- options.Description = subCommandOptions.Description
- }
- } else {
- err = &GetOptError{UnknownSubcommand, "Unknown command: " + subCommand}
+func (ssco SubSubCommandOptions) flattenToOptions(scope string, subCommand string) (options Options, err *GetOptError) {
+ if sco, err := ssco.flattenToSubcommandOptions(scope); err == nil {
+ options, err = sco.flattenToOptions(subCommand)
}
return
}
+
//func (sco SubCommandOptions) findSubcommand() (subCommand string, err *GetOptError) {
// options := sco["*"]
// subCommand = "*"
View
215 subsubcommand_options_test.go
@@ -11,113 +11,150 @@ import (
"testing"
)
-func equalSubCommandOptions(sco1 SubCommandOptions, sco2 SubCommandOptions) (equal bool) {
- if len(sco1) == len(sco2) {
- for key := range sco1 {
- if !equalOptionsArray(sco1[key], sco2[key]) {
- goto loopend
- }
- }
- equal = true
- }
-loopend:
-
- return
-}
-
func TestSubSubcommandOptionsConverter(t *testing.T) {
- ssco := SubSubCommandOptions{
- Global{
- "global description",
- Definitions{
- {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
- {"server|s", "doozer server", Optional, ""},
- {"scope", "scope", IsSubcommand, ""},
- },
- },
- Scopes{
- "app": {
- Global{
- "app description",
- {"command", "command to execute", IsSubcommand, ""},
- {"foo|f", "a param", Optional, ""},
- },
- SubCommands{
- "getenv": {
- "app getenv description",
- Definitions{
- {"name", "app's name", IsArg | Required, ""},
- {"key", "environment variable's name", IsArg | Required, ""},
- },
- },
- "revision": {
- "app revision description",
- Definitions{
- {"rev|r", "revision", IsArg | Required, ""},
- },
- },
- },
- },
- },
- }
+ ssco := SubSubCommandOptions{
+ Options{
+ "global description",
+ Definitions{
+ {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
+ {"server|s", "doozer server", Optional, ""},
+ {"scope", "scope", IsSubcommand, ""},
+ },
+ },
+ Scopes{
+ "app": {
+ Options{
+ "app description",
+ Definitions{
+ {"foo|f", "a param", Optional, ""},
+ {"command", "command to execute", IsSubcommand, ""},
+ },
+ },
+ SubCommands{
+ "getenv": {
+ "app getenv description",
+ Definitions{
+ {"key", "environment variable's name", IsArg | Required, ""},
+ },
+ },
+ "setenv": {
+ "app setenv description",
+ Definitions{
+ {"name", "app's name", IsArg | Required, ""},
+ {"key", "environment variable's name", IsArg | Required, ""},
+ },
+ },
+ },
+ },
+ "revision": {
+ Options{
+ "app revision description",
+ Definitions{
+ {"rev|r", "revision", IsArg | Required, ""},
+ {"command", "command to execute", IsSubcommand, ""},
+ },
+ },
+ SubCommands{
+ "list": {
+ "list revisions",
+ Definitions{
+ {"all|a", "long list output", Flag, ""},
+ },
+ },
+ },
+ },
+ },
+ }
- expectedAppSubOptions := SubCommandOptions{
- Global{
- "app description",
- Definitions{
- {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
- {"server|s", "doozer server", Optional, ""},
- {"scope", "scope", IsSubcommand, ""},
- {"command", "command to execute", IsSubcommand, ""},
- },
- },
- SubCommands{
- "getenv": {
- "app getenv description",
- Definitions{
- {"name", "app's name", IsArg | Required, ""},
- {"key", "environment variable's name", IsArg | Required, ""},
- },
- },
- "revision": {
- "app revision description",
- Definitions{
- {"rev|r", "revision", IsArg | Required, ""},
- },
- },
- },
- }
+ expectedAppSubOptions := SubCommandOptions{
+ Options{
+ "app description",
+ Definitions{
+ {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
+ {"server|s", "doozer server", Optional, ""},
+ {"scope", "scope", IsSubcommand, ""},
+ {"foo|f", "a param", Optional, ""},
+ {"command", "command to execute", IsSubcommand, ""},
+ },
+ },
+ SubCommands{
+ "getenv": {
+ "app getenv description",
+ Definitions{
+ {"key", "environment variable's name", IsArg | Required, ""},
+ },
+ },
+ "setenv": {
+ "app setenv description",
+ Definitions{
+ {"name", "app's name", IsArg | Required, ""},
+ {"key", "environment variable's name", IsArg | Required, ""},
+ },
+ },
+ },
+ }
- expectedRevisionOptions := Options{
- "*": {
- {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
- {"server|s", "doozer server", Optional, ""},
- {"scope", "scope", IsSubcommand, ""},
- {"command", "command to execute", IsSubcommand, ""}},
- "list": {
- {"app|n", "app's name", IsArg | Required, ""}},
+ expectedRevisionOptions := SubCommandOptions{
+ Options{
+ "app revision description",
+ Definitions{
+ {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
+ {"server|s", "doozer server", Optional, ""},
+ {"scope", "scope", IsSubcommand, ""},
+ {"rev|r", "revision", IsArg | Required, ""},
+ {"command", "command to execute", IsSubcommand, ""},
+ },
+ },
+ SubCommands{
+ "list": {
+ "list revisions",
+ Definitions{
+ {"all|a", "long list output", Flag, ""},
+ },
+ },
+ },
}
- if _, err := sco.flattenToSubcommandOptions("app"); err != nil {
+ if _, err := ssco.flattenToSubcommandOptions("app"); err != nil {
t.Errorf("conversion SuSubCommandOptions -> SubCommandOptions failed (app); \nGot the following error: %s", err.Message)
}
- if options, _ := sco.flattenToSubcommandOptions("app"); equalSubCommandOptions(options, expectedAppSubOptions) == false {
- t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (app); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedAppSubOptions)
+ if sco, _ := ssco.flattenToSubcommandOptions("app"); equalSubCommandOptions(sco, expectedAppSubOptions) == false {
+ t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (app); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", sco, expectedAppSubOptions)
}
- if _, err := sco.flattenToSubcommandOptions("revision"); err != nil {
+ if _, err := ssco.flattenToSubcommandOptions("revision"); err != nil {
t.Errorf("conversion SuSubCommandOptions -> SubCommandOptions failed (revision); \nGot the following error: %s", err.Message)
}
- if options, _ := sco.flattenToSubcommandOptions("revision"); equalSubCommandOptions(options, expectedRevisionOptions) == false {
- t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (revision); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedRevisionOptions)
+ if sco, _ := ssco.flattenToSubcommandOptions("revision"); equalSubCommandOptions(sco, expectedRevisionOptions) == false {
+ t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (revision); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", sco, expectedRevisionOptions)
}
- if _, err := sco.flattenToOptions("nonexistantsubcommand"); err.ErrorCode != UnknownSubcommand {
+ if _, err := ssco.flattenToSubcommandOptions("nonexistantsubcommand"); err.ErrorCode != UnknownSubcommand {
t.Errorf("non existant sub command didn't throw error")
}
+ expectedAppGetEnvOptions := Options{
+ "app getenv description",
+ Definitions{
+ {"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
+ {"server|s", "doozer server", Optional, ""},
+ {"scope", "scope", IsSubcommand, ""},
+ {"foo|f", "a param", Optional, ""},
+ {"command", "command to execute", IsSubcommand, ""},
+ {"key", "environment variable's name", IsArg | Required, ""},
+ },
+ }
+
+ if _, err := ssco.flattenToOptions("app", "getenv"); err != nil {
+ t.Errorf("conversion SubSubCommandOptions -> Options failed (app/getenv); \nGot the following error: %s", err.Message)
+ }
+
+ if options, _ := ssco.flattenToOptions("app", "getenv"); equalOptions(options, expectedAppGetEnvOptions) == false {
+ t.Errorf("conversion SubCommandOptions -> Options failed (app/getenv); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedAppGetEnvOptions)
+ }
+
}
//func TestSubcommandOptionsSubCommandFinder(t *testing.T) {
Please sign in to comment.
Something went wrong with that request. Please try again.