Come si analizza correttamente un elenco di parametri del programma e automatizza la gestione "--help" e/o "--version" (ad esempio "program [-d value] [--abc] [FILE1]
") in Go?comportamento getopt-like in Go
risposta
ho fatta solo per voi:
package main
import (
"fmt";
"os"
)
func main() {
for i, arg := range os.Args {
if arg == "-help" {
fmt.Printf ("I need somebody\n")
}else if arg == "-version" {
fmt.Printf ("Version Zero\n")
} else {
fmt.Printf("arg %d: %s\n", i, os.Args[i])
}
}
}
vedi anche https://play.golang.org/p/XtNXG-DhLI
prova:
$ ./8.out -help -version monkey business I need somebody Version Zero arg 3: monkey arg 4: business
Utilizzare il pacchetto "flag": http://golang.org/pkg/flag/. Tuttavia, non fa argomenti double-dash. Non c'è nulla che esattamente imita il comportamento getopt GNU (ancora).
Da la sezione "UI della riga di comando", hai diverse librerie in grado di analizzare getopt-long parameters.
Ho provato, con una Go1.0.2:
- il code.google.com/p/opts-go (Vedere its documentation), che non è del tutto operativa.
- il github.com/droundy/goopt (Vedere its documentation), che ha un complete set of examples.
Esempio:
package main
import (
"fmt"
goopt "github.com/droundy/goopt"
)
func main() {
fmt.Println("flag")
goopt.NoArg([]string{"--abc"}, "abc param, no value", noabc)
goopt.Description = func() string {
return "Example program for using the goopt flag library."
}
goopt.Version = "1.0"
goopt.Summary = "goopt demonstration program"
goopt.Parse(nil)
}
func noabc() error {
fmt.Println("You should have an --abc parameter")
return nil
}
Altri parametri predefiniti forniti con goopt
:
--help Display the generated help message (calls Help())
--create-manpage Display a manpage generated by the goopt library (uses Author, Suite, etc)
--list-options List all known flags
go-flags è molto completa, licenza BSD, e ha un chiaro example.
var opts struct {
DSomething string `short:"d" description:"Whatever this is" required:"true"`
ABC bool `long:"abc" description:"Something"`
}
fileArgs, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
go-flags è abbastanza completo e ha anche la funzionalità di sottocomando che voglio. Ma il documento è difficile da capire. Suggerisco di leggere il codice di esempio + facendo riferimento al suo documento sarebbe il migliore. (Il link di esempio che hai indicato è molto utile.) –
Penso che quello che vuoi sia docopt. Ti indicherò semplicemente lo to an earlier answer che ho postato per i dettagli.
Google ha creato un pacchetto getopt (import "github.com/pborman/getopt"
) che fornisce l'analisi della riga di comando più standard (rispetto al pacchetto "flag").
package main
import (
"fmt"
"os"
"github.com/pborman/getopt"
)
func main() {
optName := getopt.StringLong("name", 'n', "", "Your name")
optHelp := getopt.BoolLong("help", 0, "Help")
getopt.Parse()
if *optHelp {
getopt.Usage()
os.Exit(0)
}
fmt.Println("Hello " + *optName + "!")
}
$ ./hello --help
Usage: hello [--help] [-n value] [parameters ...]
--help Help
-n, --name=value Your name
$ ./hello --name Bob
Hello Bob!
Oops, ho sbagliato. Il pacchetto è stato scritto da Googler e Google è titolare del copyright. – yegle
Un'altra opzione è Kingping che fornisce il supporto per tutte le chicche standard che vi aspettate di trovare da un moderno riga di comando parsing biblioteca. Dispone di --help
in più formati, sottocomandi, requisiti, tipi, impostazioni predefinite, ecc. È ancora in fase di sviluppo. Sembra che gli altri suggerimenti qui non siano stati aggiornati da un po 'di tempo.
package main
import (
"os"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("chat", "A command-line chat application.")
debug = app.Flag("debug", "Enable debug mode.").Bool()
serverIP = app.Flag("server", "Server address.").Default("127.0.0.1").IP()
register = app.Command("register", "Register a new user.")
registerNick = register.Arg("nick", "Nickname for user.").Required().String()
registerName = register.Arg("name", "Name of user.").Required().String()
post = app.Command("post", "Post a message to a channel.")
postImage = post.Flag("image", "Image to post.").File()
postChannel = post.Arg("channel", "Channel to post to.").Required().String()
postText = post.Arg("text", "Text to post.").Strings()
)
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
// Register user
case register.FullCommand():
println(*registerNick)
// Post message
case post.FullCommand():
if *postImage != nil {
}
text := strings.Join(*postText, " ")
println("Post:", text)
}
}
E il --help
uscita:
$ chat --help
usage: chat [<flags>] <command> [<flags>] [<args> ...]
A command-line chat application.
Flags:
--help Show help.
--debug Enable debug mode.
--server=127.0.0.1 Server address.
Commands:
help [<command>]
Show help for a command.
register <nick> <name>
Register a new user.
post [<flags>] <channel> [<text>]
Post a message to a channel.
ora che qualcuno ha scritto uno. E 'stato fuori per un giorno intero ora. –
@Kinopiko: dai un'occhiata a go-mode.el nella directory misc/emacs del repository del codice sorgente. – richq
Ecco un collegamento a un esempio che utilizza i flag della riga di comando: http: // golang.org/doc/go_tutorial.html # tmp_53 (L'esempio "Echo" sul tutorial Go) –