Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag: add optional value description to string parameters #22024

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions vlib/flag/flag.v
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,39 @@ pub fn (mut fs FlagParser) float(name string, abbr u8, fdefault f64, usage strin
return value
}

@[params]
pub struct FlagConfig {
pub:
val_desc string // descriptive string for an argument
spytheman marked this conversation as resolved.
Show resolved Hide resolved
}

// string_multi returns all string values, associated with the flag named `name`.
// When no values for that flag are found, it returns an empty array.
// This version supports abbreviations.
pub fn (mut fs FlagParser) string_multi(name string, abbr u8, usage string) []string {
fs.add_flag(name, abbr, usage, '<multiple strings>')
// This version supports a custom value description.
pub fn (mut fs FlagParser) string_multi(name string, abbr u8, usage string, c FlagConfig) []string {
val_desc := if c.val_desc == '' {
'<multiple strings>'
} else {
c.val_desc
}

fs.add_flag(name, abbr, usage, val_desc)
return fs.parse_value(name, abbr)
}

// string_opt returns an option with the string value, associated with the flag in `name`.
// When the flag is not given by the user, it returns an error.
// This version supports abbreviations.
pub fn (mut fs FlagParser) string_opt(name string, abbr u8, usage string) !string {
fs.add_flag(name, abbr, usage, '<string>')
// This version supports a custom value description.
pub fn (mut fs FlagParser) string_opt(name string, abbr u8, usage string, c FlagConfig) !string {
val_desc := if c.val_desc == '' {
'<string>'
} else {
c.val_desc
}

fs.add_flag(name, abbr, usage, val_desc)
parsed := fs.parse_value(name, abbr)
if parsed.len == 0 {
return error("parameter '${name}' not provided")
Expand All @@ -424,8 +444,9 @@ pub fn (mut fs FlagParser) string_opt(name string, abbr u8, usage string) !strin
// If that flag is given as an option, then its parsed value is returned as a string.
// When it is not, it returns the default string value in `sdefault`.
// This version supports abbreviations.
pub fn (mut fs FlagParser) string(name string, abbr u8, sdefault string, usage string) string {
value := fs.string_opt(name, abbr, usage) or { return sdefault }
// This version supports a custom value description.
pub fn (mut fs FlagParser) string(name string, abbr u8, sdefault string, usage string, c FlagConfig) string {
value := fs.string_opt(name, abbr, usage, c) or { return sdefault }
return value
}

Expand Down
18 changes: 18 additions & 0 deletions vlib/flag/flag_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ fn test_if_no_options_given_usage_message_does_not_contain_options() {
assert !fp.usage().contains('Options:')
}

fn test_default_val_descriptions_for_strings() {
mut fp := flag.new_flag_parser([])
fp.string_multi('a_string', `a`, '')
fp.string('a_string', `s`, '', '')

assert fp.usage().contains('<multiple strings>')
assert fp.usage().contains('<string>')
}

fn test_custom_val_descriptions_for_strings() {
mut fp := flag.new_flag_parser([])
fp.string_multi('a_string', `a`, '', val_desc: '<multi custom>')
fp.string('a_string', `s`, '', '', val_desc: '<custom>')

assert fp.usage().contains('<multi custom>')
assert fp.usage().contains('<custom>')
}

fn test_free_args_could_be_limited() {
mut fp1 := flag.new_flag_parser(['a', 'b', 'c'])
fp1.limit_free_args(1, 4)!
Expand Down
Loading