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

Fixes urfave/cli#1648 #1649

Merged
merged 1 commit into from Jan 17, 2023
Merged

Fixes urfave/cli#1648 #1649

merged 1 commit into from Jan 17, 2023

Conversation

palsivertsen
Copy link

@palsivertsen palsivertsen commented Jan 16, 2023

Remove trim of whitespace in StringSliceFlag values, matching the behavior of StringFlag.

What type of PR is this?

  • bug

What this PR does / why we need it:

See #1648

Which issue(s) this PR fixes:

Fixes #1648

Special notes for your reviewer:

I'm uncertain if the trim behavior was intentional or not as there was no tests covering it.

Testing

Added test that verifies that parsing the same value usingStringFlag and StringSliceFlag results in equal values.

Release Notes

`StringSliceFlag` no nonger trims space from values, matching the behavior of `StringFlag`.

Remove trim of whitespace in `StringSliceFlag` values, matching the
behavior of `StringFlag`.
@palsivertsen palsivertsen requested a review from a team as a code owner January 16, 2023 16:53
@dearchap
Copy link
Contributor

@palsivertsen Thanks for the excellent PR. Do you think we should add an option whether to trim space or not ?

@dearchap dearchap merged commit fc28a1c into urfave:v2-maint Jan 17, 2023
@palsivertsen
Copy link
Author

palsivertsen commented Jan 18, 2023

Do you think we should add an option whether to trim space or not ?

No. Users can call strconv.TrimSpace directly if needed.

@skelouse
Copy link
Contributor

Do you think we should add an option whether to trim space or not ?

No. Users can call strconv.TrimSpace directly if needed.

This PR broke any code, including mine that was relying on strings to be trimmed by default.

@dearchap
Copy link
Contributor

@skelouse @palsivertsen Can one of you submit a PR to include the option ? Thanks

@skelouse
Copy link
Contributor

skelouse commented Jan 29, 2023

@skelouse @palsivertsen Can one of you submit a PR to include the option ? Thanks

IMO the option should be NoTrimSpace or KeepSpaces v3 could be TrimSpace

I'll leave it to @palsivertsen as he is relying on the default functionality to not trim.

@palsivertsen
Copy link
Author

@skelouse @dearchap I'll play around with some designs. Should we open a separate issue for this, or reuse #1648 ?

@skelouse
Copy link
Contributor

skelouse commented Jan 31, 2023

@palsivertsen Thanks for the quick response! Just reuse, it's still the same issue.

Edit: I saw another place that was trimming spaces, may help with your use case.

if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
	for _, s := range f.separator.flagSplitMultiValues(val) {
		if err := setValue.Set(strings.TrimSpace(s)); err != nil {
			return fmt.Errorf("could not parse %q as string value from %s for flag %s: %s", val, source, f.Name, err)
		}
	}

	// Set this to false so that we reset the slice if we then set values from
	// flags that have already been set by the environment.
	setValue.hasBeenSet = false
	f.HasBeenSet = true
}

@palsivertsen
Copy link
Author

@skelouse Thanks!

Any idea where to put such a flag? My first instinct was to add it to the StringSliceFlag struct so that one could use it like so:

StringSliceFlag{Name: "slice", NoTrimSpace: true}

But the StringSliceFlag struct is generated.

@skelouse
Copy link
Contributor

@palsivertsen I had the same issue with v2, I'm not exactly sure how they are generated. Maybe someone else can provide input @urfave/cli on how to edit the generated structs for v2.

@palsivertsen
Copy link
Author

@skelouse I figured out how to add the field to generated structs. Here's what I've got so far:
https://github.com/palsivertsen/urfave-cli/commit/12b6187e7f5a91cfbaaa123ff2debdc7da4c76e9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants