-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Michael Molisani <[email protected]>
eba8305
to
c9c193b
Compare
@@ -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). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
We could use the right aligned
|
That works very nicely as variadic flags can't have default values, so they won't have to stack. |
Signed-off-by: Michael Molisani <[email protected]>
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.