Permalink
Browse files

...

  • Loading branch information...
1 parent 76d7169 commit 04a780a426c3c5b0805fba5e3a7634e60c360fc7 Daniel Bornkessel committed Mar 29, 2012
View
@@ -2,6 +2,9 @@ build: fmt
go version > TESTED_GO_RELEASE
go build -x
+install:
+ go install
+
fmt:
gofmt -s=true -w */*.go *.go
View
@@ -1 +1 @@
-go version weekly.2012-03-04 +f4470a54e6db
+go version weekly.2012-03-27
View
@@ -9,4 +9,4 @@ Subcommands:
* Checks: "*" must not contain IsArgs
* Checks: "*" IsSubcommand must be last
* check: subcommand: command has to be first options
-
+ * check: sub and subsub contains at least one option w/ IsSubCommand flag
@@ -46,10 +46,12 @@ func main() {
fmt.Printf("debug: %#v\n", options["debug"].Bool)
fmt.Printf("config: %#v\n", options["config"].String)
fmt.Printf("ports: %#v\n", options["ports"].IntArray)
+ fmt.Printf("sports: %#v\n", options["sports"].IntArray)
fmt.Printf("secondaryports: %#v\n", options["sports"].IntArray)
fmt.Printf("instances: %#v\n", options["instances"].Int)
fmt.Printf("keys: %#v\n", options["keys"].StrArray)
fmt.Printf("logfile: %#v\n", options["logfile"].String)
+ fmt.Printf("directories: %#v\n", options["directories"].String)
fmt.Printf("files: %#v\n", options["files"].StrArray)
fmt.Printf("arguments: %#v\n", arguments)
@@ -11,7 +11,7 @@ func main() {
getopt.Options{
"global description",
getopt.Definitions{
- {"foo|f", "some arg", getopt.Optional, ""},
+ {"foo|f", "some arg", getopt.Optional | getopt.ExampleIsDefault, "foo"},
{"command", "command to execute", getopt.IsSubCommand, ""},
},
},
@@ -12,7 +12,7 @@ func main() {
"global description",
getopt.Definitions{
{"config|c", "config file", getopt.IsConfigFile | getopt.ExampleIsDefault, "/etc/visor.conf"},
- {"server|s", "doozer server", getopt.Optional, ""},
+ {"server|s", "doozer server", getopt.Required, ""},
{"scope", "scope", getopt.IsSubCommand, ""},
},
},
@@ -81,16 +81,26 @@ func (ssco SubSubCommandOptions) findScopeAndSubCommand() (scope string, subComm
func (ssco SubSubCommandOptions) ParseCommandLine() (scope string, subCommand string, options map[string]OptionValue, arguments []string, passThrough []string, err *GetOptError) {
var scopeScError *GetOptError
+ var flattenedOptions Options
scope, subCommand, scopeScError = ssco.findScopeAndSubCommand()
- flattenedOptions, _ := ssco.flattenToOptions(scope, subCommand)
+
+ switch {
+ case subCommand == "":
+ flattenedOptions = ssco.Global
+ case scope == "":
+ flattenedOptions = ssco.Global
+ default:
+ flattenedOptions, _ = ssco.flattenToOptions(scope, subCommand)
+ }
+
options, arguments, passThrough, err = flattenedOptions.ParseCommandLine()
if len(arguments) > 2 {
arguments = arguments[2:]
}
- if scopeScError != nil {
+ if scopeScError != nil && err == nil {
err = scopeScError
}
@@ -71,6 +71,55 @@ func testingSubSubDefinitions() (ssco SubSubCommandOptions) {
return
}
+
+func TestExampleIsDefaultError(t *testing.T) {
+ ssco := SubSubCommandOptions{
+ Options{
+ "global description",
+ Definitions{
+ {"server|s", "doozer server", Required | ExampleIsDefault, ""},
+ {"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, ""},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ os.Args = []string{"prog", "app", "getenv", "foo"}
+
+ _, _, _, _, _, err := ssco.ParseCommandLine()
+
+ if err != nil {
+ t.Errorf("got error where no error was: %#v", err)
+ }
+
+ os.Args = []string{"prog", "nonexistantscope"}
+
+ _, _, _, _, _, err = ssco.ParseCommandLine()
+
+ if err.ErrorCode != UnknownScope {
+ t.Errorf("wrong scope was detected incorrectly, got error: %d: %s", err.ErrorCode, err.Error())
+ }
+
+}
+
func TestSubSubCommandOptionsConverter(t *testing.T) {
ssco := testingSubSubDefinitions()

0 comments on commit 04a780a

Please sign in to comment.