2023-07-28 21:39:23 +03:00
|
|
|
// Copyright 2023 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-07-25 07:50:49 +04:00
|
|
|
|
2023-07-28 22:05:22 +03:00
|
|
|
// Hello is a hello, world program, demonstrating
|
|
|
|
// how to write a simple command-line program.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// hello [options] [name]
|
|
|
|
//
|
|
|
|
// The options are:
|
|
|
|
//
|
|
|
|
// -g greeting
|
|
|
|
// Greet with the given greeting, instead of "Hello".
|
|
|
|
//
|
|
|
|
// -r
|
|
|
|
// Greet in reverse.
|
|
|
|
//
|
|
|
|
// By default, hello greets the world.
|
|
|
|
// If a name is specified, hello greets that name instead.
|
2014-07-25 07:50:49 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-28 22:05:22 +03:00
|
|
|
"flag"
|
2014-07-25 07:50:49 +04:00
|
|
|
"fmt"
|
2023-07-28 22:05:22 +03:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-07-25 07:50:49 +04:00
|
|
|
|
2023-07-28 22:05:22 +03:00
|
|
|
"golang.org/x/example/hello/reverse"
|
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "usage: hello [options] [name]\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
greeting = flag.String("g", "Hello", "Greet with `greeting`")
|
|
|
|
reverseFlag = flag.Bool("r", false, "Greet in reverse")
|
2014-07-25 07:50:49 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-07-28 22:05:22 +03:00
|
|
|
// Configure logging for a command-line program.
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetPrefix("hello: ")
|
|
|
|
|
|
|
|
// Parse flags.
|
|
|
|
flag.Usage = usage
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Parse and validate arguments.
|
|
|
|
name := "world"
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) >= 2 {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
if len(args) >= 1 {
|
|
|
|
name = args[0]
|
|
|
|
}
|
|
|
|
if name == "" { // hello '' is an error
|
|
|
|
log.Fatalf("invalid name %q", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual logic.
|
|
|
|
if *reverseFlag {
|
|
|
|
fmt.Printf("%s, %s!\n", reverse.String(*greeting), reverse.String(name))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("%s, %s!\n", *greeting, name)
|
2014-07-25 07:50:49 +04:00
|
|
|
}
|