Skip to content

Commit

Permalink
add support for --help
Browse files Browse the repository at this point in the history
  • Loading branch information
majewsky committed Feb 7, 2024
1 parent 59763aa commit b3ac10c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/http"
"os"
"regexp"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -66,8 +67,11 @@ func main() {
defer undoMaxprocs()

//first two arguments must be task name and configuration file
if slices.Contains(os.Args, "--help") {
printUsageAndExit(0)
}
if len(os.Args) < 3 {
printUsageAndExit()
printUsageAndExit(1)
}
taskName, configPath, remainingArgs := os.Args[1], os.Args[2], os.Args[3:]
bininfo.SetTaskName(taskName)
Expand Down Expand Up @@ -115,7 +119,7 @@ func main() {
case "test-scan-capacity":
taskTestScanCapacity(cluster, remainingArgs)
default:
printUsageAndExit()
printUsageAndExit(1)
}
}

Expand All @@ -128,17 +132,17 @@ Usage:
\t%s test-scan-capacity <config-file> <capacitor>
`), `\t`, "\t", -1) + "\n"

func printUsageAndExit() {
func printUsageAndExit(exitCode int) {
fmt.Fprintln(os.Stderr, strings.Replace(usageMessage, "%s", os.Args[0], -1))
os.Exit(1)
os.Exit(exitCode)
}

////////////////////////////////////////////////////////////////////////////////
// task: collect

func taskCollect(cluster *core.Cluster, args []string) {
if len(args) != 0 {
printUsageAndExit()
printUsageAndExit(1)
}

ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -193,7 +197,7 @@ func taskCollect(cluster *core.Cluster, args []string) {

func taskServe(cluster *core.Cluster, args []string, provider *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) {
if len(args) != 0 {
printUsageAndExit()
printUsageAndExit(1)
}

//connect to database
Expand Down Expand Up @@ -227,7 +231,7 @@ func taskServe(cluster *core.Cluster, args []string, provider *gophercloud.Provi

func taskTestGetQuota(cluster *core.Cluster, args []string) {
if len(args) != 2 {
printUsageAndExit()
printUsageAndExit(1)
}

serviceType := args[1]
Expand Down Expand Up @@ -269,7 +273,7 @@ func taskTestGetRates(cluster *core.Cluster, args []string) {
case 3:
prevSerializedState = args[2]
default:
printUsageAndExit()
printUsageAndExit(1)
}

serviceType := args[1]
Expand Down Expand Up @@ -350,7 +354,7 @@ func dumpGeneratedPrometheusMetrics() {

func taskTestSetQuota(cluster *core.Cluster, args []string) {
if len(args) < 3 {
printUsageAndExit()
printUsageAndExit(1)
}

serviceType := args[1]
Expand All @@ -361,7 +365,7 @@ func taskTestSetQuota(cluster *core.Cluster, args []string) {
for _, arg := range args[2:] {
match := quotaValueRx.FindStringSubmatch(arg)
if match == nil {
printUsageAndExit()
printUsageAndExit(1)
}
val, err := strconv.ParseUint(match[2], 10, 64)
if err != nil {
Expand All @@ -378,7 +382,7 @@ func taskTestSetQuota(cluster *core.Cluster, args []string) {

func taskTestScanCapacity(cluster *core.Cluster, args []string) {
if len(args) != 1 {
printUsageAndExit()
printUsageAndExit(1)
}

capacitorID := args[0]
Expand Down

0 comments on commit b3ac10c

Please sign in to comment.