Skip to content

feat: Support terminated options like find -exec#2

Merged
niemeyer merged 1 commit intocanonical:masterfrom
rebornplusplus:terminated-options
Mar 27, 2023
Merged

feat: Support terminated options like find -exec#2
niemeyer merged 1 commit intocanonical:masterfrom
rebornplusplus:terminated-options

Conversation

@rebornplusplus
Copy link

Enable option to receive arguments until specified terminator is reached or EOL has been found. This is inspired from find -exec [commands..] ; where commands.. is treated as arguments to -exec.

If for an option opt, terminator is specified to be ; (semi-colon), in the following

$ program [options] --opt v --w=x -- "y z" \; [more-options]

--opt will receive {"v", "--w=x", "--", "y z"} as its argument. Note that, the -- inside will also be passed to --opt regardless PassDoubleDash is set or not. However, once the scope of --opt is finished, i.e. terminator ; is reached, -- will act as before if PassDoubleDash is set.

Use tag terminator to specify the terminator for the option related to that field.

Please note that, the specified terminator should be a separate token, instead of being jotted with other characters. For example,

--opt [arguments..] ; [options..]

will be correctly parsed with terminator: ";". However,

--opt [arguments..] arg; [options..]

will not be correctly parsed. The parser will pass "arg;", and continue to look for the terminator in [options..].

Enable option to receive arguments until specified terminator
is reached or EOL has been found. This is inspired from
``find -exec [commands..] ;`` where commands.. is treated as
arguments to -exec.

If for an option ``opt``, ``terminator`` is specified to be
; (semi-colon), in the following

    $ program [options] --opt v --w=x -- "y z" \; [more-options]

--opt will receive {"v", "--w=x", "--", "y z"} as its
argument. Note that, the -- inside will also be passed to
--opt regardless PassDoubleDash is set or not. However,
once the scope of --opt is finished, i.e. terminator ;
is reached, -- will act as before if PassDoubleDash is set.

Use tag ``terminator`` to specify the terminator for
the option related to that field.

Please note that, the specified terminator should be a
separate token, instead of being jotted with other characters.
For example,

    --opt [arguments..] ; [options..]

will be correctly parsed with terminator: ";". However,

    --opt [arguments..] arg; [options..]

will not be correctly parsed. The parser will pass "arg;",
and continue to look for the terminator in [options..].
@rebornplusplus
Copy link
Author

Upstream PR with the same change, for reference: jessevdk#395

Copy link

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

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

I've already reviewed this fairly thoroughly over on the upstream PR, and @rebornplusplus has already updated it with that feedback, so no further comments here. Looks good to me!

Copy link

@woky woky left a comment

Choose a reason for hiding this comment

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

Nice. LGTM.

Copy link

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

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

Again, right next to @woky's, that's a nice and straightforward implementation. I'm sure other projects will appreciate both of these. Thank you.

//
// As shown, "--" between the option and the terminator won't trigger
// double-dash handling (if PassDoubleDash is set), but after the
// terminator it will.

Choose a reason for hiding this comment

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

Nice behavior, and nice explanation. Thanks.

Copy link
Author

Choose a reason for hiding this comment

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

Kudos to @benhoyt for the explanation. 🥂

if len(value) > 0 {
elemVal = reflect.Indirect(reflect.New(elemTp))
for _, val := range value {
if err := convert(val, elemVal, option.tag); err != nil {

Choose a reason for hiding this comment

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

That's quite a confusing API. It's not just converting, but appending the item. Anyway, not something we need to do something about right now.

@niemeyer niemeyer merged commit 01157a7 into canonical:master Mar 27, 2023
rebornplusplus pushed a commit to rebornplusplus/pebble that referenced this pull request Mar 30, 2023
canonical/go-flags#2 has been merged.
Point to the recent commit canonical/go-flags@01157a7.
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.

4 participants