이 름 : 바다아이
|
조회수 : 8907
짧은 주소 : https://www.bada-ie.com/su/?121591785919
// [_Command-line flags_](http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option)// are a common way to specify options for command-line// programs. For example, in `wc -l` the `-l` is a// command-line flag.
package main
// Go provides a `flag` package supporting basic// command-line flag parsing. We'll use this package to// implement our example command-line program.import"flag"import"fmt"
func main() {
// Basic flag declarations are available for string,// integer, and boolean options. Here we declare a// string flag `word` with a default value `"foo"`// and a short description. This `flag.String` function// returns a string pointer (not a string value);// we'll see how to use this pointer below.
wordPtr := flag.String("word", "foo", "a string")
// This declares `numb` and `fork` flags, using a// similar approach to the `word` flag.
numbPtr := flag.Int("numb", 42, "an int")
boolPtr := flag.Bool("fork", false, "a bool")
// It's also possible to declare an option that uses an// existing var declared elsewhere in the program.// Note that we need to pass in a pointer to the flag// declaration function.
var svar string
flag.StringVar(&svar, "svar", "bar", "a string var")
// Once all flags are declared, call `flag.Parse()`// to execute the command-line parsing.
flag.Parse()
// Here we'll just dump out the parsed options and// any trailing positional arguments. Note that we// need to dereference the pointers with e.g. `*wordPtr`// to get the actual option values.
fmt.Println("word:", *wordPtr)
fmt.Println("numb:", *numbPtr)
fmt.Println("fork:", *boolPtr)
fmt.Println("svar:", svar)
fmt.Println("tail:", flag.Args())
}
결과
go build text.go
root@mr:# ./text -word=opt -numb=7 -fork -svar=flagword: opt
numb: 7
fork: true
svar: flag
tail: []
root@mr:# ./text -word=opt
word: opt
numb: 42
fork: false
svar: bar
tail: []
root@mr:# ./text -word=opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3]
root@mr:# ./text -word=opt a1 a2 a3 -numb=7word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3 -numb=7]
root@mr:# ./text -h
Usage of ./text:
-fork
a bool
-numb int
an int (default42)
-svar string
a string var (default"bar")
-wordstring
a string (default"foo")
root@mr:# ./text -wat
flag provided but not defined: -wat
Usage of ./text:
-fork
a bool
-numb int
an int (default42)
-svar string
a string var (default"bar")
-wordstring
a string (default"foo")
출처 : https://gobyexample.com/command-line-flags
|
|
번 호
카테고리
제 목
이름
조회수
Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천 실행시간 : 0.05752 초 to webmaster... gogo sea. gogo sea.