2

This code parse command-line arguments. If I type "/netstat -c /etc/config -I eth0", it should is :" c /etc/config \n i eth0", but it's not . Terminal output is :

c configfile

c interface

./netstat -c /etc/config -i eth0
c configfile
c interface

Code is as below:

package main

import (
    "flag"
    "fmt"
)

type CmdSt struct {
    configPtr    string
    interfacePtr string
}

var cmdSt CmdSt

func usage() {

    cmdSt.configPtr = *flag.String("c", "configfile", "configure file to parse ")
    cmdSt.interfacePtr = *flag.String("i", "interface", "capture network interface")
    /*
        a := flag.String("c", "configfile", "configure file to parse ")
        b := flag.String("i", "interface", "capture network interface")
    */

    flag.Parse()

    fmt.Println("c", cmdSt.configPtr)
    fmt.Println("i", cmdSt.interfacePtr)
    /*
        fmt.Println("c:", *a)
        fmt.Println("i:", *b)
    */
}
func main() {
    usage()

}
1
  • Do not post screencasts of text; rather, copy and paste text itself. Commented Feb 21, 2019 at 2:45

3 Answers 3

1

It is because you're still holding the default value before flag.Parse() is called:

// still holding the "configfile" as value
cmdSt.configPtr = *flag.String("c", "configfile", "configure file to parse ")
// still holding the "interface" as value
cmdSt.interfacePtr = *flag.String("i", "interface", "capture network interface")

// flag is parsed now, but both cmdSt.configPtr and cmdSt.interfacePtr still holding the default value because of the pointer.
flag.Parse()

You can solve the problem by using temporary variables:

// hold the value to temporary variables
a := flag.String("c", "configfile", "configure file to parse ")
b := flag.String("i", "interface", "capture network interface")

// parse the flag and save to the variables.
flag.Parse()

// now, point the value to the CmdSt struct
cmdSt.configPtr = *a
cmdSt.interfacePtr = *b


fmt.Println("c", cmdSt.configPtr)
fmt.Println("i", cmdSt.interfacePtr)
Sign up to request clarification or add additional context in comments.

Comments

1

As an alternative to using temporary variables, You can populate your already initialized struct with the flag.StringVar() like below:

flag.StringVar(&cmdSt.configPtr, "c", "configfile", "configure file to parse ")
flag.StringVar(&cmdSt.interfacePtr, "i", "interface", "capture network interface")

Then you can call the value immediately.

https://play.golang.org/p/RkW7YKfE8t8

Comments

1

The application dereferences the pointer returned from flag.String after the default is set and before the command line is parsed. The cmdSt fields are set to the default values as a result.

Fix by using the flag.xxxVar() functions. These functions store flag values into values allocated by the application.

flag.StringVar(&cmdSt.configPtr, "c", "configfile", "configure file to parse ")
flag.StringVar(&cmdSt.interfacePtr, "i", "interface", "capture network interface")
flag.Parse()

// cmdSt.configPtr and cmdSt.interfacePtr are now set to
// command flag value or default if the flag was 
// not specified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.