func parseFlags() cli {
	var cli cli
	flag.BoolVar(&cli.keygen, "k", false, "Create a new key pair")
	flag.IntVar(&cli.bitSize, "s", 1024, "Bitsize of the generated key pair")
	flag.BoolVar(&cli.encrypt, "e", false, "Encrypt from stdin")
	flag.BoolVar(&cli.decrypt, "d", false, "Decrypt from stdin")
	flag.StringVar(&cli.publicKeyName, "p", "", "Public key file name for encryption")
	flag.Parse()
	return cli
}
parseFlags defines all relevant flags and their default values.
package main
import (
	"flag"
	"io/ioutil"
	"os"
)
const publicKeyName = "public.key"
const privateKeyName = "private.key"
type cli struct {
	keygen        bool
	bitSize       int
	encrypt       bool
	decrypt       bool
	publicKeyName string
}
func main() {
	cli := parseFlags()
	switch {
	case cli.keygen:
		generateKeys(cli.bitSize)
	case cli.encrypt:
		startEncryption(cli.publicKeyName)
	case cli.decrypt:
		startDecryption()
	default:
		flag.Usage()
	}
}
func generateKeys(bitSize int) {
	pub, priv := GenerateKey(bitSize)
	pubFile, err := os.Create(publicKeyName)
	if err != nil {
		panic(err)
	}
	pubFile.WriteString(publicKeyPEM(&pub))
	pubFile.Close()
	privFile, err := os.Create(privateKeyName)
	if err != nil {
		panic(err)
	}
	privFile.WriteString(privateKeyPEM(&priv))
	privFile.Close()
}
func startEncryption(publicKeyName string) {
	if publicKeyName == "" {
		flag.Usage()
		return
	}
	pemPublicKey, err := ioutil.ReadFile(publicKeyName)
	if err != nil {
		panic(err)
	}
	publicKey := readPublicKey(pemPublicKey)
	bs, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}
	i := Encrypt(bs, publicKey)
	os.Stdout.Write(i)
}
func startDecryption() {
	pemPrivateKey, err := ioutil.ReadFile(privateKeyName)
	if err != nil {
		panic(err)
	}
	privateKey := readPrivateKey(pemPrivateKey)
	bs, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}
	i := Decrypt(bs, privateKey)
	os.Stdout.Write(i)
}