hyperledger/fabric · error

invalid number of args. expected only the output file

Error message

invalid number of args. expected only the output file

What it means

PackageChaincode enforces cobra-style positional argument validation: the package subcommand accepts exactly one positional argument, the output package file. Any extra or missing argument causes this error and usage is silenced so only the message is shown.

Source

Thrown at internal/peer/lifecycle/chaincode/package.go:111

		"path",
		"peerAddresses",
		"tlsRootCertFiles",
		"connectionProfile",
	}
	attachFlags(chaincodePackageCmd, flagList)

	return chaincodePackageCmd
}

// PackageChaincode packages a chaincode.
func (p *Packager) PackageChaincode(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 output file")
	}
	p.setInput(args[0])

	return p.Package()
}

func (p *Packager) setInput(outputFile string) {
	p.Input = &PackageInput{
		OutputFile: outputFile,
		Path:       chaincodePath,
		Type:       chaincodeLang,
		Label:      packageLabel,
	}
}

// Package packages chaincodes into the package type,
// (.tar.gz) used by _lifecycle and writes it to disk
func (p *Packager) Package() error {

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Use exactly: peer lifecycle chaincode package <output-file> --path <path> --lang <lang> --label <label>.
  2. Quote paths containing spaces: package "/tmp/my packages/basic.tar.gz" ...
  3. Check each flag spelling; an unaccepted flag's value becomes a stray positional argument.
  4. Print the actual command in scripts (set -x) to count positional args.

Example fix

// before (missing --lang flag, so 'golang' becomes a stray positional)
peer lifecycle chaincode package basic.tar.gz golang --path . --label basic_1.0
// after
peer lifecycle chaincode package basic.tar.gz --path . --lang golang --label basic_1.0
Defensive patterns

Strategy: validation

Validate before calling

#!/bin/bash
# Count positional args the way cobra will see them
args=("")
: "${1:?usage: package.sh <output.tar.gz>}"
peer lifecycle chaincode package "$1" \
  --path "$CC_SRC_PATH" \
  --lang "$CC_LANG" \
  --label "$LABEL"
# Exactly one positional argument: "$1"

Try / catch

if ! out=$(peer lifecycle chaincode package "$@" 2>&1); then
  case "$out" in
    *"invalid number of args"*) echo "Check flag spelling/quoting; package takes only the output file positionally"; exit 2 ;;
    *) echo "$out"; exit 1 ;;
  esac
fi

Prevention

When it happens

Trigger: `peer lifecycle chaincode package` called with 0 args, 2+ positional args (e.g. forgetting a --flag and having its value counted positionally: `package out.tar.gz golang --path ...` treats 'golang' as a second positional arg), or quoting mistakes splitting paths into multiple args.

Common situations: Dropped/misspelled flags (--lan instead of --lang) making extra positionals; spaces in unquoted output paths; scripts appending extra arguments.

Related errors


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