-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
237 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
xx | ||
.vscode/launch.json | ||
*.exe | ||
*.exe~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func parseArgsInternal(args []string) (hasHelp bool, hasVersion bool, commands []string, nonFlagCount int) { | ||
commands = []string{} | ||
if len(args) == 0 { | ||
hasHelp = true | ||
return | ||
} | ||
|
||
helpForms := map[string]bool{ | ||
"--help": true, "-help": true, "help": true, | ||
"--h": true, "-h": true, | ||
} | ||
|
||
versionForms := map[string]bool{ | ||
"--version": true, "-version": true, "version": true, | ||
"--v": true, "-v": true, | ||
} | ||
|
||
for i, arg := range args { | ||
if helpForms[arg] { | ||
hasHelp = true | ||
continue | ||
} | ||
if versionForms[arg] { | ||
hasVersion = true | ||
continue | ||
} | ||
commands = append(commands, arg) | ||
if i != 0 && len(arg) == 0 || arg[0] != '-' { | ||
nonFlagCount++ | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func cleanArgs(args []string) []string { | ||
programName := args[:1] // program name | ||
|
||
hasHelp, hasVersion, commands, _ := parseArgsInternal(args[1:]) | ||
if hasHelp { | ||
result := append(programName, "help") | ||
if len(commands) > 0 { | ||
return append(result, commands[0]) | ||
} | ||
return result | ||
} | ||
|
||
if hasVersion { | ||
return append(programName, "version") | ||
} | ||
|
||
return append(programName, commands...) | ||
} | ||
|
||
func validateArgs(args []string, expectedCmdCount, expectedFlagCount int) error { | ||
_, _, flags, cmdCnt := parseArgsInternal(args) | ||
if cmdCnt != expectedCmdCount || len(flags) != expectedFlagCount { | ||
if cmdCnt > expectedCmdCount { | ||
var err error | ||
if unknown := getUnknownCmd(args); len(unknown) > 0 { | ||
err = fmt.Errorf("command '%s' not found", unknown) | ||
} else { | ||
err = fmt.Errorf("use only one command at a time") | ||
} | ||
return err | ||
} | ||
return fmt.Errorf("argument mismatch: %v %v %d %d", args, flags, cmdCnt, len(flags)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.