make error messages nicer
This changes the error message so that not the program help is printed but instead the specific submenu help. This should result in much faster and better understanding of the problematic situation.
This commit is contained in:
parent
470d21409b
commit
a81c103572
16
flags.go
16
flags.go
|
@ -65,6 +65,7 @@ type (
|
|||
}
|
||||
|
||||
Flags struct {
|
||||
Name string // name of the sub function
|
||||
flagset *flag.FlagSet // the flagset reference for printing the help
|
||||
flag_container *paramContainer
|
||||
Flags *flagSet // the end result of the flag setting
|
||||
|
@ -78,8 +79,9 @@ type (
|
|||
// create a new flag handler with the name of the subfunction
|
||||
func NewFlags(method_name string) *Flags {
|
||||
return &Flags{
|
||||
Name: method_name,
|
||||
Flags: &flagSet{},
|
||||
flagset: flag.NewFlagSet(method_name, flag.ExitOnError),
|
||||
flagset: flag.NewFlagSet(method_name, flag.ContinueOnError),
|
||||
check_list: make([]flagCheck, 0),
|
||||
flag_container: ¶mContainer{},
|
||||
}
|
||||
|
@ -90,11 +92,21 @@ func (f *Flags) Parse(options []string) error {
|
|||
f.flagset.Parse(options)
|
||||
for _, check := range f.check_list {
|
||||
// TODO handle error in a betetr way (output specific help, not command help)
|
||||
if err := check(); err != nil { return err }
|
||||
if err := check(); err != nil {
|
||||
f.Usagef("%s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Flags) Usagef(message string, args ...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, "error: " + message + "\n", args...)
|
||||
fmt.Fprintf(os.Stderr, "usage: %s %s [options]\n", os.Args[0], f.Name)
|
||||
fmt.Fprint(os.Stderr, "where options are:\n")
|
||||
f.flagset.PrintDefaults()
|
||||
}
|
||||
|
||||
// add the private key option to the requested flags
|
||||
func (f *Flags) AddPrivateKey() {
|
||||
f.check_list = append(f.check_list, f.parsePrivateKey)
|
||||
|
|
14
main.go
14
main.go
|
@ -48,18 +48,18 @@ func create_private_key() {
|
|||
fs.AddOutput()
|
||||
fs.AddPrivateKeyGenerationFlags()
|
||||
err := fs.Parse(program_args())
|
||||
if err != nil { crash_with_help(1, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
|
||||
var pk pki.Pemmer
|
||||
switch fs.Flags.PrivateKeyGenerationFlags.Type {
|
||||
case "ecdsa": pk, err = pki.NewPrivateKeyEcdsa(fs.Flags.PrivateKeyGenerationFlags.Curve)
|
||||
case "rsa": pk, err = pki.NewPrivateKeyRsa(fs.Flags.PrivateKeyGenerationFlags.Size)
|
||||
}
|
||||
if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
marsh_pem, err := pk.MarshalPem()
|
||||
if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
_, err = marsh_pem.WriteTo(fs.Flags.Output)
|
||||
if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
}
|
||||
|
||||
// create a public key derived from a private key
|
||||
|
@ -68,14 +68,14 @@ func create_public_key() {
|
|||
fs.AddPrivateKey()
|
||||
fs.AddOutput()
|
||||
err := fs.Parse(program_args())
|
||||
if err != nil { crash_with_help(1, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
|
||||
var pub_key pki.Pemmer
|
||||
pub_key = fs.Flags.PrivateKey.Public()
|
||||
marsh_pem, err := pub_key.MarshalPem()
|
||||
if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
_, err = marsh_pem.WriteTo(fs.Flags.Output)
|
||||
if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
|
||||
if err != nil { os.Exit(2) }
|
||||
}
|
||||
|
||||
// print the module help
|
||||
|
|
Loading…
Reference in New Issue