MCPcopy Create free account
hub / github.com/cli/cli / parseArg

Function parseArg

internal/attachments/flags.go:115–133  ·  view source on GitHub ↗

parseArg splits the `path#alt text` form of an --attach argument. An existing path wins over the delimiter, since `#` is legal in filenames.

(arg string)

Source from the content-addressed store, hash-verified

113// parseArg splits the `path#alt text` form of an --attach argument. An existing
114// path wins over the delimiter, since `#` is legal in filenames.
115func parseArg(arg string) (path, alt string) {
116 if _, err := os.Stat(arg); err == nil {
117 return arg, ""
118 }
119 // Scan from the last hash to the first, so the longest path that exists
120 // wins. That continues the rule above, where the whole argument is the
121 // longest match of all, and it leaves a hash inside the alt text usable.
122 for i := strings.LastIndex(arg, "#"); i > 0; i = strings.LastIndex(arg[:i], "#") {
123 if _, err := os.Stat(arg[:i]); err == nil {
124 return arg[:i], arg[i+1:]
125 }
126 }
127 // No candidate exists, so the argument names a file that is not there. The
128 // last hash keeps the error naming the path a reader would expect.
129 if idx := strings.LastIndex(arg, "#"); idx > 0 {
130 return arg[:idx], arg[idx+1:]
131 }
132 return arg, ""
133}

Callers 1

assetFromArgFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected