本文整理匯總了Golang中github.com/ogier/pflag.NArg函數的典型用法代碼示例。如果您正苦於以下問題:Golang NArg函數的具體用法?Golang NArg怎麽用?Golang NArg使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NArg函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
flag.Parse()
switch {
case *multiple != "":
// ugly things about to happen...
if flag.NArg() > 0 { // means it's using -a
fmt.Print(performBasename(*multiple, *suffix, *zero)) // pick first from -a
for _, v := range flag.Args() { // the rest...
fmt.Print(performBasename(v, *suffix, *zero))
}
} else { // means it's using --multiple, split em all
for _, v := range strings.Split(*multiple, " ") {
fmt.Print(performBasename(v, *suffix, *zero))
}
}
case *suffix != "": //implies -a
fmt.Print(performBasename(flag.Args()[0], *suffix, *zero))
default:
name := flag.Args()[0]
suffix := ""
if flag.NArg() == 2 {
suffix = flag.Args()[1]
}
fmt.Print(performBasename(name, suffix, *zero))
}
}
示例2: main
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s", Help)
os.Exit(1)
}
flag.Parse()
if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
if flag.NArg() > 1 {
fatal.Fatalf("extra operand %s", flag.Arg(1))
}
var (
file *os.File
err error
)
if flag.Arg(0) == "-" || flag.NArg() == 0 {
file = os.Stdin
} else {
file, err = os.Open(flag.Arg(0))
if err != nil {
fatal.Fatalln(err)
}
defer file.Close()
}
ok := tsort(file)
os.Exit(ok)
}
示例3: main
func main() {
flag.Usage = func() {
// This is a little weird because I want to insert the correct
// UTMP/WTMP file names into the Help output, but usually my
// Help constants are raw string literals, so I had to
// break it up into a couple chunks and move around some formatting.
fmt.Fprintf(os.Stderr, "%s %s. %s %s",
Help1, utmp.UtmpFile, utmp.WtmpFile, Help2)
os.Exit(1)
}
flag.Parse()
if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
switch flag.NArg() {
case 0:
uptime(utmp.UtmpFile, utmp.CheckPIDs)
case 1:
uptime(flag.Arg(0), 0)
default:
fatal.Fatalf("extra operand %s\n", flag.Arg(1))
}
}
示例4: main
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s", Help)
os.Exit(1)
}
flag.Parse()
if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
args := flag.Args()
if flag.NArg() == 0 {
args = []string{"y"}
}
for {
for i, arg := range args {
os.Stdout.WriteString(arg)
if i == len(args)-1 {
os.Stdout.Write(LineFeed)
} else {
os.Stdout.Write(Space)
}
}
}
}
示例5: main
func main() {
dir, _ := os.Getwd()
port := flag.IntP("port", "p", 8080, "help message for flagname")
flag.Parse()
if flag.NArg() > 1 {
flag.Usage()
os.Exit(1)
} else if flag.NArg() == 1 {
dir = flag.Args()[0]
}
bindTo := fmt.Sprintf(":%d", *port)
fmt.Printf("Serving '%s' on %s...\n", dir, bindTo)
panic(http.ListenAndServe(bindTo, http.FileServer(http.Dir(dir))))
}
示例6: main
func main() {
NAME = path.Base(os.Args[0])
flags := make(map[string]*bool)
flag.Usage = usageLong
flags["version"] = flag.BoolP("version", "v", false,
"Print version number and exit")
flags["help"] = flag.BoolP("help", "h", false,
"Print this help message and exit")
flags["index"] = flag.BoolP("index", "i", false,
"Create indexes for the lexicon")
flag.Parse()
// Handle -v/--version
if *flags["version"] {
fmt.Println(Version)
os.Exit(0)
}
// Handle -h/--help
if *flags["help"] {
if strings.Contains(strings.Join(os.Args, " "), "--help") {
usageLong()
} else {
usageShort()
}
os.Exit(0)
}
// Handle -i/--index
if *flags["index"] {
fmt.Println("Creating indexes")
if err := index.Create(); err != nil {
fmt.Fprintf(os.Stderr, "Error creating indexes: %v", err)
os.Exit(1)
} else {
fmt.Println("Done")
os.Exit(0)
}
}
// Handle missing <text> argument
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "%s: missing <text> argument\n", NAME)
usageLong()
os.Exit(1)
}
fmt.Printf("Searching for %q\n", flag.Arg(0))
}
示例7: main
func main() {
pflag.Usage = func() {
fmt.Fprintf(os.Stderr, USAGE)
}
var output = pflag.StringP("output", "o", ".", "output directory")
pflag.Parse()
if pflag.NArg() == 1 {
genRoutes(pflag.Arg(0), *output)
} else {
pflag.Usage()
}
}
示例8: main
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s", Help)
os.Exit(1)
}
flag.Parse()
if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
if flag.NArg() > 1 {
fatal.Fatalf("extra operand %s", flag.Arg(1))
}
file := os.Stdin
if (flag.Arg(0) != "-" &&
flag.Arg(0) != "") ||
flag.NArg() != 0 {
var err error
file, err = os.Open(flag.Arg(0))
if err != nil {
fatal.Fatalln(err)
}
defer file.Close()
}
os.Exit(tsort(struct {
io.Reader
io.Writer
}{
file,
os.Stdout,
}))
}
示例9: setup
// Initial setup when the program starts.
func setup() {
// ensure that zpool/zfs commands do not use localized messages:
os.Setenv("LC_ALL", "C")
// command line flags:
pflag.StringVarP(&cfgFile, "conf", "c", CFGFILE, "configuration file path")
pflag.BoolVarP(&optDebug, "debug", "d", false, "print debug information to stdout")
optHashPassword := pflag.BoolP("passwordhash", "P", false, "hash web password")
optTest := pflag.BoolP("test", "t", false, "test configuration and exit")
optVersion := pflag.BoolP("version", "v", false, "print version information and exit")
pflag.Parse()
if pflag.NArg() > 0 {
pflag.Usage()
os.Exit(2)
}
if *optVersion {
version()
os.Exit(0)
}
if *optHashPassword {
wwwHashPassword()
os.Exit(0)
}
// initialize logging & notification:
if *optTest {
optDebug = true
}
cfg = getCfg()
if cfg == nil {
os.Exit(2)
}
notify = setupLog(cfg)
if *optTest {
notifyCloseC := notify.Close()
select { // wait max 1 second for loggers to finish
case <-notifyCloseC:
case <-time.After(time.Second):
}
os.Exit(0)
}
}
示例10: main
func main() {
pflag.Usage = func() {
fmt.Fprintf(os.Stderr, USAGE)
}
var output = pflag.StringP("output", "o", ".", "output directory")
var version = pflag.BoolP("version", "", false, "version")
pflag.Parse()
if *version {
fmt.Println(VERSION)
} else if pflag.NArg() == 1 {
genRoutes(pflag.Arg(0), *output)
} else {
pflag.Usage()
}
}
示例11: main
func main() {
var cluster *string = flag.String("cluster", "default", "Name of cluster")
flag.Parse()
if flag.NArg() < 2 {
fmt.Println("Usage:\n\taws-rollout [service] [image]")
return
}
var service string = flag.Arg(0)
var image string = flag.Arg(1)
svc := ecs.New(session.New())
clusterArn, err := findClusterArn(svc, *cluster)
if err != nil {
fmt.Println(err.Error())
return
}
serviceArn, err := findServiceArn(svc, clusterArn, service)
if err != nil {
fmt.Println(err.Error())
return
}
taskArn, err := findTaskArn(svc, clusterArn, serviceArn)
newTaskArn, err := setImage(svc, taskArn, image)
params := &ecs.UpdateServiceInput{
Service: aws.String(serviceArn),
Cluster: aws.String(clusterArn),
TaskDefinition: aws.String(newTaskArn),
}
serv, err := svc.UpdateService(params)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("Deployed %s %d", newTaskArn, *serv.Service.PendingCount)
}
示例12: init
func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: chanloader [options] /b/res/123456\nOptions:")
flag.PrintDefaults()
}
flag.Parse()
if *refresh < time.Second*30 {
*refresh = time.Second * 30
}
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
runtime.GOMAXPROCS(runtime.NumCPU())
}
示例13: main
func main() {
var (
// general options
stateDir = pflag.String("statedir", "", "the server state directory")
help = pflag.BoolP("help", "h", false, "show this help")
// server options
server = pflag.BoolP("server", "s", false, "run the server in the foreground")
port = pflag.IntP("port", "p", 40000, "server port to listen on")
// client options
method = pflag.StringP("method", "X", "GET", "client method")
plugin = pflag.String("plugin", "", "client plugin")
data = pflag.StringP("data", "d", "", "client body")
headers = &repString{[]string{}}
verbose = pflag.BoolP("verbose", "v", false, "show full http response")
)
pflag.VarP(headers, "header", "H", "client request header")
pflag.Parse()
if *help {
os.Exit(runHelp())
}
if *server {
os.Exit(runServer(*port, *stateDir))
}
if pflag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "must pass in path to make api call")
runHelp()
os.Exit(1)
}
os.Exit(runClient(*stateDir, *plugin, *method, pflag.Arg(0), *data, headers.strs, *verbose))
}
示例14: main
func main() {
var ok int // return status
outHandle := syscall.Handle(os.Stdout.Fd())
outType, err := syscall.GetFileType(outHandle)
if err != nil {
fatal.Fatalln(err)
}
outBsize := 4096
// catch (./cat) < /etc/group
var args []string
if flag.NArg() == 0 {
args = []string{"-"}
} else {
args = flag.Args()
}
// the main loop
var file *os.File
for _, arg := range args {
var inStat os.FileInfo
if arg == "-" {
file = os.Stdin
} else {
file, err = os.Open(arg)
if err != nil {
fatal.Fatalln(err)
}
inStat, err = file.Stat()
if err != nil {
fatal.Fatalln(err)
}
if inStat.IsDir() {
fatal.Printf("%s: Is a directory\n", file.Name())
}
}
inHandle := syscall.Handle(file.Fd())
inBsize := 4096
// See http://stackoverflow.com/q/29360969/2967113
// for why this differs from the Unix versions.
//
// Make sure we're not catting a file to itself,
// provided it's a regular file. Catting a non-reg
// file to itself is cool, e.g. cat file > file
if outType == syscall.FILE_TYPE_DISK {
inPath := make([]byte, syscall.MAX_PATH)
outPath := make([]byte, syscall.MAX_PATH)
err = k32.GetFinalPathNameByHandleA(inHandle, inPath, 0)
if err != nil {
fatal.Fatalln(err)
}
err = k32.GetFinalPathNameByHandleA(outHandle, outPath, 0)
if err != nil {
fatal.Fatalln(err)
}
if string(inPath) == string(outPath) {
if n, _ := file.Seek(0, os.SEEK_CUR); n < inStat.Size() {
fatal.Fatalf("%s: input file is output file\n", file.Name())
}
}
}
if simple {
outBuf := bufio.NewWriterSize(os.Stdout, 4096)
ok ^= simpleCat(file, outBuf)
// Flush because we don't have a chance to in
// simpleCat() because we use io.Copy()
outBuf.Flush()
} else {
// If you want to know why, exactly, I chose
// outBsize -1 + inBsize*4 + 20, read GNU's cat
// source code. The tl;dr is the 20 is the counter
// buffer, inBsize*4 is from potentially prepending
// the control characters (M-^), and outBsize is
// due to new tests for newlines.
size := outBsize - 1 + inBsize*4 + 20
outBuf := bufio.NewWriterSize(os.Stdout, size)
inBuf := make([]byte, inBsize+1)
ok ^= cat(file, inBuf, outBuf)
}
file.Close()
}
os.Exit(ok)
}
示例15: main
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s", Help)
os.Exit(1)
}
flag.Parse()
if *constVersion {
fmt.Printf("Unicode Version: %s\n", unicode.Version)
os.Exit(0)
} else if *version {
fmt.Printf("%s\n", Version)
os.Exit(0)
}
if !(*printBytes || *printChars || *printLines || *printWords || *printLineLength) {
*printLines = true
*printBytes = true
*printWords = true
}
// This is a gross attempt to simulate this...
// (print_lines + print_words + print_chars +
// print_bytes + print_linelength) == 1
//
// Since Go can't add booleans (e.g. false + true == 1)
// and checking that *only* one of 5 bool variables would be sloppy,
// we check the number of set flags and the remaining non-'print' flags
// which is a much smaller set of conditions to check
//
// 1 flag and it's --files0-from
if (flag.NFlag() == 1 && *filesFrom == "" && *tabWidth == 8) ||
// 2 flags and one's *filesFrom OR *tabWidth
(flag.NFlag() == 2 && (*filesFrom != "" || *tabWidth != 8)) ||
// 3 flags and two are *filesFrom AND *tabWidth
(flag.NFlag() == 3 && *filesFrom != "" && *tabWidth != 8) {
printOne = true
}
var (
ok = 0 // Return status.
files = flag.Args() // List of files.
numFiles = len(files) // Number of files to wc.
reasonable = true // Can we read file list into memory?
stdin = true // Are we reading from stdin?
size int64
)
if *filesFrom != "" {
// Cannot specify files with --files0-from.
if flag.NArg() > 0 {
fatal.Fatalln("file operands cannot be combined with --files0-from")
}
// --files0-from is small enough to fit into RAM.
if reasonable, size = isReasonable(*filesFrom); reasonable {
files, numFiles = getFileList(*filesFrom, size)
stdin = false
}
}
fs := getFileStatus(numFiles, files)
numberWidth = findNumberWidth(numFiles, fs)
if !reasonable {
var (
file *os.File
err error
)
file = os.Stdin
if *filesFrom != "-" {
file, err = os.Open(*filesFrom)
}
if err != nil {
fatal.Fatalf("cannot open: %s\n", *filesFrom)
}
defer file.Close()
r := bufio.NewReader(file)
i := 0
for {
fname, err := r.ReadString(NullByte)
if err != nil {
if err != io.EOF && i < 1 {
fatal.Fatalln(err)
}
break
}
if len(fname) > 1 {
// Trim trailing null-byte.
if fname[len(fname)-1] == NullByte {
fname = fname[:len(fname)-1]
}
} else {
log.Fatalf("invalid zero-length file name at position: %d\n", i)
}
//.........這裏部分代碼省略.........