We are currently implementing a simple curl command in the Go language.
Command-line options are intended only for -v, -x, -o, -d and are configured as follows:
//Configuring Options
var showHeader pool
flag.BoolVar(&showHeader, "v", false, "-v, -- verbose Make the operation more talkative")
var outputFile string
flag.StringVar(&outputFile, "o", "", "-o, --output<file>Write to file installed of stdout")
var postValues string
flag.StringVar(&postValues, "d", "", "-d, --data<data>HTTP POST data")
var requestType string
flag.StringVar(&requestType, "X", "GET", "-X, --request<command>Specify request command to use")
flag.Parse()
After that, after configuring the HTTP client, the request is skipped by branching as follows:
addr:=flag.Arg(0)
// Branch at GET or POST
if requestType == "GET" {
get(client,addr,showHeader,outputFile)
} else if requestType=="POST" {
post(client,addr,showHeader,values)
} else {
fmt.Printf("%s:requestType is not correct!\n", os.Args[0])
fmt.Printf("%s:try'kcurl --help'or'kcurl --manual' for more information\n", os.Args[0])
os.Exit(1)
}
However, in the program you created,
Only the format where the command line option precedes the command line argument (URL), such as gorunmain.go-v-otest.txt 'http://google.com'
, and the format where gorunmain.go'-otest.txt-v
recognizes the -v-otest.txt as a string and leaves the default value as the -o option value.
fmt.Println(flag.Args())
fmt.Println (showHeader, outputFile, postValues, requestType)
// [http://google.com-v-o test.txt]
// false GET
In this case, how should I set the flag?
I would appreciate it if you could let me know.
The flag package does not assume this way.
Flag parsing stops just before the first non-flag argument ("-"is an non-flag argument) or after the terminator" -- ".
As the document says, if a flag follows a non-flag argument, the flag package does not parse the argument as a flag.The flag package has chosen this kind of design.Learn more: https://github.com/golang/go/issues/4513
If you want to do this, for example, you will use a third-party package.Various things are known, so please choose according to your usage: https://awesome-go.com/ #command-line
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
582 PHP ssh2_scp_send fails to send files as intended
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.