larksuite/cli · error

default

Error message

default: %w

What it means

validateInputCLI rejects a shortcut input field whose declared default value is not assignable to the field's Go value type. When a field declares a default (defaultValue.Set), valueAssignableTo checks the default against field.valueType; any mismatch (wrong kind, wrong scalar type, invalid JSON for complex types) is wrapped as "default: <underlying reason>" so the root cause is preserved. This is a registration-time schema validation error, thrown before any CLI parsing happens.

Solutions

  1. Change the default value's Go type so it matches field.valueType (e.g. use 8080 for an int field, "8080" for a string field)
  2. If the default is intentional for a differently-typed field, convert it explicitly (strconv.Itoa/ParseInt) at declaration time
  3. Remove the default if the field must always be supplied explicitly

Example fix

// before
Value: 42 // default on a field of type string
// after
Value: "42" // matches the field's string type
Defensive patterns

Strategy: validation

Validate before calling

func checkDefaultType(fieldType reflect.Type, def any) error {
	if def == nil {
		return nil
	}
	return valueAssignableTo(def, fieldType)
}
// call before registering the InputField

Try / catch

if err := compileInput(...); err != nil {
	if strings.HasPrefix(err.Error(), "default: ") {
		// fix the default value's type before retrying
	}
	return err
}

Prevention

When it happens

Trigger: Declaring an InputField with a defaultValue whose Go type does not match the field's valueType, e.g. a string default on an int field, an int default on a string field, or a value that cannot unmarshal into a slice/struct type. Raised by compileInput during shortcut compilation and exercised by TestParseCLITagRejectsInvalidGrammar.

Common situations: Refactoring a field's Go type (int -> string) without updating the default; copying a default from a sibling field with a different type; using JSON text as a default for a complex field that expects assignment, not a JSON string.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/56a70e297adafb8c. Report an issue: GitHub.

Appendix: source

Thrown at shortcuts/common/typed_compile_args.go:278

	if supplement.CLI.Deprecated != "" {
		if field.cli.Deprecated != "" {
			return fmt.Errorf("CLI.Deprecated is declared twice")
		}
		field.cli.Deprecated = supplement.CLI.Deprecated
	}
	return nil
}

func validateInputCLI(field *compiledInputField) error {
	if field.cli.Deprecated != "" && !field.cli.Hidden {
		return fmt.Errorf("deprecated primary flag must be hidden")
	}
	if field.required && field.defaultValue.Set {
		return fmt.Errorf("required input cannot declare a default")
	}
	if field.defaultValue.Set {
		if err := valueAssignableTo(field.defaultValue.Value, field.valueType); err != nil {
			return fmt.Errorf("default: %w", err)
		}
	}
	seenSources := make(map[typedValueSource]struct{})
	for _, source := range field.cli.ValueSources {
		if source != typedSourceFlag && source != typedSourceFile && source != typedSourceStdin {
			return fmt.Errorf("unknown value source %q", source)
		}
		if _, duplicate := seenSources[source]; duplicate {
			return fmt.Errorf("duplicate value source %q", source)
		}
		seenSources[source] = struct{}{}
	}
	if len(field.cli.ValueSources) > 0 {
		if _, ok := seenSources[typedSourceFlag]; !ok {
			return fmt.Errorf("ValueSources must include flag")
		}
		if (len(seenSources) > 1) && indirectKind(field.valueType) != reflect.String && field.cli.Encoding != typedEncodingJSON {
			return fmt.Errorf("file/stdin sources require string input or encoding=json")

View on GitHub (pinned to 7fd6ef3c07)