hyperledger/fabric · error

invalid number of args. expected only the packaged chaincode

Error message

invalid number of args. expected only the packaged chaincode file

What it means

The calculatepackageid cobra command takes exactly one positional argument — the packaged chaincode file. CalculatePackageID rejects any other argument count with this error, before attempting to parse or hash the package. It guards against users passing flags-as-args or multiple files.

Source

Thrown at internal/peer/lifecycle/chaincode/calculatepackageid.go:90

		"peerAddresses",
		"tlsRootCertFiles",
		"connectionProfile",
		"output",
	}
	attachFlags(calculatePackageIDCmd, flagList)

	return calculatePackageIDCmd
}

// CalculatePackageID calculates the package ID for a packaged chaincode.
func (p *PackageIDCalculator) CalculatePackageID(args []string) error {
	if p.Command != nil {
		// Parsing of the command line is done so silence cmd usage
		p.Command.SilenceUsage = true
	}

	if len(args) != 1 {
		return errors.New("invalid number of args. expected only the packaged chaincode file")
	}
	p.setInput(args[0])

	return p.PackageID()
}

// PackageID calculates the package ID for a packaged chaincode and print it.
func (p *PackageIDCalculator) PackageID() error {
	err := p.Input.Validate()
	if err != nil {
		return err
	}
	pkgBytes, err := p.Reader.ReadFile(p.Input.PackageFile)
	if err != nil {
		return errors.WithMessagef(err, "failed to read chaincode package at '%s'", p.Input.PackageFile)
	}

	metadata, _, err := persistence.ParseChaincodePackage(pkgBytes)

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Pass exactly one argument: `peer lifecycle chaincode calculatepackageid ./mycc_1.0.tgz`.
  2. Quote or brace-expand globs so only the intended single .tgz file is passed.
  3. If a wrapper script still uses a flag, update it to pass the package file as the single positional argument.

Example fix

// before: flag-style or multiple args
peer lifecycle chaincode calculatepackageid --package-file a.tgz b.tgz
// after: exactly one positional arg
peer lifecycle chaincode calculatepackageid ./mycc_1.0.tgz
Defensive patterns

Strategy: validation

Validate before calling

args := []string{"./mycc_1.0.tgz"}
if len(args) != 1 {
    return errors.New("calculatepackageid takes exactly one positional arg: the packaged chaincode file")
}
if _, err := os.Stat(args[0]); err != nil {
    return fmt.Errorf("package file missing: %w", err)
}

Try / catch

if err := cmd.CalculatePackageID(args); err != nil && strings.Contains(err.Error(), "invalid number of args") {
    cmd.Usage()
    return nil // show usage instead of a raw error
}

Prevention

When it happens

Trigger: Running `peer lifecycle chaincode calculatepackageid` with zero positional args, with more than one path, or relying on an older flag-based usage (-p/--package-file) that no longer maps to the positional arg (the flag may only set defaults).

Common situations: Scripts written against an older fabric-cli syntax where the package file was passed via a flag; accidental extra filenames (e.g. both a .tgz and a .tar.gz) glob-expanded onto the command line.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/bb8ab84edb355d6c. Report an issue: GitHub.