[lxc-devel] [lxd/master] lxd-benchmark: Fix --help and --version handling

stgraber on Github lxc-bot at linuxcontainers.org
Wed Mar 8 22:20:45 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170308/d78ab201/attachment.bin>
-------------- next part --------------
From 932a75a1847fb4c69ea2c0906a53fd53c728605d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 8 Mar 2017 17:20:05 -0500
Subject: [PATCH] lxd-benchmark: Fix --help and --version handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 test/lxd-benchmark/main.go | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/test/lxd-benchmark/main.go b/test/lxd-benchmark/main.go
index e8f619c..a6c9544 100644
--- a/test/lxd-benchmark/main.go
+++ b/test/lxd-benchmark/main.go
@@ -12,6 +12,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/gnuflag"
+	"github.com/lxc/lxd/shared/version"
 )
 
 var argCount = gnuflag.Int("count", 100, "Number of containers to create")
@@ -32,16 +33,32 @@ func main() {
 
 func run(args []string) error {
 	// Parse command line
-	gnuflag.Parse(true)
-
 	if len(os.Args) == 1 || !shared.StringInSlice(os.Args[1], []string{"spawn", "delete"}) {
-		fmt.Printf("Usage: %s spawn [--count=COUNT] [--image=IMAGE] [--privileged=BOOL] [--parallel=COUNT]\n", os.Args[0])
-		fmt.Printf("       %s delete [--parallel=COUNT]\n\n", os.Args[0])
-		gnuflag.Usage()
-		fmt.Printf("\n")
-		return fmt.Errorf("An action (spawn or delete) must be passed.")
+		if len(os.Args) > 1 && os.Args[1] == "--version" {
+			fmt.Println(version.Version)
+			return nil
+		}
+
+		out := os.Stderr
+		if len(os.Args) > 1 && os.Args[1] == "--help" {
+			out = os.Stdout
+		}
+		gnuflag.SetOut(out)
+
+		fmt.Fprintf(out, "Usage: %s spawn [--count=COUNT] [--image=IMAGE] [--privileged=BOOL] [--parallel=COUNT]\n", os.Args[0])
+		fmt.Fprintf(out, "       %s delete [--parallel=COUNT]\n\n", os.Args[0])
+		gnuflag.PrintDefaults()
+		fmt.Fprintf(out, "\n")
+
+		if len(os.Args) > 1 && os.Args[1] == "--help" {
+			return nil
+		}
+
+		return fmt.Errorf("An valid action (spawn or delete) must be passed.")
 	}
 
+	gnuflag.Parse(true)
+
 	// Connect to LXD
 	c, err := lxd.NewClient(&lxd.DefaultConfig, "local")
 	if err != nil {


More information about the lxc-devel mailing list