Skip to content

feat: custom separators for variadic flags #76

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

molisani
Copy link
Member

@molisani molisani commented Apr 1, 2025

Resolves #74

Describe your changes
Allow variadic option for flags to be a string. If set, Stricli will use that string as a separator and call .split(separator) on every raw input (before it is parsed/validated).

For example, variadic: "," will split the raw input --flag 1,2,3 into ["1", "2", "3"] which will each be parsed separately (assuming this is a parsed number flag). This can still be combined with multiple instances of the same flag, so --flag 1,2 --flag 3 will be ["1", "2", "3"] as well.

Testing performed
Added several new test cases to achieve complete coverage.

Additional context
I've also added some additional validation to the flag specification, as the separator cannot be the empty string and should not include any whitespace.

Note: I don't know what would be the best way to indicate this behavior in the help text. I experimented with --flag <placeholder><separator><placeholder> but that got too verbose too fast.

@molisani molisani added the enhancement ✨ New feature or request label Apr 1, 2025
@molisani molisani self-assigned this Apr 1, 2025
@molisani molisani requested a review from a team as a code owner April 1, 2025 18:33
@molisani molisani force-pushed the fix/variadic-separator branch from eba8305 to c9c193b Compare April 1, 2025 18:33
@github-actions github-actions bot added core ⚙ Relates to the @stricli/core package documentation 📝 Improvements or additions to documentation labels Apr 1, 2025
mkubilayk
mkubilayk previously approved these changes Apr 11, 2025
@@ -151,6 +151,8 @@ import DefaultFlagCode from "./examples/default-flag.txt";

A flag can be variadic when the type it represents is an array of values. In this case, the flag can be specified multiple times and each value is then parsed individually and added to a single array. If the type of a flag is an array it must be set as variadic.

If the `variadic` config property is set to a string, Stricli will use that as a separator and split each input string. This is useful for cases where the input string is a single value that contains multiple values separated by a specific character (like a comma).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add an example for this as well? Could move this paragraph below the code example and add another example after that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it would be good to have a working example, but I'm waiting for #60 to write any more interactive examples.

@mkubilayk
Copy link
Collaborator

Note: I don't know what would be the best way to indicate this behavior in the help text. I experimented with --flag <placeholder><separator><placeholder> but that got too verbose too fast.

We could use the right aligned [...] section to mention the separator in the help text.

  -m [--mode]...           Brief                            [separator = ,]

@molisani
Copy link
Member Author

Note: I don't know what would be the best way to indicate this behavior in the help text. I experimented with --flag <placeholder><separator><placeholder> but that got too verbose too fast.

We could use the right aligned [...] section to mention the separator in the help text.

  -m [--mode]...           Brief                            [separator = ,]

That works very nicely as variadic flags can't have default values, so they won't have to stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core ⚙ Relates to the @stricli/core package documentation 📝 Improvements or additions to documentation enhancement ✨ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Would be great if variadic didn't require repeating the flag over and over
2 participants