xmain: Return usage errors automatically from opts
This commit is contained in:
parent
8e76cbd3c8
commit
f8418f3a2c
2 changed files with 7 additions and 7 deletions
|
|
@ -37,26 +37,26 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
// These should be kept up-to-date with the d2 man page
|
// These should be kept up-to-date with the d2 man page
|
||||||
watchFlag, err := ms.Opts.Bool("D2_WATCH", "watch", "w", false, "watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.\n(default localhost:0, which is will open on a randomly available local port).")
|
watchFlag, err := ms.Opts.Bool("D2_WATCH", "watch", "w", false, "watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.\n(default localhost:0, which is will open on a randomly available local port).")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
hostFlag := ms.Opts.String("HOST", "host", "h", "localhost", "host listening address when used with watch")
|
hostFlag := ms.Opts.String("HOST", "host", "h", "localhost", "host listening address when used with watch")
|
||||||
portFlag := ms.Opts.String("PORT", "port", "p", "0", "port listening address when used with watch")
|
portFlag := ms.Opts.String("PORT", "port", "p", "0", "port listening address when used with watch")
|
||||||
bundleFlag, err := ms.Opts.Bool("D2_BUNDLE", "bundle", "b", true, "when outputting SVG, bundle all assets and layers into the output file.")
|
bundleFlag, err := ms.Opts.Bool("D2_BUNDLE", "bundle", "b", true, "when outputting SVG, bundle all assets and layers into the output file.")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
debugFlag, err := ms.Opts.Bool("DEBUG", "debug", "d", false, "print debug logs.")
|
debugFlag, err := ms.Opts.Bool("DEBUG", "debug", "d", false, "print debug logs.")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
layoutFlag := ms.Opts.String("D2_LAYOUT", "layout", "l", "dagre", `the layout engine used.`)
|
layoutFlag := ms.Opts.String("D2_LAYOUT", "layout", "l", "dagre", `the layout engine used.`)
|
||||||
themeFlag, err := ms.Opts.Int64("D2_THEME", "theme", "t", 0, "the diagram theme ID. For a list of available options, see https://oss.terrastruct.com/d2")
|
themeFlag, err := ms.Opts.Int64("D2_THEME", "theme", "t", 0, "the diagram theme ID. For a list of available options, see https://oss.terrastruct.com/d2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
versionFlag, err := ms.Opts.Bool("", "version", "v", false, "get the version")
|
versionFlag, err := ms.Opts.Bool("", "version", "v", false, "get the version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xmain.UsageErrorf(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ms.Opts.Flags.Parse(ms.Opts.Args)
|
err = ms.Opts.Flags.Parse(ms.Opts.Args)
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ func (o *Opts) Int64(envKey, flag, shortFlag string, defaultVal int64, usage str
|
||||||
if env := o.getEnv(flag, envKey); env != "" {
|
if env := o.getEnv(flag, envKey); env != "" {
|
||||||
envVal, err := strconv.ParseInt(env, 10, 64)
|
envVal, err := strconv.ParseInt(env, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(`invalid environment variable %s. Expected int64. Found "%v".`, envKey, envVal)
|
return nil, UsageErrorf(`invalid environment variable %s. Expected int64. Found "%v".`, envKey, envVal)
|
||||||
}
|
}
|
||||||
defaultVal = envVal
|
defaultVal = envVal
|
||||||
}
|
}
|
||||||
|
|
@ -148,7 +148,7 @@ func (o *Opts) String(envKey, flag, shortFlag string, defaultVal, usage string)
|
||||||
func (o *Opts) Bool(envKey, flag, shortFlag string, defaultVal bool, usage string) (*bool, error) {
|
func (o *Opts) Bool(envKey, flag, shortFlag string, defaultVal bool, usage string) (*bool, error) {
|
||||||
if env := o.getEnv(flag, envKey); env != "" {
|
if env := o.getEnv(flag, envKey); env != "" {
|
||||||
if !boolyEnv(env) {
|
if !boolyEnv(env) {
|
||||||
return nil, fmt.Errorf(`invalid environment variable %s. Expected bool. Found "%s".`, envKey, env)
|
return nil, UsageErrorf(`invalid environment variable %s. Expected bool. Found "%s".`, envKey, env)
|
||||||
}
|
}
|
||||||
if truthyEnv(env) {
|
if truthyEnv(env) {
|
||||||
defaultVal = true
|
defaultVal = true
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue