|
1 | 1 | package flags |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "reflect" |
@@ -363,6 +364,22 @@ func TestEnvDefaults(t *testing.T) { |
363 | 364 | } |
364 | 365 | } |
365 | 366 |
|
| 367 | +type CustomFlag struct { |
| 368 | + Value string |
| 369 | +} |
| 370 | + |
| 371 | +func (c *CustomFlag) UnmarshalFlag(s string) error { |
| 372 | + c.Value = s |
| 373 | + return nil |
| 374 | +} |
| 375 | + |
| 376 | +func (c *CustomFlag) IsValidValue(s string) error { |
| 377 | + if !(s == "-1" || s == "-foo") { |
| 378 | + return errors.New("invalid flag value") |
| 379 | + } |
| 380 | + return nil |
| 381 | +} |
| 382 | + |
366 | 383 | func TestOptionAsArgument(t *testing.T) { |
367 | 384 | var tests = []struct { |
368 | 385 | args []string |
@@ -419,30 +436,46 @@ func TestOptionAsArgument(t *testing.T) { |
419 | 436 | rest: []string{"-", "-"}, |
420 | 437 | }, |
421 | 438 | { |
422 | | - // Accept arguments which start with '-' if the next character is a digit, for number options only |
| 439 | + // Accept arguments which start with '-' if the next character is a digit |
423 | 440 | args: []string{"--int-slice", "-3"}, |
424 | 441 | }, |
425 | 442 | { |
426 | | - // Accept arguments which start with '-' if the next character is a digit, for number options only |
| 443 | + // Accept arguments which start with '-' if the next character is a digit |
427 | 444 | args: []string{"--int16", "-3"}, |
428 | 445 | }, |
429 | 446 | { |
430 | | - // Accept arguments which start with '-' if the next character is a digit, for number options only |
| 447 | + // Accept arguments which start with '-' if the next character is a digit |
431 | 448 | args: []string{"--float32", "-3.2"}, |
432 | 449 | }, |
433 | 450 | { |
434 | | - // Accept arguments which start with '-' if the next character is a digit, for number options only |
| 451 | + // Accept arguments which start with '-' if the next character is a digit |
435 | 452 | args: []string{"--float32ptr", "-3.2"}, |
436 | 453 | }, |
| 454 | + { |
| 455 | + // Accept arguments for values that pass the IsValidValue fuction for value validators |
| 456 | + args: []string{"--custom-flag", "-foo"}, |
| 457 | + }, |
| 458 | + { |
| 459 | + // Accept arguments for values that pass the IsValidValue fuction for value validators |
| 460 | + args: []string{"--custom-flag", "-1"}, |
| 461 | + }, |
| 462 | + { |
| 463 | + // Rejects arguments for values that fail the IsValidValue fuction for value validators |
| 464 | + args: []string{"--custom-flag", "-2"}, |
| 465 | + expectError: true, |
| 466 | + errType: ErrExpectedArgument, |
| 467 | + errMsg: "invalid flag value", |
| 468 | + }, |
437 | 469 | } |
438 | 470 |
|
439 | 471 | var opts struct { |
440 | | - StringSlice []string `long:"string-slice"` |
441 | | - IntSlice []int `long:"int-slice"` |
442 | | - Int16 int16 `long:"int16"` |
443 | | - Float32 float32 `long:"float32"` |
444 | | - Float32Ptr *float32 `long:"float32ptr"` |
445 | | - OtherOption bool `long:"other-option" short:"o"` |
| 472 | + StringSlice []string `long:"string-slice"` |
| 473 | + IntSlice []int `long:"int-slice"` |
| 474 | + Int16 int16 `long:"int16"` |
| 475 | + Float32 float32 `long:"float32"` |
| 476 | + Float32Ptr *float32 `long:"float32ptr"` |
| 477 | + OtherOption bool `long:"other-option" short:"o"` |
| 478 | + Custom CustomFlag `long:"custom-flag" short:"c"` |
446 | 479 | } |
447 | 480 |
|
448 | 481 | for _, test := range tests { |
|
0 commit comments