Skip to content

Commit e752e59

Browse files
committed
refactor: update command flag handling to focus on --label flags
Modified the command parsing logic to only accept --label flags, treating other flags as regular arguments. Updated related unit tests to reflect changes in flag handling and ensure accurate extraction of flag values. Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com>
1 parent 242ddfd commit e752e59

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

pkg/modelfile/parser/parser.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func splitCommand(line string) (string, []string, []string, error) {
185185

186186
// extractCommandFlags parses the command flags and returns the remaining part of the line
187187
// and the command flags (with values only, without the flag names).
188+
// Only accepts --label flags, other flags are ignored and treated as arguments.
188189
func extractCommandFlags(line string) (string, []string, error) {
189190
flags := []string{}
190191
var i int
@@ -214,10 +215,17 @@ func extractCommandFlags(line string) (string, []string, error) {
214215
}
215216

216217
if flag != "" {
217-
// Extract the value part from --flag=value format
218-
flagValue := extractFlagValue(flag)
219-
if flagValue != "" {
220-
flags = append(flags, flagValue)
218+
// Only process --label flags, ignore all others
219+
if strings.HasPrefix(flag, "--label") {
220+
// Extract the value part from --label=value format
221+
flagValue := extractFlagValue(flag)
222+
if flagValue != "" {
223+
flags = append(flags, flagValue)
224+
}
225+
} else {
226+
// For non-label flags, treat them as part of the remaining arguments
227+
// We need to backtrack to include this flag in the remaining content
228+
return line[start:], flags, nil
221229
}
222230
}
223231
}
@@ -231,9 +239,9 @@ func isFlag(line string, i int) bool {
231239
return i+1 < len(line) && line[i] == '-' && line[i+1] == '-'
232240
}
233241

234-
// extractFlagValue extracts the value from a flag string
242+
// extractFlagValue extracts the value from a --label flag string
235243
// Example: "--label=key=value" returns "key=value"
236-
// Example: "--untested" returns ""
244+
// Example: "--label=" returns ""
237245
func extractFlagValue(flag string) string {
238246
// Remove the leading "--"
239247
if strings.HasPrefix(flag, "--") {

pkg/modelfile/parser/parser_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ func TestSplitCommand(t *testing.T) {
200200
{"MODEL foo", false, "MODEL", []string{"foo"}, []string{}},
201201
{"NAME bar", false, "NAME", []string{"bar"}, []string{}},
202202
{"MODEL --label=key=value /home/user/model.safetensors", false, "MODEL", []string{"/home/user/model.safetensors"}, []string{"key=value"}},
203-
{"MODEL --untested --experimental=test model.safetensors", false, "MODEL", []string{"model.safetensors"}, []string{"test"}},
204-
{"CONFIG --format=json config.yaml", false, "CONFIG", []string{"config.yaml"}, []string{"json"}},
205-
{"MODEL --flag1=value1 --flag2=value2 model.bin", false, "MODEL", []string{"model.bin"}, []string{"value1", "value2"}},
206-
{"MODEL --untested model.safetensors", false, "MODEL", []string{"model.safetensors"}, []string{}}, // flag without value
203+
{"MODEL --untested --experimental=test model.safetensors", false, "MODEL", []string{"--untested", "--experimental=test", "model.safetensors"}, []string{}},
204+
{"CONFIG --format=json config.yaml", false, "CONFIG", []string{"--format=json", "config.yaml"}, []string{}},
205+
{"MODEL --label=flag1=value1 --label=flag2=value2 model.bin", false, "MODEL", []string{"model.bin"}, []string{"flag1=value1", "flag2=value2"}},
206+
{"MODEL --untested model.safetensors", false, "MODEL", []string{"--untested", "model.safetensors"}, []string{}},
207207
{"invalid", true, "", nil, nil},
208208
}
209209

@@ -232,30 +232,25 @@ func TestExtractFlagValue(t *testing.T) {
232232
expectedValue string
233233
}{
234234
{
235-
name: "flag with key=value",
235+
name: "label flag with key=value",
236236
flag: "--label=key=value",
237237
expectedValue: "key=value",
238238
},
239239
{
240-
name: "flag with simple value",
241-
flag: "--format=json",
242-
expectedValue: "json",
243-
},
244-
{
245-
name: "flag without value",
246-
flag: "--untested",
247-
expectedValue: "",
248-
},
249-
{
250-
name: "flag with empty value",
240+
name: "label flag with empty value",
251241
flag: "--label=",
252242
expectedValue: "",
253243
},
254244
{
255-
name: "complex flag value",
245+
name: "complex label flag value",
256246
flag: fmt.Sprintf("--label=%s=true", modelspec.AnnotationMediaTypeUntested),
257247
expectedValue: fmt.Sprintf("%s=true", modelspec.AnnotationMediaTypeUntested),
258248
},
249+
{
250+
name: "label flag without value",
251+
flag: "--label",
252+
expectedValue: "",
253+
},
259254
}
260255

261256
assert := assert.New(t)

0 commit comments

Comments
 (0)