func parseArgs()

in tools/mount_gcsfuse/main.go [150:221]


func parseArgs(
	args []string) (
	device string,
	mountPoint string,
	opts map[string]string,
	err error) {
	opts = make(map[string]string)

	// Process each argument in turn.
	positionalCount := 0
	for i, s := range args {
		switch {
		// Skip the program name.
		case i == 0:
			continue

		// "-o" is illegal only when at the end. We handle its argument in the case
		// below.
		case s == "-o":
			if i == len(args)-1 {
				err = fmt.Errorf("unexpected -o at end of args")
				return
			}

		// systemd passes -n (alias --no-mtab) to the mount helper. This seems to
		// be a result of the new setup on many Linux systems with /etc/mtab as a
		// symlink pointing to /proc/self/mounts. /proc/self/mounts is read-only,
		// so any helper that would normally write to /etc/mtab should be
		// configured not to do so. Because systemd does not provide a way to
		// disable this behavior for mount helpers that do not write to /etc/mtab,
		// we ignore the flag.
		case s == "-n":
			continue

		// Is this an options string following a "-o"?
		case i > 0 && args[i-1] == "-o":
			mount.ParseOptions(opts, s)

		// Is this the device?
		case positionalCount == 0:
			device = s
			// The kernel might be converting the bucket name to a path if a directory with the
			// same name as the bucket exists in the folder where the mount command is executed.
			//
			// As of October 10th, 2024, bucket names don't support "/", so it is safe to
			// assume any received bucket name containing "/" is actually a path and
			// extract the base file name.
			// Ref: https://cloud.google.com/storage/docs/buckets#naming
			if strings.Contains(device, "/") {
				// Get the last part of the path (bucket name)
				device = filepath.Base(device)
			}
			positionalCount++

		// Is this the mount point?
		case positionalCount == 1:
			mountPoint = s
			positionalCount++

		default:
			err = fmt.Errorf("unexpected arg %d: %q", i, s)
			return
		}
	}

	if positionalCount != 2 {
		err = fmt.Errorf("expected two positional arguments; got %d", positionalCount)
		return
	}

	return
}